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

[Grid] Prevent crash if spacing is set to zero in theme #33777

Merged
merged 7 commits into from Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
25 changes: 22 additions & 3 deletions docs/data/material/customization/density/DensityTool.js
Expand Up @@ -25,9 +25,20 @@ export default function DensityTool() {
};

const handleSpacingChange = (event, value) => {
let spacing = value || +event.target.value;

// If the entered value is greater than maxSpacing, setting up maxSpacing as value
if (spacing > maxSpacing) {
spacing = maxSpacing;
}
// If the entered value is less than minSpacing, setting up minSpacing as value
if (spacing < minSpacing) {
spacing = minSpacing;
}

dispatch({
type: 'SET_SPACING',
payload: value || +event.target.value,
payload: spacing,
});
};

Expand Down Expand Up @@ -71,7 +82,11 @@ export default function DensityTool() {
</Typography>
</Grid>
<Grid item>
<IconButton aria-label={t('increaseSpacing')} onClick={decreaseSpacing}>
<IconButton
aria-label={t('decreaseSpacing')}
onClick={decreaseSpacing}
disabled={spacingUnit === minSpacing}
>
<DecreaseIcon />
</IconButton>
<Input
Expand All @@ -86,7 +101,11 @@ export default function DensityTool() {
'aria-labelledby': 'input-slider',
}}
/>
<IconButton aria-label={t('decreaseSpacing')} onClick={increaseSpacing}>
<IconButton
aria-label={t('increaseSpacing')}
onClick={increaseSpacing}
disabled={spacingUnit === maxSpacing}
>
<IncreaseIcon />
</IconButton>
</Grid>
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-material/src/Grid/Grid.js
Expand Up @@ -181,7 +181,7 @@ export function generateRowGap({ theme, ownerState }) {
};
}

if (zeroValueBreakpointKeys.includes(breakpoint)) {
if (zeroValueBreakpointKeys?.includes(breakpoint)) {
return {};
}

Expand Down Expand Up @@ -227,7 +227,7 @@ export function generateColumnGap({ theme, ownerState }) {
};
}

if (zeroValueBreakpointKeys.includes(breakpoint)) {
if (zeroValueBreakpointKeys?.includes(breakpoint)) {
return {};
}

Expand Down
19 changes: 19 additions & 0 deletions packages/mui-material/src/Grid/Grid.test.js
Expand Up @@ -689,6 +689,25 @@ describe('Material UI <Grid />', () => {
'Warning: Failed prop type: The prop `spacing` of `Grid` can only be used together with the `container` prop.',
);
});

it('should not throw error for setting zero spacing in theme', () => {
const theme = createTheme({ spacing: 0 });

const App = () => {
return (
<ThemeProvider theme={theme}>
<Grid container spacing={4}>
<Grid item>test</Grid>
<Grid item>test</Grid>
</Grid>
</ThemeProvider>
);
};

expect(() => {
render(<App />);
}).not.to.throw();
});
});

describe('prop: rowSpacing, columnSpacing', () => {
Expand Down