-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add fixture * Fix Uncontrolled radio groups * address feedback * fix tests; prettier * Update TestCase.js
- Loading branch information
Showing
9 changed files
with
140 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
fixtures/dom/src/components/fixtures/input-change-events/RadioGroupFixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
|
||
'use strict'; | ||
|
||
var {ELEMENT_NODE} = require('HTMLNodeType'); | ||
import type {Fiber} from 'ReactFiber'; | ||
import type {ReactInstance} from 'ReactInstanceType'; | ||
|
||
|
@@ -23,6 +24,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 +47,11 @@ function getTracker(inst: any) { | |
return inst._wrapperState.valueTracker; | ||
} | ||
|
||
function attachTracker(inst: InstanceWithWrapperState, tracker: ?ValueTracker) { | ||
inst._wrapperState.valueTracker = tracker; | ||
function detachTracker(subject: SubjectWithWrapperState) { | ||
subject._wrapperState.valueTracker = null; | ||
} | ||
|
||
function detachTracker(inst: InstanceWithWrapperState) { | ||
delete inst._wrapperState.valueTracker; | ||
} | ||
|
||
function getValueFromNode(node) { | ||
function getValueFromNode(node: any) { | ||
var value; | ||
if (node) { | ||
value = isCheckable(node) ? '' + node.checked : node.value; | ||
|
@@ -113,40 +113,46 @@ 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); | ||
attachTracker(inst, trackValueOnNode(node, inst)); | ||
inst._wrapperState.valueTracker = trackValueOnNode(node, inst); | ||
}, | ||
|
||
updateValueIfChanged(inst: InstanceWithWrapperState | Fiber) { | ||
if (!inst) { | ||
updateValueIfChanged(subject: SubjectWithWrapperState | Fiber) { | ||
if (!subject) { | ||
return false; | ||
} | ||
var tracker = getTracker(inst); | ||
var tracker = getTracker(subject); | ||
|
||
if (!tracker) { | ||
if (typeof (inst: any).tag === 'number') { | ||
inputValueTracking.trackNode((inst: any).stateNode); | ||
if (typeof (subject: any).tag === 'number') { | ||
inputValueTracking.trackNode((subject: any).stateNode); | ||
} else { | ||
inputValueTracking.track((inst: any)); | ||
inputValueTracking.track((subject: any)); | ||
} | ||
return true; | ||
} | ||
|
||
var lastValue = tracker.getValue(); | ||
var nextValue = getValueFromNode( | ||
ReactDOMComponentTree.getNodeFromInstance(inst), | ||
); | ||
|
||
var node = subject; | ||
|
||
// TODO: remove check when the Stack renderer is retired | ||
if ((subject: any).nodeType !== ELEMENT_NODE) { | ||
node = ReactDOMComponentTree.getNodeFromInstance(subject); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
trueadm
Contributor
|
||
} | ||
|
||
var nextValue = getValueFromNode(node); | ||
|
||
if (nextValue !== lastValue) { | ||
tracker.setValue(nextValue); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This seems to be causing issues in production for us. This function throws with invalid argument. Do you have any thoughts on how this could happen?