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

[styles] Fix theme default props overriden by Component default props #20091

Merged
merged 1 commit into from
Mar 16, 2020
Merged
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
10 changes: 4 additions & 6 deletions packages/material-ui-styles/src/withStyles/withStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ const withStyles = (stylesOrCreator, options = {}) => Component => {

const WithStyles = React.forwardRef(function WithStyles(props, ref) {
const { classes: classesProp, innerRef, ...other } = props;
const classes = useStyles(props);
// The wrapper receives only user supplied props, which could be a subset of
// the actual props Component might receive due to merging with defaultProps.
// So copying it here would give us the same result in the wrapper as well.
const classes = useStyles({ ...Component.defaultProps, ...props });

let theme;
let more = other;
Expand Down Expand Up @@ -91,11 +94,6 @@ const withStyles = (stylesOrCreator, options = {}) => Component => {
}),
};

// The wrapper receives only user supplied props, which could be a subset of
// the actual props Component might receive due to merging with defaultProps.
// So copying it here would give us the same result in the wrapper as well.
WithStyles.defaultProps = Component.defaultProps;

if (process.env.NODE_ENV !== 'production') {
WithStyles.displayName = `WithStyles(${getDisplayName(Component)})`;
}
Expand Down
27 changes: 27 additions & 0 deletions packages/material-ui-styles/src/withStyles/withStyles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,33 @@ describe('withStyles', () => {
wrapper.unmount();
});

it('should use theme.props instead of defaultProps', () => {
const MuiFoo = () => <div />;
MuiFoo.defaultProps = {
foo: 'foo',
};

const styles = { root: { display: 'flex' } };
const StyledComponent = withStyles(styles, { name: 'MuiFoo' })(MuiFoo);

const wrapper = mount(
<ThemeProvider
theme={createMuiTheme({
props: {
MuiFoo: {
foo: 'bar',
},
},
})}
>
<StyledComponent foo={undefined} />
</ThemeProvider>,
);

assert.strictEqual(wrapper.find(MuiFoo).props().foo, 'bar');
wrapper.unmount();
});

it('should work when depending on a theme', () => {
const styles = theme => ({ root: { padding: theme.spacing(1) } });
const StyledComponent = withStyles(styles, { name: 'MuiTextField' })(Empty);
Expand Down