Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Theme] Merge components and slots props #35477

Merged
merged 4 commits into from Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/mui-utils/src/resolveProps.test.ts
Expand Up @@ -44,4 +44,40 @@ describe('resolveProps', () => {
foo: '',
});
});

it('merge components and componentsProps props', () => {
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
expect(
resolveProps(
{ components: { Input: 'Input' }, componentsProps: { input: { className: 'input' } } },
{
components: { Root: 'Root' },
componentsProps: { root: { className: 'root' }, input: { style: { color: 'red' } } },
},
),
).to.deep.equal({
components: { Root: 'Root', Input: 'Input' },
componentsProps: {
root: { className: 'root' },
input: { className: 'input', style: { color: 'red' } },
},
});
});

it('merge slots and slotProps props', () => {
expect(
resolveProps(
{ slots: { input: 'input' }, slotProps: { input: { className: 'input' } } },
{
slots: { root: 'root' },
slotProps: { root: { className: 'root' }, input: { style: { color: 'red' } } },
},
),
).to.deep.equal({
slots: { root: 'root', input: 'input' },
slotProps: {
root: { className: 'root' },
input: { className: 'input', style: { color: 'red' } },
},
});
});
});
41 changes: 35 additions & 6 deletions packages/mui-utils/src/resolveProps.ts
Expand Up @@ -4,14 +4,43 @@
* @param {object} props
* @returns {object} resolved props
*/
export default function resolveProps<T extends { className?: string } & Record<string, unknown>>(
defaultProps: T,
props: T,
) {
export default function resolveProps<
T extends {
components?: Record<string, unknown>;
componentsProps?: Record<string, unknown>;
slots?: Record<string, unknown>;
slotProps?: Record<string, unknown>;
} & Record<string, unknown>,
>(defaultProps: T, props: T) {
const output = { ...props };

Object.keys(defaultProps).forEach((propName: keyof T) => {
if (output[propName] === undefined) {
(Object.keys(defaultProps) as Array<keyof T>).forEach((propName) => {
if (propName.toString().match(/^(components|slots)$/)) {
output[propName] = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the slotProps & componentsProps, should we merge the second level too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's better.

...(defaultProps[propName] as any),
...(output[propName] as any),
};
} else if (propName.toString().match(/^(componentsProps|slotProps)$/)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also merge props like TransitionProps (just by one depth lile slots and components ?
To solve #34978

Or should it be a distinct PR ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be a separate PR. I propose to keep it small to fix the main issue first.

const defaultSlotProps = (defaultProps[propName] || {}) as T[keyof T];
const slotProps = props[propName] as {} as T[keyof T];
output[propName] = {} as T[keyof T];
Comment on lines +24 to +26
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeScript is hard to get right for logic like this 🥲


if (!slotProps || !Object.keys(slotProps)) {
// Reduce the iteration if the slot props is empty
output[propName] = defaultSlotProps;
} else if (!defaultSlotProps || !Object.keys(defaultSlotProps)) {
// Reduce the iteration if the default slot props is empty
output[propName] = slotProps;
} else {
output[propName] = { ...slotProps };
Object.keys(defaultSlotProps).forEach((slotPropName) => {
(output[propName] as Record<string, unknown>)[slotPropName] = resolveProps(
(defaultSlotProps as Record<string, any>)[slotPropName],
(slotProps as Record<string, any>)[slotPropName],
);
});
}
} else if (output[propName] === undefined) {
output[propName] = defaultProps[propName];
}
});
Expand Down