{"version":3,"sources":["../../src/use-controlled-component/index.ts"],"sourcesContent":["import { useLatest } from '../use-latest'\nimport { useSafeState } from '../use-safe-state'\nimport { useStableFn } from '../use-stable-fn'\nimport { isFunction, isObject } from '../utils/basic'\n\nimport type { UseSafeStateOptions } from '../use-safe-state'\n\nexport interface UseControlledComponentOptions<T = string, P extends object = object> extends UseSafeStateOptions {\n  /**\n   * The callback that is called when the value of the controlled component changes\n   */\n  onChange?: (value: T, preValue?: T) => void\n  /**\n   * The callback that is called when the value of the controlled component is reset\n   */\n  onReset?: (initialValue: T) => void\n  /**\n   * The props of the controlled component\n   */\n  props?: P | ((value: T) => P)\n  /**\n   * The fallback value of the controlled component\n   */\n  fallbackValue?: T\n}\n\nexport interface ControlledComponentProps<T> {\n  /**\n   * The value of the controlled component\n   */\n  value: T\n  /**\n   * The callback that is called when the value of the controlled component changes\n   */\n  onChange: (eventOrValue?: T | { target: { value: T } }) => void\n}\n\nexport interface UseControlledComponentReturns<T, P> {\n  /**\n   * The value of the controlled component\n   */\n  value: T\n  /**\n   * The props that should be passed to the controlled component\n   */\n  props: ControlledComponentProps<T> & P\n  /**\n   * Reset the value of the controlled component to the initial value\n   */\n  reset(): void\n  /**\n   * Set the value of the controlled component\n   */\n  setValue: (value: T) => void\n}\n\n/**\n * A React Hook that helps you to manage controlled components that receive `value` and `onChange` props meeting the following type.\n */\nexport function useControlledComponent<T = string, P extends object = object>(\n  initialValue: T = '' as T,\n  options: UseControlledComponentOptions<T, P> = {},\n): UseControlledComponentReturns<T, P> {\n  const { fallbackValue, onReset, props, onChange, ...stateOptions } = options\n\n  const [value, setValue] = useSafeState(initialValue as T, stateOptions)\n\n  function handleChange(eventOrValue?: T | { target: { value: T } }) {\n    let newValue = (eventOrValue ?? fallbackValue) as T\n\n    if (isObject(eventOrValue) && 'target' in eventOrValue && 'value' in eventOrValue.target) {\n      newValue = (eventOrValue.target.value ?? fallbackValue) as T\n    }\n\n    const { onChange, value: lastValue } = latest.current\n\n    setValue(newValue)\n    onChange?.(newValue, lastValue)\n  }\n\n  const latest = useLatest({ value, onChange, onReset })\n\n  const reset = useStableFn(() => {\n    setValue(initialValue as T)\n    latest.current.onReset?.(initialValue)\n  })\n\n  const comProps = (isFunction(props) ? props(value) : (props ?? {})) as P\n\n  return {\n    value,\n    reset,\n    props: { ...comProps, onChange: handleChange, value },\n    setValue: useStableFn((value: T) => handleChange(value)),\n  }\n}\n"],"names":["useControlledComponent","initialValue","options","fallbackValue","onReset","props","onChange","stateOptions","useSafeState","value","setValue","handleChange","eventOrValue","newValue","isObject","target","latest","current","lastValue","useLatest","reset","useStableFn","comProps","isFunction"],"mappings":";;;;+BA2DgBA;;;eAAAA;;;yBA3DU;4BACG;2BACD;qBACS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwD9B,SAASA;QACdC,eAAAA,iEAAkB,IAClBC,UAAAA,iEAA+C,CAAC;IAEhD,IAAQC,gBAA6DD,QAA7DC,eAAeC,UAA8CF,QAA9CE,SAASC,QAAqCH,QAArCG,OAAOC,WAA8BJ,QAA9BI,UAAaC,0CAAiBL;;;;;;IAErE,IAA0BM,iCAAAA,IAAAA,0BAAY,EAACP,cAAmBM,mBAAnDE,QAAmBD,kBAAZE,WAAYF;IAE1B,SAASG,aAAaC,YAA2C;QAC/D,IAAIC,WAAYD,yBAAAA,0BAAAA,eAAgBT;QAEhC,IAAIW,IAAAA,eAAQ,EAACF,iBAAiB,YAAYA,gBAAgB,WAAWA,aAAaG,MAAM,EAAE;gBAC5EH;YAAZC,WAAYD,CAAAA,6BAAAA,aAAaG,MAAM,CAACN,KAAK,cAAzBG,wCAAAA,6BAA6BT;QAC3C;QAEA,IAAuCa,kBAAAA,OAAOC,OAAO,EAA7CX,WAA+BU,gBAA/BV,UAAUG,AAAOS,YAA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