tdesign-react
Version:
TDesign Component for React
37 lines (33 loc) • 1.48 kB
JavaScript
/**
* tdesign v1.15.1
* (c) 2025 tdesign
* @license MIT
*/
import { _ as _slicedToArray } from '../_chunks/dep-48805ab8.js';
import { useState } from 'react';
import { upperFirst } from 'lodash-es';
import noop from '../_util/noop.js';
import '../_chunks/dep-026a4c6b.js';
var useControlled = function useControlled() {
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var valueKey = arguments.length > 1 ? arguments[1] : undefined;
var onChange = arguments.length > 2 ? arguments[2] : undefined;
var defaultOptions = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
var controlled = Reflect.has(props, valueKey);
var value = props[valueKey];
var defaultValue = defaultOptions["default".concat(upperFirst(valueKey))] || props["default".concat(upperFirst(valueKey))];
var _useState = useState(defaultValue),
_useState2 = _slicedToArray(_useState, 2),
internalValue = _useState2[0],
setInternalValue = _useState2[1];
if (controlled) return [value, onChange || noop];
return [internalValue, function (newValue) {
setInternalValue(newValue);
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
onChange === null || onChange === void 0 || onChange.apply(void 0, [newValue].concat(args));
}];
};
export { useControlled as default };
//# sourceMappingURL=useControlled.js.map