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][Checkbox][Radio] Fix theme style overrides not working for different sizes #39377

Merged
merged 6 commits into from
Oct 20, 2023
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
1 change: 1 addition & 0 deletions packages/mui-material/src/Checkbox/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const CheckboxRoot = styled(SwitchBase, {
return [
styles.root,
ownerState.indeterminate && styles.indeterminate,
styles[`size${capitalize(ownerState.size)}`],
ownerState.color !== 'default' && styles[`color${capitalize(ownerState.color)}`],
];
},
Expand Down
43 changes: 43 additions & 0 deletions packages/mui-material/src/Checkbox/Checkbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describeConformance, act, createRenderer } from '@mui-internal/test-uti
import Checkbox, { checkboxClasses as classes } from '@mui/material/Checkbox';
import FormControl from '@mui/material/FormControl';
import ButtonBase from '@mui/material/ButtonBase';
import { ThemeProvider, createTheme } from '@mui/material/styles';

describe('<Checkbox />', () => {
const { render } = createRenderer();
Expand Down Expand Up @@ -92,6 +93,48 @@ describe('<Checkbox />', () => {
});
});

describe('theme: customization', () => {
it('should be customizable in the theme using the size prop.', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}

const theme = createTheme({
components: {
MuiCheckbox: {
styleOverrides: {
sizeMedium: {
marginTop: 40,
paddingLeft: 20,
},
sizeSmall: {
marginLeft: -40,
paddingRight: 2,
},
},
},
},
});

const { container } = render(
<ThemeProvider theme={theme}>
<Checkbox />
<Checkbox size="small" />
</ThemeProvider>,
);

expect(container.querySelector(`.${classes.sizeMedium}`)).toHaveComputedStyle({
marginTop: '40px',
paddingLeft: '20px',
});

expect(container.querySelector(`.${classes.sizeSmall}`)).toHaveComputedStyle({
marginLeft: '-40px',
paddingRight: '2px',
});
});
});

describe('with FormControl', () => {
describe('enabled', () => {
it('should not have the disabled class', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/mui-material/src/Radio/Radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ const RadioRoot = styled(SwitchBase, {
overridesResolver: (props, styles) => {
const { ownerState } = props;

return [styles.root, styles[`color${capitalize(ownerState.color)}`]];
return [
styles.root,
ownerState.size !== 'medium' && styles[`size${capitalize(ownerState.size)}`],
styles[`color${capitalize(ownerState.color)}`],
];
},
})(({ theme, ownerState }) => ({
color: (theme.vars || theme).palette.text.secondary,
Expand Down
33 changes: 33 additions & 0 deletions packages/mui-material/src/Radio/Radio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { describeConformance, createRenderer } from '@mui-internal/test-utils';
import Radio, { radioClasses as classes } from '@mui/material/Radio';
import FormControl from '@mui/material/FormControl';
import ButtonBase from '@mui/material/ButtonBase';
import { ThemeProvider, createTheme } from '@mui/material/styles';

describe('<Radio />', () => {
const { render } = createRenderer();
Expand Down Expand Up @@ -94,4 +95,36 @@ describe('<Radio />', () => {
});
});
});

describe('theme: customization', () => {
it('should be customizable in the theme using the size prop.', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}

const theme = createTheme({
components: {
MuiRadio: {
styleOverrides: {
sizeSmall: {
marginLeft: -40,
paddingRight: 2,
},
},
},
},
});

const { container } = render(
<ThemeProvider theme={theme}>
<Radio size="small" />
</ThemeProvider>,
);

expect(container.querySelector(`.${classes.sizeSmall}`)).toHaveComputedStyle({
marginLeft: '-40px',
paddingRight: '2px',
});
});
});
});