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

fix(CompDefaultProps): comp default props type #2065

Merged
merged 5 commits into from Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/mantine-core/src/Input/use-input-props.ts
Expand Up @@ -8,9 +8,9 @@ interface BaseProps extends InputWrapperBaseProps, InputSharedProps, DefaultProp
id?: string;
}

export function useInputProps<T extends BaseProps>(
export function useInputProps<T extends BaseProps, U extends Partial<T>>(
component: string,
defaultProps: Partial<T>,
defaultProps: U,
props: T
) {
const {
Expand All @@ -35,7 +35,7 @@ export function useInputProps<T extends BaseProps>(
inputWrapperOrder,
withAsterisk,
...others
} = useComponentDefaultProps(component, defaultProps, props);
} = useComponentDefaultProps<T>(component, defaultProps, props);

const uid = useId(id);

Expand Down
12 changes: 9 additions & 3 deletions src/mantine-styles/src/theme/MantineProvider.tsx
Expand Up @@ -41,11 +41,17 @@ export function useMantineEmotionCache() {
return useContext(MantineProviderContext)?.emotionCache;
}

export function useComponentDefaultProps<T extends Record<string, any>>(
export function useComponentDefaultProps<T extends Record<string, any>, U extends Partial<T> = {}>(
component: string,
defaultProps: Partial<T>,
defaultProps: U,
props: T
): T {
): [keyof U] extends [keyof T]
? T
: {
[Key in Exclude<keyof T, keyof U>]: T[Key];
} & {
[Key in Extract<keyof T, keyof U>]-?: U[Key] & T[Key];
} {
const theme = useMantineTheme();
const contextProps = theme.components[component]?.defaultProps;
return { ...defaultProps, ...contextProps, ...filterProps(props) };
Expand Down
@@ -1,8 +1,12 @@
type FilterPropsRes<T extends Record<string, any>> = {
[Key in keyof T]-?: T[Key] extends undefined ? never : T[Key];
};

export function filterProps<T extends Record<string, any>>(props: T) {
return Object.keys(props).reduce<T>((acc, key: keyof T) => {
return Object.keys(props).reduce<FilterPropsRes<T>>((acc, key: keyof T) => {
if (props[key] !== undefined) {
acc[key] = props[key];
}
return acc;
}, {} as T);
}, {} as FilterPropsRes<T>);
}