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

[FormControlLabel] Fix label prop type to be in-line with other label prop types #31139

Merged
merged 5 commits into from
Mar 23, 2022
Merged

[FormControlLabel] Fix label prop type to be in-line with other label prop types #31139

merged 5 commits into from
Mar 23, 2022

Conversation

jannes-io
Copy link
Contributor

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.

@mui-bot
Copy link

mui-bot commented Feb 18, 2022

Details of bundle changes

Generated by 🚫 dangerJS against 92f8ce9

@jannes-io jannes-io marked this pull request as draft February 18, 2022 12:08
… other label prop types

This change would prevent typing issues when using libraries such as react-i18next.
@jannes-io jannes-io marked this pull request as ready for review February 18, 2022 12:23
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Feb 28, 2022
@@ -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;
Copy link
Member

@oliviertassinari oliviertassinari Mar 9, 2022

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)

Copy link
Contributor Author

@jannes-io jannes-io Mar 10, 2022

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.

Copy link
Member

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.

Copy link
Contributor Author

@jannes-io jannes-io Mar 14, 2022

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

image

Copy link
Member

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.

Copy link
Contributor Author

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.

@oliviertassinari oliviertassinari added the component: text field This is the name of the generic UI component, not the React module! label Mar 9, 2022
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Mar 10, 2022
Copy link
Member

@michaldudak michaldudak left a 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!

@mnajdova mnajdova changed the title [FormControlLabel] fix FormControlLabel:label prop type to be in-line with other label prop types [FormControlLabel] Fix label prop type to be in-line with other label prop types Mar 23, 2022
@mnajdova mnajdova merged commit ec4bc06 into mui:master Mar 23, 2022
@jannes-io jannes-io deleted the formcontrollabel-label-prop-type branch March 23, 2022 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: text field This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants