| 34605 | var _react = require("react"); |
| 34606 | var _useConst = require("./useConst"); |
| 34607 | function useControllableValue(controlledValue, defaultUncontrolledValue, onChange) { |
| 34608 | var _a = _react.useState(defaultUncontrolledValue), value = _a[0], setValue = _a[1]; |
| 34609 | var isControlled = (0, _useConst.useConst)(controlledValue !== undefined); |
| 34610 | var currentValue = isControlled ? controlledValue : value; |
| 34611 | // Duplicate the current value and onChange in refs so they're accessible from |
| 34612 | // setValueOrCallOnChange without creating a new callback every time |
| 34613 | var valueRef = _react.useRef(currentValue); |
| 34614 | var onChangeRef = _react.useRef(onChange); |
| 34615 | _react.useEffect(function() { |
| 34616 | valueRef.current = currentValue; |
| 34617 | onChangeRef.current = onChange; |
| 34618 | }); |
| 34619 | // To match the behavior of the setter returned by React.useState, this callback's identity |
| 34620 | // should never change. This means it MUST NOT directly reference variables that can change. |
| 34621 | var setValueOrCallOnChange = (0, _useConst.useConst)(function() { |
| 34622 | return function(update, ev) { |
| 34623 | // Assuming here that TValue is not a function, because a controllable value will typically |
| 34624 | // be something a user can enter as input |
| 34625 | var newValue = typeof update === "function" ? update(valueRef.current) : update; |
| 34626 | if (onChangeRef.current) onChangeRef.current(ev, newValue); |
| 34627 | if (!isControlled) setValue(newValue); |
| 34628 | }; |
| 34629 | }); |
| 34630 | return [ |
| 34631 | currentValue, |
| 34632 | setValueOrCallOnChange |
| 34633 | ]; |
| 34634 | } |
| 34635 | |
| 34636 | },{"react":"8ePka","./useConst":"jDC9C","@parcel/transformer-js/src/esmodule-helpers.js":"jA2du"}],"exwLQ":[function(require,module,exports) { |
| 34637 | var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js"); |