@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
40 lines (35 loc) • 1.17 kB
text/typescript
function mergeProps(
slotProps: Record<string, any>,
childProps: Record<string, any>,
) {
// all child props should override
const overrideProps = { ...childProps };
for (const propName in childProps) {
const slotPropValue = slotProps[propName];
const childPropValue = childProps[propName];
const isHandler = /^on[A-Z]/.test(propName);
if (isHandler) {
// if the handler exists on both, we compose them
if (slotPropValue && childPropValue) {
overrideProps[propName] = (...args: unknown[]) => {
childPropValue(...args);
slotPropValue(...args);
};
}
// but if it exists only on the slot, we use only this one
else if (slotPropValue) {
overrideProps[propName] = slotPropValue;
}
}
// if it's `style`, we merge them
else if (propName === "style") {
overrideProps[propName] = { ...slotPropValue, ...childPropValue };
} else if (propName === "className") {
overrideProps[propName] = [slotPropValue, childPropValue]
.filter(Boolean)
.join(" ");
}
}
return { ...slotProps, ...overrideProps };
}
export { mergeProps };