@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
43 lines • 1.13 kB
JavaScript
export function extendPropsWithContext(props, defaults = {}, ...contexts) {
props = {
...defaults,
...props
};
return {
...props,
...reduceContextHasValue(props, defaults, contexts)
};
}
export function extendPropsWithContextInClassComponent(props, defaults = {}, ...contexts) {
return {
...props,
...reduceContextHasValue(props, defaults, contexts, {
onlyMergeExistingProps: true
})
};
}
export function reduceContext(contexts) {
return contexts.reduce((acc, cur) => {
if (cur) {
acc = {
...acc,
...cur
};
}
return acc;
}, {});
}
function reduceContextHasValue(props, defaults, contexts, {
onlyMergeExistingProps = false
} = {}) {
const context = reduceContext(contexts);
return Object.entries(context).reduce((acc, [key, value]) => {
if (!onlyMergeExistingProps || Object.prototype.hasOwnProperty.call(props, key)) {
if (props[key] === (defaults === null || defaults === void 0 ? void 0 : defaults[key])) {
acc[key] = value;
}
}
return acc;
}, {});
}
//# sourceMappingURL=extendPropsWithContext.js.map