@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
81 lines (80 loc) • 4.14 kB
JavaScript
"use client";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import "core-js/modules/web.dom-collections.iterator.js";
import { useCallback, useContext, useEffect, useReducer, useRef } from 'react';
import useExternalValue from './useExternalValue';
import usePath from './usePath';
import DataContext from '../DataContext/Context';
import ValueProviderContext from '../Value/Provider/ValueProviderContext';
import SummaryListContext from '../Value/SummaryList/SummaryListContext';
export default function useValueProps(localProps) {
var _useExternalValue, _props$label, _fieldInternalsRef$cu, _fieldInternalsRef$cu2, _fieldInternalsRef$cu3;
const [, forceUpdate] = useReducer(() => ({}), {});
const {
extend
} = useContext(ValueProviderContext);
const props = extend(localProps);
const {
verifyChild
} = useContext(SummaryListContext) || {};
verifyChild === null || verifyChild === void 0 ? 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 ? void 0 : setValueInternals(path, props);
useEffect(() => {
if (inheritLabel || inheritVisibility) {
setFieldEventListener === null || setFieldEventListener === void 0 ? 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 ? 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 ? void 0 : (_fieldInternalsRef$cu = fieldInternalsRef.current) === null || _fieldInternalsRef$cu === void 0 ? void 0 : (_fieldInternalsRef$cu2 = _fieldInternalsRef$cu[path]) === null || _fieldInternalsRef$cu2 === void 0 ? void 0 : (_fieldInternalsRef$cu3 = _fieldInternalsRef$cu2.props) === null || _fieldInternalsRef$cu3 === void 0 ? void 0 : _fieldInternalsRef$cu3.label : undefined;
return _objectSpread(_objectSpread({}, props), {}, {
label,
value
});
}
//# sourceMappingURL=useValueProps.js.map