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

[material-ui][utils] Merge sx instead of override when using mergeSlotProps #45062

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/mui-material/src/utils/mergeSlotProps.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { expect } from 'chai';
import { SxProps } from '@mui/material/styles';

import mergeSlotProps from './mergeSlotProps';

Expand Down Expand Up @@ -44,6 +45,31 @@ describe('utils/index.js', () => {
});
});

it('merge sx', () => {
expect(
mergeSlotProps<{ sx: SxProps }>(
{ sx: { color: 'red' } },
{ sx: { backgroundColor: 'blue' } },
),
).to.deep.equal({
sx: [{ backgroundColor: 'blue' }, { color: 'red' }],
});
});

it('merge sx array', () => {
expect(
mergeSlotProps<{ sx: SxProps }>(
{ sx: [{ color: 'red', '&.Mui-disabled': { opacity: 0 } }] },
{ sx: [{ backgroundColor: 'blue', '&.Mui-disabled': { opacity: 0.5 } }] },
),
).to.deep.equal({
sx: [
{ backgroundColor: 'blue', '&.Mui-disabled': { opacity: 0.5 } },
{ color: 'red', '&.Mui-disabled': { opacity: 0 } },
],
});
});

it('external slot props should override', () => {
expect(
mergeSlotProps<OwnerState>(
Expand Down Expand Up @@ -130,6 +156,35 @@ describe('utils/index.js', () => {
});
});

it('merge sx for callback', () => {
expect(
mergeSlotProps(
() => ({
sx: { color: 'red' },
}),
() => ({
sx: { backgroundColor: 'blue' },
}),
)(),
).to.deep.equal({
sx: [{ backgroundColor: 'blue' }, { color: 'red' }],
});
});

it('merge sx array for callback', () => {
expect(
mergeSlotProps(
() => ({ sx: [{ color: 'red', '&.Mui-disabled': { opacity: 0 } }] }),
() => ({ sx: [{ backgroundColor: 'blue', '&.Mui-disabled': { opacity: 0.5 } }] }),
)(),
).to.deep.equal({
sx: [
{ backgroundColor: 'blue', '&.Mui-disabled': { opacity: 0.5 } },
{ color: 'red', '&.Mui-disabled': { opacity: 0 } },
],
});
});

it('external callback should be called with default slot props', () => {
expect(
mergeSlotProps<(ownerState: OwnerState) => OwnerState>(
Expand Down
30 changes: 24 additions & 6 deletions packages/mui-material/src/utils/mergeSlotProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,38 @@ export default function mergeSlotProps<
externalSlotPropsValue?.style && {
style: { ...defaultSlotPropsValue.style, ...externalSlotPropsValue.style },
}),
...(defaultSlotPropsValue?.sx &&
externalSlotPropsValue?.sx && {
sx: [
...(Array.isArray(defaultSlotPropsValue.sx)
? defaultSlotPropsValue.sx
: [defaultSlotPropsValue.sx]),
...(Array.isArray(externalSlotPropsValue.sx)
? externalSlotPropsValue.sx
: [externalSlotPropsValue.sx]),
],
}),
};
}) as U;
}
const className = clsx(
(defaultSlotProps as Record<string, any>)?.className,
externalSlotProps?.className,
);
const typedDefaultSlotProps = defaultSlotProps as Record<string, any>;
const className = clsx(typedDefaultSlotProps?.className, externalSlotProps?.className);
Comment on lines +49 to +50
Copy link
Member Author

Choose a reason for hiding this comment

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

Logic is the same, just types no remove individual type casting below.

return {
...defaultSlotProps,
...externalSlotProps,
...(!!className && { className }),
...((defaultSlotProps as Record<string, any>)?.style &&
...(typedDefaultSlotProps?.style &&
externalSlotProps?.style && {
style: { ...(defaultSlotProps as Record<string, any>).style, ...externalSlotProps.style },
style: { ...typedDefaultSlotProps.style, ...externalSlotProps.style },
}),
...(typedDefaultSlotProps?.sx &&
externalSlotProps?.sx && {
sx: [
...(Array.isArray(typedDefaultSlotProps.sx)
? typedDefaultSlotProps.sx
: [typedDefaultSlotProps.sx]),
...(Array.isArray(externalSlotProps.sx) ? externalSlotProps.sx : [externalSlotProps.sx]),
],
}),
} as U;
}
Loading