Skip to content

Commit

Permalink
[system] Warn for spacing when non integer value is used with theme.s…
Browse files Browse the repository at this point in the history
…pacing array (#23460)
  • Loading branch information
mnajdova authored Nov 10, 2020
1 parent 3cd5dd6 commit 1fca268
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/material-ui-system/src/spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ export function createUnarySpacing(theme) {
}

if (process.env.NODE_ENV !== 'production') {
if (abs > themeSpacing.length - 1) {
if (!Number.isInteger(abs)) {
console.error(
[
'Material-UI: The `theme.spacing` array type cannot be combined with non integer values.' +
'You should either use an integer value that can be used as index, or define the `theme.spacing` as a number.',
].join('\n'),
);
} else if (abs > themeSpacing.length - 1) {
console.error(
[
`Material-UI: The value provided (${abs}) overflows.`,
Expand Down
25 changes: 25 additions & 0 deletions packages/material-ui-system/src/spacing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ describe('spacing', () => {
);
expect(output).to.deep.equal({ padding: undefined });
});

it('should warn if non integer value is used with theme.spacing defined as array', () => {
let output;
expect(() => {
output = spacing({
theme: {
spacing: [1, 2, 3, 4, 5, 6],
},
p: 0.5,
});
}).toErrorDev(
'Material-UI: The `theme.spacing` array type cannot be combined with non integer values.',
);
expect(output).to.deep.equal({ padding: undefined });
});

it('should accept non integer value', () => {
const output = spacing({
theme: {
spacing: 8,
},
p: 0.5,
});
expect(output).to.deep.equal({ padding: 4 });
});
});

it('should support negative values', () => {
Expand Down

0 comments on commit 1fca268

Please sign in to comment.