Skip to content

Commit

Permalink
add NonForwardedProps global config
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp committed Oct 7, 2021
1 parent db8a5f5 commit 5d4a951
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
38 changes: 38 additions & 0 deletions docs/pages/rounded-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as React from 'react';
import { createTheme, ThemeProvider, Button } from '@mui/material';
import { NonForwardedProps } from '@mui/system';

NonForwardedProps.set('MuiButton', ['rounded']);
// NonForwardedProps.set('MuiButtonBase', ['rounded']);
// NonForwardedProps.set('MuiTouchRipple', ['rounded']);

declare module '@mui/material/Button' {
interface ButtonExtraProps {
rounded?: boolean;
}
}

const theme = createTheme({
components: {
MuiButton: {
variants: [
{
props: { rounded: true }, // this works great! but there is no way to update the TS
style: {
borderRadius: 50,
},
},
],
},
},
});

export default function RoundedButton() {
return (
<ThemeProvider theme={theme}>
<Button rounded variant="contained">
Rounded
</Button>
</ThemeProvider>
);
}
4 changes: 3 additions & 1 deletion packages/mui-material/src/Button/Button.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export interface ButtonPropsColorOverrides {}

export interface ButtonPropsSizeOverrides {}

export interface ButtonExtraProps {}

export type ButtonTypeMap<
P = {},
D extends React.ElementType = 'button',
Expand Down Expand Up @@ -84,7 +86,7 @@ export type ButtonTypeMap<
'text' | 'outlined' | 'contained',
ButtonPropsVariantOverrides
>;
};
} & ButtonExtraProps;
defaultComponent: D;
}>;

Expand Down
5 changes: 5 additions & 0 deletions packages/mui-system/src/createStyled.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export interface SerializedStyles {
next?: SerializedStyles;
}

export const NonForwardedProps: {
set: (componentName: string, nonForwardedProps: string[]) => void;
get: (componentName: string, prop: any) => boolean;
};

export type CSSProperties = CSS.PropertiesFallback<number | string>;
export type CSSPropertiesWithMultiValues = {
[K in keyof CSSProperties]: CSSProperties[K] | Array<Extract<CSSProperties[K], string>>;
Expand Down
36 changes: 29 additions & 7 deletions packages/mui-system/src/createStyled.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ const variantsResolver = (props, styles, theme, name) => {
return variantsStyles;
};

function createNonForwardedProps() {
const nonForwardedProps = {};
return {
set(componentName, props) {
nonForwardedProps[componentName] = props;
},
get(componentName, prop) {
const list = nonForwardedProps[componentName];
if (!list) {
return true;
}
return !list.includes(prop);
},
};
}

export const NonForwardedProps = createNonForwardedProps();

export function shouldForwardProp(prop) {
return prop !== 'ownerState' && prop !== 'theme' && prop !== 'sx' && prop !== 'as';
}
Expand All @@ -76,6 +94,7 @@ export default function createStyled(input = {}) {
skipVariantsResolver: inputSkipVariantsResolver,
skipSx: inputSkipSx,
overridesResolver,
shouldForwardProp: shouldForwardPropOption,
...options
} = inputOptions;

Expand All @@ -95,17 +114,20 @@ export default function createStyled(input = {}) {
}
}

let shouldForwardPropOption = shouldForwardProp;
let finalShouldForwardProp = shouldForwardPropOption || shouldForwardProp;

if (componentSlot === 'Root') {
shouldForwardPropOption = rootShouldForwardProp;
} else if (componentSlot) {
// any other slot specified
shouldForwardPropOption = slotShouldForwardProp;
if (!shouldForwardPropOption) {
if (componentSlot === 'Root') {
finalShouldForwardProp = rootShouldForwardProp;
} else if (componentSlot) {
// any other slot specified
finalShouldForwardProp = slotShouldForwardProp;
}
}

const defaultStyledResolver = styledEngineStyled(tag, {
shouldForwardProp: shouldForwardPropOption,
shouldForwardProp: (prop) =>
finalShouldForwardProp(prop) && NonForwardedProps.get(componentName, prop),
label,
...options,
});
Expand Down

0 comments on commit 5d4a951

Please sign in to comment.