UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

78 lines (77 loc) 3.19 kB
"use client"; import { useCallback, useContext, useEffect, useReducer, useRef } from 'react'; import useExternalValue from "./useExternalValue.js"; import usePath from "./usePath.js"; import DataContext from "../DataContext/Context.js"; import ValueProviderContext from "../Value/Provider/ValueProviderContext.js"; import SummaryListContext from "../Value/SummaryList/SummaryListContext.js"; export default function useValueProps(localProps) { var _useExternalValue, _props$label, _fieldInternalsRef$cu; const [, forceUpdate] = useReducer(() => ({}), {}); const { extend } = useContext(ValueProviderContext); const props = extend(localProps); const { verifyChild } = useContext(SummaryListContext) || {}; verifyChild === null || verifyChild === void 0 || verifyChild(); const { path: pathProp, value: valueProp, itemPath, defaultValue, inheritVisibility, inheritLabel, transformIn = external => external, toInput = internal => internal, fromExternal = external => external } = props; const transformers = useRef({ transformIn, toInput, fromExternal }); const { path } = usePath({ path: pathProp, itemPath }); const externalValue = (_useExternalValue = useExternalValue({ path, itemPath, value: valueProp, transformers })) !== null && _useExternalValue !== void 0 ? _useExternalValue : defaultValue; const { fieldInternalsRef, mountedFieldsRef, setValueInternals, setFieldEventListener } = useContext(DataContext) || {}; setValueInternals === null || setValueInternals === void 0 || setValueInternals(path, props); useEffect(() => { if (inheritLabel || inheritVisibility) { setFieldEventListener === null || setFieldEventListener === void 0 || setFieldEventListener(path, 'onMount', () => { requestAnimationFrame(forceUpdate); }); } }, [setFieldEventListener, path, inheritVisibility, inheritLabel]); const shouldBeVisible = useCallback(path => { var _mountedFieldsRef$cur; const item = mountedFieldsRef === null || mountedFieldsRef === void 0 || (_mountedFieldsRef$cur = mountedFieldsRef.current) === null || _mountedFieldsRef$cur === void 0 ? void 0 : _mountedFieldsRef$cur.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 === null || fieldInternalsRef === void 0 || (_fieldInternalsRef$cu = fieldInternalsRef.current) === null || _fieldInternalsRef$cu === void 0 || (_fieldInternalsRef$cu = _fieldInternalsRef$cu[path]) === null || _fieldInternalsRef$cu === void 0 || (_fieldInternalsRef$cu = _fieldInternalsRef$cu.props) === null || _fieldInternalsRef$cu === void 0 ? void 0 : _fieldInternalsRef$cu.label : undefined; return { ...props, label, value }; } //# sourceMappingURL=useValueProps.js.map