89 lines
3.0 KiB
JavaScript
89 lines
3.0 KiB
JavaScript
'use client'
|
|
"use strict";
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
|
|
// src/use-controllable.ts
|
|
var use_controllable_exports = {};
|
|
__export(use_controllable_exports, {
|
|
useControllableProp: () => useControllableProp,
|
|
useControllableState: () => useControllableState
|
|
});
|
|
module.exports = __toCommonJS(use_controllable_exports);
|
|
var import_utils2 = require("@chakra-ui/utils");
|
|
var import_react3 = require("react");
|
|
|
|
// src/use-callback-ref.ts
|
|
var import_react2 = require("react");
|
|
|
|
// src/use-safe-layout-effect.ts
|
|
var import_utils = require("@chakra-ui/utils");
|
|
var import_react = require("react");
|
|
var useSafeLayoutEffect = import_utils.isBrowser ? import_react.useLayoutEffect : import_react.useEffect;
|
|
|
|
// src/use-callback-ref.ts
|
|
function useCallbackRef(fn, deps = []) {
|
|
const ref = (0, import_react2.useRef)(fn);
|
|
useSafeLayoutEffect(() => {
|
|
ref.current = fn;
|
|
});
|
|
return (0, import_react2.useCallback)((...args) => {
|
|
var _a;
|
|
return (_a = ref.current) == null ? void 0 : _a.call(ref, ...args);
|
|
}, deps);
|
|
}
|
|
|
|
// src/use-controllable.ts
|
|
function useControllableProp(prop, state) {
|
|
const isControlled = prop !== void 0;
|
|
const value = isControlled && typeof prop !== "undefined" ? prop : state;
|
|
return [isControlled, value];
|
|
}
|
|
function useControllableState(props) {
|
|
const {
|
|
value: valueProp,
|
|
defaultValue,
|
|
onChange,
|
|
shouldUpdate = (prev, next) => prev !== next
|
|
} = props;
|
|
const onChangeProp = useCallbackRef(onChange);
|
|
const shouldUpdateProp = useCallbackRef(shouldUpdate);
|
|
const [valueState, setValue] = (0, import_react3.useState)(defaultValue);
|
|
const isControlled = valueProp !== void 0;
|
|
const value = isControlled ? valueProp : valueState;
|
|
const updateValue = (0, import_react3.useCallback)(
|
|
(next) => {
|
|
const nextValue = (0, import_utils2.runIfFn)(next, value);
|
|
if (!shouldUpdateProp(value, nextValue)) {
|
|
return;
|
|
}
|
|
if (!isControlled) {
|
|
setValue(nextValue);
|
|
}
|
|
onChangeProp(nextValue);
|
|
},
|
|
[isControlled, onChangeProp, value, shouldUpdateProp]
|
|
);
|
|
return [value, updateValue];
|
|
}
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
useControllableProp,
|
|
useControllableState
|
|
});
|
|
//# sourceMappingURL=use-controllable.js.map
|