@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
46 lines (45 loc) • 1.92 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = require("react");
var _componentHelper = require("../../../../shared/component-helper.js");
var _ValueProviderContext = _interopRequireDefault(require("./ValueProviderContext.js"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function useValueProvider(props) {
const {
overwriteProps,
...restProps
} = props || {};
const nestedContext = (0, _react.useContext)(_ValueProviderContext.default);
const inheritedProps = nestedContext === null || nestedContext === void 0 ? void 0 : nestedContext.inheritedContext;
const nestedValueProps = (0, _react.useMemo)(() => {
if (inheritedProps && Object.keys(inheritedProps).length > 0) {
return {
...inheritedProps,
...restProps
};
}
return restProps;
}, [inheritedProps, restProps]);
const extend = (0, _react.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 ? (0, _componentHelper.assignPropsWithContext)(props, nestedValueProps) : props;
return value;
}, [nestedValueProps, overwriteProps]);
return {
extend,
inheritedProps: restProps,
inheritedContext: nestedValueProps
};
}
var _default = exports.default = useValueProvider;
//# sourceMappingURL=useValueProvider.js.map