UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

83 lines (82 loc) 2.85 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useValueProps; var _react = require("react"); var _useExternalValue2 = _interopRequireDefault(require("./useExternalValue.js")); var _usePath = _interopRequireDefault(require("./usePath.js")); var _Context = _interopRequireDefault(require("../DataContext/Context.js")); var _ValueProviderContext = _interopRequireDefault(require("../Value/Provider/ValueProviderContext.js")); var _SummaryListContext = _interopRequireDefault(require("../Value/SummaryList/SummaryListContext.js")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function useValueProps(localProps) { var _useExternalValue, _props$label; const [, forceUpdate] = (0, _react.useReducer)(() => ({}), {}); const { extend } = (0, _react.useContext)(_ValueProviderContext.default); const props = extend(localProps); const { verifyChild } = (0, _react.useContext)(_SummaryListContext.default) || {}; verifyChild?.(); const { path: pathProp, value: valueProp, itemPath, defaultValue, inheritVisibility, inheritLabel, transformIn = external => external, toInput = internal => internal, fromExternal = external => external } = props; const transformers = (0, _react.useRef)({ transformIn, toInput, fromExternal }); const { path } = (0, _usePath.default)({ path: pathProp, itemPath }); const externalValue = (_useExternalValue = (0, _useExternalValue2.default)({ path, itemPath, value: valueProp, transformers })) !== null && _useExternalValue !== void 0 ? _useExternalValue : defaultValue; const { fieldInternalsRef, mountedFieldsRef, setValueInternals, setFieldEventListener } = (0, _react.useContext)(_Context.default) || {}; setValueInternals?.(path, props); (0, _react.useEffect)(() => { if (inheritLabel || inheritVisibility) { setFieldEventListener?.(path, 'onMount', () => { requestAnimationFrame(forceUpdate); }); } }, [setFieldEventListener, path, inheritVisibility, inheritLabel]); const shouldBeVisible = (0, _react.useCallback)(path => { const item = mountedFieldsRef?.current?.get(path); if (!item || !inheritVisibility) { return true; } return item.isVisible !== false && (item.isPreMounted !== false || item.wasStepChange === true); }, [inheritVisibility, mountedFieldsRef]); const value = shouldBeVisible(path) ? transformIn(toInput(externalValue)) : undefined; const label = (_props$label = props.label) !== null && _props$label !== void 0 ? _props$label : inheritLabel ? fieldInternalsRef?.current?.[path]?.props?.label : undefined; return { ...props, label, value }; } //# sourceMappingURL=useValueProps.js.map