@stanfordspezi/spezi-web-design-system
Version:
Stanford Biodesign Digital Health Spezi Web Design System
53 lines (52 loc) • 1.31 kB
JavaScript
import * as t from "react";
import { u as m } from "./index-hURUNS5V.mjs";
var v = t[" useInsertionEffect ".trim().toString()] || m;
function w({
prop: e,
defaultProp: s,
onChange: o = () => {
},
caller: f
}) {
const [u, c, i] = R({
defaultProp: s,
onChange: o
}), n = e !== void 0, a = n ? e : u;
{
const r = t.useRef(e !== void 0);
t.useEffect(() => {
const l = r.current;
l !== n && console.warn(
`${f} is changing from ${l ? "controlled" : "uncontrolled"} to ${n ? "controlled" : "uncontrolled"}. Components should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled value for the lifetime of the component.`
), r.current = n;
}, [n, f]);
}
const d = t.useCallback(
(r) => {
if (n) {
const l = C(r) ? r(e) : r;
l !== e && i.current?.(l);
} else
c(r);
},
[n, e, c, i]
);
return [a, d];
}
function R({
defaultProp: e,
onChange: s
}) {
const [o, f] = t.useState(e), u = t.useRef(o), c = t.useRef(s);
return v(() => {
c.current = s;
}, [s]), t.useEffect(() => {
u.current !== o && (c.current?.(o), u.current = o);
}, [o, u]), [o, f, c];
}
function C(e) {
return typeof e == "function";
}
export {
w as u
};