From 981b033eb2d8530c7c5c20fb8dcdd8e806b28148 Mon Sep 17 00:00:00 2001 From: Siriwat K Date: Wed, 13 Nov 2024 11:12:02 +0700 Subject: [PATCH] [material-ui][Grid v1] Fix regression spacing prop with string value (#44376) --- packages/mui-material/src/Grid/Grid.js | 4 ++-- packages/mui-material/src/Grid/Grid.test.js | 17 +++++++++++++++-- .../src/createTheme/createSpacing.test.ts | 6 ++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/mui-material/src/Grid/Grid.js b/packages/mui-material/src/Grid/Grid.js index 4968b320d73537..7aa3c302748f61 100644 --- a/packages/mui-material/src/Grid/Grid.js +++ b/packages/mui-material/src/Grid/Grid.js @@ -170,7 +170,7 @@ export function generateRowGap({ theme, ownerState }) { if (themeSpacing !== '0px') { return { - marginTop: theme.spacing(-propValue), + marginTop: `-${themeSpacing}`, [`& > .${gridClasses.item}`]: { paddingTop: themeSpacing, }, @@ -214,7 +214,7 @@ export function generateColumnGap({ theme, ownerState }) { styles = handleBreakpoints({ theme }, columnSpacingValues, (propValue, breakpoint) => { const themeSpacing = theme.spacing(propValue); if (themeSpacing !== '0px') { - const negativeValue = theme.spacing(-propValue); + const negativeValue = `-${themeSpacing}`; return { width: `calc(100% + ${themeSpacing})`, marginLeft: negativeValue, diff --git a/packages/mui-material/src/Grid/Grid.test.js b/packages/mui-material/src/Grid/Grid.test.js index dd2c069b9ff9da..92d4e970b9cb98 100644 --- a/packages/mui-material/src/Grid/Grid.test.js +++ b/packages/mui-material/src/Grid/Grid.test.js @@ -746,7 +746,7 @@ describe('Material UI ', () => { generateRowGap({ ownerState: { container: true, - rowSpacing: { xs: 1, sm: 2 }, + rowSpacing: { xs: 1, sm: 2, md: '16px' }, }, theme, }), @@ -763,13 +763,19 @@ describe('Material UI ', () => { }, marginTop: '-16px', }, + [`@media (min-width:${defaultTheme.breakpoints.values.md}px)`]: { + '& > .MuiGrid-item': { + paddingTop: '16px', + }, + marginTop: '-16px', + }, }); expect( generateColumnGap({ ownerState: { container: true, - columnSpacing: { xs: 1, sm: 2 }, + columnSpacing: { xs: 1, sm: 2, md: '16px' }, }, theme, }), @@ -788,6 +794,13 @@ describe('Material UI ', () => { marginLeft: '-16px', width: 'calc(100% + 16px)', }, + [`@media (min-width:${defaultTheme.breakpoints.values.md}px)`]: { + '& > .MuiGrid-item': { + paddingLeft: '16px', + }, + marginLeft: '-16px', + width: 'calc(100% + 16px)', + }, }); }); diff --git a/packages/mui-system/src/createTheme/createSpacing.test.ts b/packages/mui-system/src/createTheme/createSpacing.test.ts index 41f917b4c881bf..e11f7193d5ca37 100644 --- a/packages/mui-system/src/createTheme/createSpacing.test.ts +++ b/packages/mui-system/src/createTheme/createSpacing.test.ts @@ -51,6 +51,12 @@ describe('createSpacing', () => { expect(spacing(1, 'auto', 2, 3)).to.equal('0.25rem auto 0.5rem 0.75rem'); }); + it('should support valid CSS unit', () => { + const spacing = createSpacing(); + expect(spacing('16px')).to.equal('16px'); + expect(spacing('1rem')).to.equal('1rem'); + }); + describe('warnings', () => { it('should warn for wrong input', () => { expect(() => {