@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
77 lines (76 loc) • 2.44 kB
JavaScript
"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;
const [, forceUpdate] = useReducer(() => ({}), {});
const {
extend
} = useContext(ValueProviderContext);
const props = extend(localProps);
const {
verifyChild
} = useContext(SummaryListContext) || {};
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?.(path, props);
useEffect(() => {
if (inheritLabel || inheritVisibility) {
setFieldEventListener?.(path, 'onMount', () => {
requestAnimationFrame(forceUpdate);
});
}
}, [setFieldEventListener, path, inheritVisibility, inheritLabel]);
const shouldBeVisible = 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