Skip to content

Commit

Permalink
[@mantine/styles] Fix components props with undefined value from coll…
Browse files Browse the repository at this point in the history
…istions with default props (#985)
  • Loading branch information
rtivital committed Mar 11, 2022
1 parent d34b759 commit 3941fa5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/mantine-styles/src/theme/MantineProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DEFAULT_THEME } from './default-theme';
import type { MantineThemeOverride, MantineTheme } from './types';
import type { CSSObject } from '../tss';
import { mergeThemeWithFunctions } from './utils/merge-theme/merge-theme';
import { filterProps } from './utils/filter-props/filter-props';
import { NormalizeCSS } from './NormalizeCSS';

type ProviderStyles = Record<
Expand Down Expand Up @@ -47,7 +48,7 @@ export function useMantineDefaultProps<T extends Record<string, any>>(
props: T
): T {
const contextProps = useContext(MantineProviderContext)?.defaultProps?.[component] || {};
return { ...defaultProps, ...contextProps, ...props };
return { ...defaultProps, ...contextProps, ...filterProps(props) };
}

function GlobalStyles() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { filterProps } from './filter-props';

describe('@mantine/styles filterProps', () => {
it('filters out props with undefined value', () => {
expect(
filterProps({
a: undefined,
b: null,
c: 0,
d: '',
e: {},
f: [],
})
).toStrictEqual({
b: null,
c: 0,
d: '',
e: {},
f: [],
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function filterProps<T extends Record<string, any>>(props: T) {
return Object.keys(props).reduce<T>((acc, key: keyof T) => {
if (props[key] !== undefined) {
acc[key] = props[key];
}
return acc;
}, {} as T);
}

0 comments on commit 3941fa5

Please sign in to comment.