-
Notifications
You must be signed in to change notification settings - Fork 46.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
Fix uncontrolled radios #10156
Fix uncontrolled radios #10156
Changes from 2 commits
ce70f13
25cff31
84bda97
48fc77f
15228e8
dede4b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ const propTypes = { | |
children: PropTypes.node.isRequired, | ||
title: PropTypes.node.isRequired, | ||
resolvedIn: semverString, | ||
introducedIn: semverString, | ||
resolvedBy: PropTypes.string | ||
}; | ||
|
||
|
@@ -31,6 +32,7 @@ class TestCase extends React.Component { | |
const { | ||
title, | ||
description, | ||
introducedIn, | ||
resolvedIn, | ||
resolvedBy, | ||
affectedBrowsers, | ||
|
@@ -40,13 +42,13 @@ class TestCase extends React.Component { | |
let { complete } = this.state; | ||
|
||
const { version } = parse(window.location.search); | ||
const isTestRelevant = ( | ||
const isTestFixed = ( | ||
!version || | ||
!resolvedIn || | ||
semver.gte(version, resolvedIn) | ||
); | ||
|
||
complete = !isTestRelevant || complete; | ||
complete = !isTestFixed || complete; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing some local testing, looks like this causes a failure on line 103: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Last time I do merge conflict resolve on github :P man. |
||
|
||
return ( | ||
<section | ||
|
@@ -68,6 +70,16 @@ class TestCase extends React.Component { | |
</h2> | ||
|
||
<dl className="test-case__details"> | ||
{introducedIn && ( | ||
<dt>First broken in: </dt>)} | ||
{introducedIn && ( | ||
<dd> | ||
<a href={'https://github.com/facebook/react/tag/v' + introducedIn}> | ||
<code>{introducedIn}</code> | ||
</a> | ||
</dd> | ||
)} | ||
|
||
{resolvedIn && ( | ||
<dt>First supported in: </dt>)} | ||
{resolvedIn && ( | ||
|
@@ -100,12 +112,12 @@ class TestCase extends React.Component { | |
</p> | ||
|
||
<div className="test-case__body"> | ||
{!isTestRelevant &&( | ||
{!isTestFixed && ( | ||
<p className="test-case__invalid-version"> | ||
<strong>Note:</strong> This test case was fixed in a later version of React. | ||
This test is not expected to pass for the selected version, and that's ok! | ||
</p> | ||
)} | ||
)} | ||
|
||
{children} | ||
</div> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react'; | ||
|
||
import Fixture from '../../Fixture'; | ||
|
||
class RadioGroupFixture extends React.Component { | ||
constructor(props, context) { | ||
super(props, context); | ||
|
||
this.state = { | ||
changeCount: 0, | ||
}; | ||
} | ||
|
||
handleChange = () => { | ||
this.setState(({ changeCount }) => { | ||
return { | ||
changeCount: changeCount + 1 | ||
} | ||
}) | ||
} | ||
|
||
handleReset = () => { | ||
this.setState({ | ||
changeCount: 0, | ||
}) | ||
} | ||
|
||
render() { | ||
const { changeCount } = this.state; | ||
const color = changeCount === 2 ? 'green' : 'red'; | ||
|
||
return ( | ||
<Fixture> | ||
<label> | ||
<input | ||
defaultChecked | ||
name="foo" | ||
type='radio' | ||
onChange={this.handleChange} | ||
/> | ||
Radio 1 | ||
</label> | ||
<label> | ||
<input | ||
name="foo" | ||
type='radio' | ||
onChange={this.handleChange} | ||
/> | ||
Radio 2 | ||
</label> | ||
|
||
{' '} | ||
<p style={{ color }}> | ||
<code>onChange</code>{' calls: '}<strong>{changeCount}</strong> | ||
</p> | ||
<button onClick={this.handleReset}>Reset count</button> | ||
</Fixture> | ||
) | ||
} | ||
} | ||
|
||
export default RadioGroupFixture; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,9 @@ type ValueTracker = { | |
type WrapperState = {_wrapperState: {valueTracker: ?ValueTracker}}; | ||
type ElementWithWrapperState = Element & WrapperState; | ||
type InstanceWithWrapperState = ReactInstance & WrapperState; | ||
type SubjectWithWrapperState = | ||
| InstanceWithWrapperState | ||
| ElementWithWrapperState; | ||
|
||
var ReactDOMComponentTree = require('ReactDOMComponentTree'); | ||
|
||
|
@@ -43,15 +46,11 @@ function getTracker(inst: any) { | |
return inst._wrapperState.valueTracker; | ||
} | ||
|
||
function attachTracker(inst: InstanceWithWrapperState, tracker: ?ValueTracker) { | ||
inst._wrapperState.valueTracker = tracker; | ||
function detachTracker(subject: SubjectWithWrapperState) { | ||
delete subject._wrapperState.valueTracker; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so. I think originally I was being overly cautious about expected properties sticking around. |
||
} | ||
|
||
function detachTracker(inst: InstanceWithWrapperState) { | ||
delete inst._wrapperState.valueTracker; | ||
} | ||
|
||
function getValueFromNode(node) { | ||
function getValueFromNode(node: any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general we should avoid adding (I don’t mean this particular case is problematic, but this is something to always keep in mind.) |
||
var value; | ||
if (node) { | ||
value = isCheckable(node) ? '' + node.checked : node.value; | ||
|
@@ -113,22 +112,22 @@ var inputValueTracking = { | |
return getTracker(ReactDOMComponentTree.getInstanceFromNode(node)); | ||
}, | ||
|
||
trackNode: function(node: ElementWithWrapperState) { | ||
if (node._wrapperState.valueTracker) { | ||
trackNode(node: ElementWithWrapperState) { | ||
if (getTracker(node)) { | ||
return; | ||
} | ||
node._wrapperState.valueTracker = trackValueOnNode(node, node); | ||
}, | ||
|
||
track: function(inst: InstanceWithWrapperState) { | ||
track(inst: InstanceWithWrapperState) { | ||
if (getTracker(inst)) { | ||
return; | ||
} | ||
var node = ReactDOMComponentTree.getNodeFromInstance(inst); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked at the stacktrace and it's crashing here (rather than in the other place I expected). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ooo I think I understand where the bug is |
||
attachTracker(inst, trackValueOnNode(node, inst)); | ||
inst._wrapperState.valueTracker = trackValueOnNode(node, inst); | ||
}, | ||
|
||
updateValueIfChanged(inst: InstanceWithWrapperState | Fiber) { | ||
updateValueIfChanged(inst: SubjectWithWrapperState | Fiber) { | ||
if (!inst) { | ||
return false; | ||
} | ||
|
@@ -144,9 +143,13 @@ var inputValueTracking = { | |
} | ||
|
||
var lastValue = tracker.getValue(); | ||
var nextValue = getValueFromNode( | ||
ReactDOMComponentTree.getNodeFromInstance(inst), | ||
); | ||
|
||
var node = inst; | ||
if (!(inst: any).nodeName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you comment on this part? What are you trying to detect? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah its not great, I'm trying to detect if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use same check as in here and leave a TODO to remove it when Stack is removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perfect 👍 |
||
node = ReactDOMComponentTree.getNodeFromInstance(inst); | ||
} | ||
|
||
var nextValue = getValueFromNode(node); | ||
|
||
if (nextValue !== lastValue) { | ||
tracker.setValue(nextValue); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -860,6 +860,9 @@ ReactDOMComponent.Mixin = { | |
// happen after `_updateDOMProperties`. Otherwise HTML5 input validations | ||
// raise warnings and prevent the new value from being assigned. | ||
ReactDOMInput.updateWrapper(this); | ||
// We also check that we haven't missed a value update, such as a | ||
// Radio group shifting the checked value to another named radio input. | ||
inputValueTracking.updateValueIfChanged(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the actual fix |
||
break; | ||
case 'textarea': | ||
ReactDOMTextarea.updateWrapper(this); | ||
|
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.
I switched to the unminified versions intentionally so was easier to step through stuff in devtools