{"version":3,"file":"use-controlled-state.cjs","names":["useState","isValueDefined","getDefinedValue","useCallback"],"sources":["../../../src/utils/hooks/use-controlled-state.ts"],"sourcesContent":["import { useEffect, useState, useCallback } from '@wordpress/element';\nimport { isValueDefined, getDefinedValue } from '../values';\nimport type { ControlledStateOptions } from './types';\n\nconst defaultOptions: Readonly< { initial: undefined; fallback: '' } > = {\n\tinitial: undefined,\n\t/**\n\t * Defaults to empty string, as that is preferred for usage with\n\t * <input />, <textarea />, and <select /> form elements.\n\t */\n\tfallback: '',\n};\n\n/**\n * Custom hooks for \"controlled\" components to track and consolidate internal\n * state and incoming values. This is useful for components that render\n * `input`, `textarea`, or `select` HTML elements.\n *\n * https://reactjs.org/docs/forms.html#controlled-components\n *\n * At first, a component using useControlledState receives an initial prop\n * value, which is used as initial internal state.\n *\n * This internal state can be maintained and updated without\n * relying on new incoming prop values.\n *\n * Unlike the basic useState hook, useControlledState's state can\n * be updated if a new incoming prop value is changed.\n *\n * @param currentState             The current value.\n * @param [options=defaultOptions] Additional options for the hook.\n *\n * @return The controlled value and the value setter.\n */\nfunction useControlledState< T >(\n\tcurrentState: T | undefined,\n\toptions: ControlledStateOptions< T > = defaultOptions\n): [ T | '', ( nextState: T ) => void ] {\n\tconst { initial, fallback } = { ...defaultOptions, ...options };\n\n\tconst [ internalState, setInternalState ] = useState( currentState );\n\tconst hasCurrentState = isValueDefined( currentState );\n\n\t/*\n\t * Resets internal state if value every changes from uncontrolled <-> controlled.\n\t */\n\tuseEffect( () => {\n\t\tif ( hasCurrentState && internalState ) {\n\t\t\tsetInternalState( undefined );\n\t\t}\n\t}, [ hasCurrentState, internalState ] );\n\n\tconst state = getDefinedValue(\n\t\t[ currentState, internalState, initial ],\n\t\tfallback\n\t);\n\n\tconst setState = useCallback(\n\t\t( nextState: T ) => {\n\t\t\tif ( ! hasCurrentState ) {\n\t\t\t\tsetInternalState( nextState );\n\t\t\t}\n\t\t},\n\t\t[ hasCurrentState ]\n\t);\n\n\treturn [ state, setState ];\n}\n\nexport default useControlledState;\n"],"mappings":";;;;;;;;AAIA,MAAM,iBAAmE;CACxE,SAAS,KAAA;;;;;CAKT,UAAU;AACX;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAS,mBACR,cACA,UAAuC,gBACA;CACvC,MAAM,EAAE,SAAS,aAAa;EAAE,GAAG;EAAgB,GAAG;CAAQ;CAE9D,MAAM,CAAE,eAAe,qBAAA,GAAqBA,mBAAAA,SAAAA,CAAU,YAAa;CACnE,MAAM,mBAAA,GAAkBC,aAAAA,eAAAA,CAAgB,YAAa;CAKrD,CAAA,GAAA,mBAAA,UAAA,OAAiB;EAChB,IAAK,mBAAmB,eACvB,iBAAkB,KAAA,CAAU;CAE9B,GAAG,CAAE,iBAAiB,aAAc,CAAE;CAgBtC,OAAO,EAAA,GAdOC,aAAAA,gBAAAA,CACb;EAAE;EAAc;EAAe;CAAQ,GACvC,QAYY,IAAA,GATIC,mBAAAA,YAAAA,EACd,cAAkB;EACnB,IAAK,CAAE,iBACN,iBAAkB,SAAU;CAE9B,GACA,CAAE,eAAgB,CAGI,CAAE;AAC1B"}