-
-
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
[RadioGroup] Warn for uncontrolled <-> controlled switch #14878
Conversation
Details of bundle changes.Comparing: bdb4baa...df1a893
|
This approach won't work, the constructor is only called once and we need to check on prop changes. The check should probably be done in componentDidUpdate. |
Sorry, I wasn’t really clear. IsControlled is set in the constructor and doesn’t change you should do the same check in componentDidUpdate and warn if the result is different from isControlled. |
@manonthemat We have recently introduced uncontrolled behavior support for this component. We want to support it. What we are interestd into is to warn when people incorrectly switch between the uncontrolled and controlled mode. |
Since we can't make the RadioGroup
/**
- * The default input value, useful when not controlling the component.
+ * The default input value of an uncontrolled RadioGroup. Required when `RadioGroup` is uncontrolled.
*/
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),
|
Thanks for the guidance. I'll give it another shot. |
for a trailing comma
since changing between controlled and uncontrolled behavior is discouraged
@manonthemat Hold fire on my suggestion. @oliviertassinari is suggesting (in private chat) that we shouldn't be opinionated. I personally disagree, but this isn't my personal project. Let's reach a consensus on what to warn and what not before you devote more time to this PR. |
@@ -96,7 +106,7 @@ RadioGroup.propTypes = { | |||
*/ | |||
children: PropTypes.node, | |||
/** | |||
* The default input value, useful when not controlling the component. | |||
* The required default input value of an uncontrolled RadioGroup. |
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 90% of the cases, users will be better off with a select radio value, no matter what.
- NN/g encourages to always have a selected radio: https://www.nngroup.com/articles/checkboxes-vs-radio-buttons/.
- But, it's not something everybody agrees with, e.g. Windows. They recognize that an always selected value should be the norm. However, there are exceptions where not providing a default value is more appropriate:
Exceptions: Don't have a default selection if:
- There is no acceptable default option for safety, security, or legal reasons and therefore the user must make an explicit choice. If the user doesn't make a selection, display an error message to force one.
- The user interface (UI) must reflect the current state and the option hasn't been set yet. A default value would - incorrectly imply that the user doesn't need to make a selection.
- The goal is to collect unbiased data. Default values would bias data collection.
- The group of radio buttons represents a property in a mixed state, which happens when displaying a property for multiple objects that don't have the same setting. Don't display an error message in this case since each object has a valid state.
So, I'm in favor of encouraging an always selected radio, but allow people to have zero selected: nothing required, but highly encouraged.
@@ -20,6 +20,16 @@ class RadioGroup extends React.Component { | |||
} | |||
} | |||
|
|||
componentDidUpdate(_prevProps, prevState) { | |||
warning( | |||
this.isControlled === (prevState === null), |
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.
Something is off here, it makes me think we miss tests.
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('should warn when switching between uncontrolled to controlled', () => { |
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.
same
Co-Authored-By: manonthemat <[email protected]>
Co-Authored-By: manonthemat <[email protected]>
Closes #14696