@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
40 lines (39 loc) • 1.65 kB
JavaScript
"use client";
import { useCallback, useContext, useMemo } from 'react';
import { assignPropsWithContext } from "../../../../shared/component-helper.js";
import ValueProviderContext from "./ValueProviderContext.js";
function useValueProvider(props) {
const {
overwriteProps,
...restProps
} = props || {};
const nestedContext = useContext(ValueProviderContext);
const inheritedProps = nestedContext === null || nestedContext === void 0 ? void 0 : nestedContext.inheritedContext;
const nestedValueProps = useMemo(() => {
if (inheritedProps && Object.keys(inheritedProps).length > 0) {
return {
...inheritedProps,
...restProps
};
}
return restProps;
}, [inheritedProps, restProps]);
const extend = useCallback(valueProps => {
var _valueProps$path;
const key = overwriteProps && (valueProps === null || valueProps === void 0 || (_valueProps$path = valueProps.path) === null || _valueProps$path === void 0 || (_valueProps$path = _valueProps$path.split('/')) === null || _valueProps$path === void 0 ? void 0 : _valueProps$path.pop());
const overwrite = overwriteProps === null || overwriteProps === void 0 ? void 0 : overwriteProps[key];
const props = overwrite ? {
...valueProps,
...overwrite
} : valueProps;
const value = Object.keys(nestedValueProps).length > 0 ? assignPropsWithContext(props, nestedValueProps) : props;
return value;
}, [nestedValueProps, overwriteProps]);
return {
extend,
inheritedProps: restProps,
inheritedContext: nestedValueProps
};
}
export default useValueProvider;
//# sourceMappingURL=useValueProvider.js.map