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YAEA,sFAAsF;YACtF,6DAA6D;YAC7D,WAAW,aAAa;QAC1B;IACF,GACA;QAAC;KAAS;IAGZ,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 =\n  typeof document !== 'undefined'\n    ? (React['useInsertionEffect'] ?? React.useLayoutEffect)\n    : () => {};\n\nexport function useControlledState<T, C = T>(\n  value: Exclude<T, undefined>,\n  defaultValue: Exclude<T, undefined> | undefined,\n  onChange?: (v: C, ...args: any[]) => void\n): [T, (value: SetStateAction<T>, ...args: any[]) => void];\nexport function useControlledState<T, C = T>(\n  value: Exclude<T, undefined> | undefined,\n  defaultValue: Exclude<T, undefined>,\n  onChange?: (v: C, ...args: any[]) => void\n): [T, (value: SetStateAction<T>, ...args: any[]) => void];\nexport function useControlledState<T, C = T>(\n  value: T,\n  defaultValue: T,\n  onChange?: (v: C, ...args: any[]) => void\n): [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(\n        `WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`\n      );\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(\n    (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    },\n    [onChange]\n  );\n\n  return [currentValue, setValue];\n}\n"],"names":[],"version":3,"file":"useControlledState.cjs.map"}