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 ? 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);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