1 line
6.7 KiB
JSON
1 line
6.7 KiB
JSON
|
{"ast":null,"code":"import _slicedToArray from \"@babel/runtime/helpers/esm/slicedToArray\";\nimport useEvent from \"./useEvent\";\nimport { useLayoutUpdateEffect } from \"./useLayoutEffect\";\nimport useState from \"./useState\";\n/** We only think `undefined` is empty */\nfunction hasValue(value) {\n return value !== undefined;\n}\n\n/**\n * Similar to `useState` but will use props value if provided.\n * Note that internal use rc-util `useState` hook.\n */\nexport default function useMergedState(defaultStateValue, option) {\n var _ref = option || {},\n defaultValue = _ref.defaultValue,\n value = _ref.value,\n onChange = _ref.onChange,\n postState = _ref.postState;\n\n // ======================= Init =======================\n var _useState = useState(function () {\n if (hasValue(value)) {\n return value;\n } else if (hasValue(defaultValue)) {\n return typeof defaultValue === 'function' ? defaultValue() : defaultValue;\n } else {\n return typeof defaultStateValue === 'function' ? defaultStateValue() : defaultStateValue;\n }\n }),\n _useState2 = _slicedToArray(_useState, 2),\n innerValue = _useState2[0],\n setInnerValue = _useState2[1];\n var mergedValue = value !== undefined ? value : innerValue;\n var postMergedValue = postState ? postState(mergedValue) : mergedValue;\n\n // ====================== Change ======================\n var onChangeFn = useEvent(onChange);\n var _useState3 = useState([mergedValue]),\n _useState4 = _slicedToArray(_useState3, 2),\n prevValue = _useState4[0],\n setPrevValue = _useState4[1];\n useLayoutUpdateEffect(function () {\n var prev = prevValue[0];\n if (innerValue !== prev) {\n onChangeFn(innerValue, prev);\n }\n }, [prevValue]);\n\n // Sync value back to `undefined` when it from control to un-control\n useLayoutUpdateEffect(function () {\n if (!hasValue(value)) {\n setInnerValue(value);\n }\n }, [value]);\n\n // ====================== Update ======================\n var triggerChange = useEvent(function (updater, ignoreDestroy) {\n setInnerValue(updater, ignoreDestroy);\n setPrevValue([mergedValue], ignoreDestroy);\n });\n return [postMergedValue, triggerChange];\n}","map":{"version":3,"names":["_slicedToArray","useEvent","useLayoutUpdateEffect","useState","hasValue","value","undefined","useMergedState","defaultStateValue","option","_ref","defaultValue","onChange","postState","_useState","_useState2","innerValue","setInnerValue","mergedValue","postMergedValue","onChangeFn","_useState3","_useState4","prevValue","setPrevValue","prev","triggerChange","updater","ignoreDestroy"],"sources":["C:/Users/Аришина)/Desktop/promo/node_modules/rc-util/es/hooks/useMergedState.js"],"sourcesContent":["import _slicedToArray from \"@babel/runtime/helpers/esm/slicedToArray\";\nimport useEvent from \"./useEvent\";\nimport { useLayoutUpdateEffect } from \"./useLayoutEffect\";\nimport useState from \"./useState\";\n/** We only think `undefined` is empty */\nfunction hasValue(value) {\n return value !== undefined;\n}\n\n/**\n * Similar to `useState` but will use props value if provided.\n * Note that internal use rc-util `useState` hook.\n */\nexport default function useMergedState(defaultStateValue, option) {\n var _ref = option || {},\n defaultValue = _ref.defaultValue,\n value = _ref.value,\n onChange = _ref.onChange,\n postState = _ref.postState;\n\n // ======================= Init =======================\n var _useState = useState(function () {\n if (hasValue(value)) {\n return value;\n } else if (hasValue(defaultValue)) {\n return typeof defaultValue === 'function' ? defaultValue() : defaultValue;\n } else {\n return typeof defaultStateValue === 'function' ? defaultStateValue() : defaultStateValue;\n }\n }),\n _useState2 = _slicedToArray(_useState, 2),\n innerValue = _useState2[0],\n setInnerValue = _useState2[1];\n var mergedValue = value !== undefined ? value : innerValue;\n var postMergedValue = postState
|