-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[FormControlLabel] Fix label prop type to be in-line with other label prop types #31139
[FormControlLabel] Fix label prop type to be in-line with other label prop types #31139
Conversation
… other label prop types This change would prevent typing issues when using libraries such as react-i18next.
@@ -45,7 +45,7 @@ export interface FormControlLabelProps | |||
/** | |||
* A text or an element to be used in an enclosing label element. | |||
*/ | |||
label: string | number | React.ReactElement; | |||
label: React.ReactNode; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Going in the opposite direction of #29324 sounds great, but we still need to account for when the label is empty. I forgot about this in #25883 which lead to this bug: #29301. cc @michaldudak (I had a look at this PR because Jannes is in the hiring pipeline).
For the solution, I would propose 1. adding a test case for an empty label and 2. checking how disableTypography
is implemented in the other components, and copying it (done better)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to match implementation of other components. Additionally, since there now is a nullish check I've set the label type to label?: React.ReactNode
. I'm not entirely sure if it makes sense to allow dropping the property, might be some special case. But there's definitely a valid case to allow null
and undefined
in relation to translation frameworks, be it not displaying untranslated values or even asynchronously fetching translations/label values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels weird to have a FormControlLabel whose sole purpose is to render a label and allow the label
prop to not exist. IMO it's ok for the prop to accept null
. undefined
doesn't feel right to me, but if it is common among i18n frameworks, I can live with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michaldudak yeah I completely agree with you there, I share the same concern. I've written a quick codesandbox for usage with i18next
and react-i18next
and you can see that without any additional typing it doesn't accept the result because it's possibly undefined.
https://codesandbox.io/s/react-typescript-forked-t10648?file=/src/App.tsx
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, we can specify the type to be label: React.ReactNode;
(without ?:
). This will make the prop mandatory but will allow it to accept undefined
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Damn I forgot that React.ReactNode
includes | undefined
so yeah, PR has been updated to make the prop required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks OK to me now. Thanks for your work!
This change would prevent typing issues when using libraries such as react-i18next. It also simplifies the FormControlLabel label type to be the same as all other elements that provide a
label
prop.Add tests for different label types.