-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[Badge] Typescript Custom Variant does not work #24323
Comments
Thanks for reporting @xiaoyu-tamu The problem was introduced in #23745 when creating the unstyled component. The problem is that the unstyled component defines the variant as a union of strings, not as overridable string union. I believe we need to move the usage of the diff --git a/packages/material-ui-unstyled/src/BadgeUnstyled/BadgeUnstyled.d.ts b/packages/material-ui-unstyled/src/BadgeUnstyled/BadgeUnstyled.d.ts
index a2b86fba17..a3427eeaea 100644
--- a/packages/material-ui-unstyled/src/BadgeUnstyled/BadgeUnstyled.d.ts
+++ b/packages/material-ui-unstyled/src/BadgeUnstyled/BadgeUnstyled.d.ts
@@ -1,4 +1,5 @@
import * as React from 'react';
+import { OverridableStringUnion } from '@material-ui/types';
import { OverridableComponent, OverridableTypeMap, OverrideProps } from '../OverridableComponent';
export interface BadgeOrigin {
@@ -6,6 +7,9 @@ export interface BadgeOrigin {
horizontal: 'left' | 'right';
}
+export interface BadgePropsVariantOverrides {}
+export type BadgeVariantDefaults = Record<'standard' | 'dot', true>;
+
export interface BadgeUnstyledTypeMap<P = {}, D extends React.ElementType = 'div'> {
props: P & {
/**
@@ -102,7 +106,7 @@ export interface BadgeUnstyledTypeMap<P = {}, D extends React.ElementType = 'div
* The variant to use.
* @default 'standard'
*/
- variant?: 'standard' | 'dot';
+ variant?: OverridableStringUnion<BadgeVariantDefaults, BadgePropsVariantOverrides>;
};
defaultComponent: D;
}
diff --git a/packages/material-ui/src/Badge/Badge.d.ts b/packages/material-ui/src/Badge/Badge.d.ts
index d9446e0534..abb2cd79ea 100644
--- a/packages/material-ui/src/Badge/Badge.d.ts
+++ b/packages/material-ui/src/Badge/Badge.d.ts
@@ -1,6 +1,5 @@
import * as React from 'react';
import { SxProps } from '@material-ui/system';
-import { OverridableStringUnion } from '@material-ui/types';
import {
ExtendBadgeUnstyledTypeMap,
BadgeUnstyledTypeMap,
@@ -9,9 +8,6 @@ import {
import { Theme } from '../styles';
import { OverridableComponent, OverrideProps } from '../OverridableComponent';
-export interface BadgePropsVariantOverrides {}
-export type BadgeVariantDefaults = Record<'standard' | 'dot', true>;
-
export type BadgeTypeMap<
D extends React.ElementType = 'span',
P = {}
@@ -38,11 +34,6 @@ export type BadgeTypeMap<
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx?: SxProps<Theme>;
- /**
- * The variant to use.
- * @default 'standard'
- */
- variant?: OverridableStringUnion<BadgeVariantDefaults, BadgePropsVariantOverrides>;
};
defaultComponent: D;
}>; I don't like that we need to move the variants logic to the unstyled component, but for this component, the variant affects the rendered tree of the component itself. Maybe we should try to refactor and move this logic to the styled version 🤷 @oliviertassinari @eps1lon what do you think? |
@mnajdova Did you consider this solution? diff --git a/packages/material-ui-unstyled/src/BadgeUnstyled/BadgeUnstyled.d.ts b/packages/material-ui-unstyled/src/BadgeUnstyled/BadgeUnstyled.d.ts
index a2b86fba17..48e2b2d5c1 100644
--- a/packages/material-ui-unstyled/src/BadgeUnstyled/BadgeUnstyled.d.ts
+++ b/packages/material-ui-unstyled/src/BadgeUnstyled/BadgeUnstyled.d.ts
@@ -102,7 +102,7 @@ export interface BadgeUnstyledTypeMap<P = {}, D extends React.ElementType = 'div
* The variant to use.
* @default 'standard'
*/
- variant?: 'standard' | 'dot';
+ variant?: string;
};
defaultComponent: D;
}
diff --git a/packages/material-ui-unstyled/src/BadgeUnstyled/BadgeUnstyled.js b/packages/material-ui-unstyled/src/BadgeUnstyled/BadgeUnstyled.js
index 585096889c..3f27c36d8c 100644
--- a/packages/material-ui-unstyled/src/BadgeUnstyled/BadgeUnstyled.js
+++ b/packages/material-ui-unstyled/src/BadgeUnstyled/BadgeUnstyled.js
@@ -206,10 +206,7 @@ BadgeUnstyled.propTypes = {
* The variant to use.
* @default 'standard'
*/
- variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
- PropTypes.oneOf(['dot', 'standard']),
- PropTypes.string,
- ]),
+ variant: PropTypes.string,
};
export default BadgeUnstyled; On a broader note:
diff --git a/packages/material-ui-types/index.d.ts b/packages/material-ui-types/index.d.ts
index 378e37492a..93de7d3a0c 100644
--- a/packages/material-ui-types/index.d.ts
+++ b/packages/material-ui-types/index.d.ts
@@ -47,7 +47,7 @@ export type Omit<T, K extends keyof any> = T extends any ? Pick<T, Exclude<keyof
*
* @internal
*/
-export type OverridableStringUnion<T, U = {}> = GenerateStringUnion<Overwrite<T, U>>;
+export type OverridableStringUnion<T, U = {}> = GenerateStringUnion<Overwrite<Record<T, true>, U>>;
/**
* Like `T & U`, but using the value types from `U` where their properties overlap.
diff --git a/packages/material-ui/src/Button/Button.d.ts b/packages/material-ui/src/Button/Button.d.ts
index 93b55007f7..36180c5c2f 100644
--- a/packages/material-ui/src/Button/Button.d.ts
+++ b/packages/material-ui/src/Button/Button.d.ts
@@ -4,8 +4,8 @@ import { Theme } from '../styles';
import { ExtendButtonBase, ExtendButtonBaseTypeMap } from '../ButtonBase';
import { OverrideProps, OverridableComponent, OverridableTypeMap } from '../OverridableComponent';
-export interface ButtonPropsVariantOverrides {}
-export type ButtonVariantDefaults = Record<'text' | 'outlined' | 'contained', true>;
+export interface ButtonPropsOverridesVariant {}
+export interface ButtonPropsOverridesColor {}
export type ButtonTypeMap<
P = {},
@@ -97,7 +97,7 @@ export type ButtonTypeMap<
* The color of the component. It supports those theme colors that make sense for this component.
* @default 'primary'
*/
- color?: 'inherit' | 'primary' | 'secondary';
+ color?: OverridableStringUnion<'inherit' | 'primary' | 'secondary', ButtonPropsOverridesColor>;
/**
* If `true`, the component is disabled.
* @default false
@@ -145,7 +145,7 @@ export type ButtonTypeMap<
* The variant to use.
* @default 'text'
*/
- variant?: OverridableStringUnion<ButtonVariantDefaults, ButtonPropsVariantOverrides>;
+ variant?: OverridableStringUnion<'text' | 'outlined' | 'contained', ButtonPropsOverridesVariant>;
};
defaultComponent: D;
}>; |
@oliviertassinari right I forgot that I shortly scanned #24267, falling a bit behind after the vacation 😄 Yeah, it makes sense to just use |
Seems to me we haven't gotten to the right abstraction if logic specific to the styled component is part of the unstyled component. I agree that the logic specific to the styled version should be part of the styled implementation. The unstyled component shouldn't know about different variants. In the end, one might want to implement a Badge without variants. |
I second this, that is why I mentioned it. For the |
Current Behavior 😯
Expected Behavior 🤔
Typescript should not throw error
Steps to Reproduce 🕹
https://codesandbox.io/s/ecstatic-jackson-i6z4j?file=/src/App.tsx
Your Environment 🌎
`npx @material-ui/envinfo`
The text was updated successfully, but these errors were encountered: