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AAsF;YACtF,6DAA6D;YAC7D,WAAW,aAAa;QAC1B;IACF,GAAG;QAAC;KAAS;IAEb,OAAO;QAAC;QAAc;KAAS;AACjC","sources":["packages/react-stately/src/utils/useControlledState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport React, {SetStateAction, useCallback, useEffect, useReducer, useRef, useState} from 'react';\n\n// Use the earliest effect possible to reset the ref below.\nconst useEarlyEffect: typeof React.useLayoutEffect = typeof document !== 'undefined'\n  ? React['useInsertionEffect'] ?? React.useLayoutEffect\n  : () => {};\n\nexport function useControlledState<T, C = T>(value: Exclude<T, undefined>, defaultValue: Exclude<T, undefined> | undefined, onChange?: (v: C, ...args: any[]) => void): [T, (value: SetStateAction<T>, ...args: any[]) => void];\nexport function useControlledState<T, C = T>(value: Exclude<T, undefined> | undefined, defaultValue: Exclude<T, undefined>, onChange?: (v: C, ...args: any[]) => void): [T, (value: SetStateAction<T>, ...args: any[]) => void];\nexport function useControlledState<T, C = T>(value: T, defaultValue: T, onChange?: (v: C, ...args: any[]) => void): [T, (value: SetStateAction<T>, ...args: any[]) => void] {\n  // Store the value in both state and a ref. The state value will only be used when uncontrolled.\n  // The ref is used to track the most current value, which is passed to the function setState callback.\n  let [stateValue, setStateValue] = useState(value || defaultValue);\n  let valueRef = useRef(stateValue);\n\n  let isControlledRef = useRef(value !== undefined);\n  let isControlled = value !== undefined;\n  useEffect(() => {\n    let wasControlled = isControlledRef.current;\n    if (wasControlled !== isControlled && process.env.NODE_ENV !== 'production') {\n      console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`);\n    }\n    isControlledRef.current = isControlled;\n  }, [isControlled]);\n\n  // After each render, update the ref to the current value.\n  // This ensures that the setState callback argument is reset.\n  // Note: the effect should not have any dependencies so that controlled values always reset.\n  let currentValue = isControlled ? value : stateValue;\n  useEarlyEffect(() => {\n    valueRef.current = currentValue;\n  });\n\n  let [, forceUpdate] = useReducer(() => ({}), {});\n  let setValue = useCallback((value: SetStateAction<T>, ...args: any[]) => {\n    // @ts-ignore - TS doesn't know that T cannot be a function.\n    let newValue = typeof value === 'function' ? value(valueRef.current) : value;\n    if (!Object.is(valueRef.current, newValue)) {\n      // Update the ref so that the next setState callback has the most recent value.\n      valueRef.current = newValue;\n\n      setStateValue(newValue);\n\n      // Always trigger a re-render, even when controlled, so that the layout effect above runs to reset the value.\n      forceUpdate();\n\n      // Trigger onChange. Note that if setState is called multiple times in a single event,\n      // onChange will be called for each one instead of only once.\n      onChange?.(newValue, ...args);\n    }\n  }, [onChange]);\n\n  return [currentValue, setValue];\n}\n"],"names":[],"version":3,"file":"useControlledState.mjs.map"}