Skip to content

Commit bfab5fe

Browse files
committed
Have defaultValue reach DOM node for html input box for facebook#4618
1 parent 55bd203 commit bfab5fe

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

src/renderers/dom/client/wrappers/ReactDOMInput.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,18 @@ var ReactDOMInput = {
6969

7070
var nativeProps = assign({}, props, {
7171
defaultChecked: undefined,
72-
defaultValue: undefined,
73-
value: value != null ? value : inst._wrapperState.initialValue,
7472
checked: checked != null ? checked : inst._wrapperState.initialChecked,
7573
onChange: inst._wrapperState.onChange,
7674
});
7775

76+
if (value !== undefined) {
77+
// for contolled inputs, use defaultValue as an initial value
78+
nativeProps.value = value != null ? value : props.defaultValue;
79+
} else {
80+
// for uncontrolled inputs, pass defaultValue property to DOM element
81+
nativeProps.defaultValue = props.defaultValue;
82+
}
83+
7884
return nativeProps;
7985
},
8086

@@ -103,10 +109,8 @@ var ReactDOMInput = {
103109
warnIfValueIsNull(props);
104110
}
105111

106-
var defaultValue = props.defaultValue;
107112
inst._wrapperState = {
108113
initialChecked: props.defaultChecked || false,
109-
initialValue: defaultValue != null ? defaultValue : null,
110114
listeners: null,
111115
onChange: _handleChange.bind(inst),
112116
};

src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js

+63
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('ReactDOMInput', function() {
3636
stub = ReactTestUtils.renderIntoDocument(stub);
3737
var node = ReactDOM.findDOMNode(stub);
3838

39+
expect(node.getAttribute('value')).toBe('0');
3940
expect(node.value).toBe('0');
4041
});
4142

@@ -55,6 +56,68 @@ describe('ReactDOMInput', function() {
5556
expect(node.value).toBe('false');
5657
});
5758

59+
it('should update `defaultValue` for uncontrolled input', function() {
60+
var container = document.createElement('div');
61+
62+
var el = ReactDOM.render(<input type="text" defaultValue="0" />, container);
63+
var node = ReactDOM.findDOMNode(el);
64+
65+
expect(node.value).toBe('0');
66+
67+
ReactDOM.render(<input type="text" defaultValue="1" />, container);
68+
69+
expect(node.value).toBe('1');
70+
});
71+
72+
it('should take `defaultValue` for a controlled input', function() {
73+
var container = document.createElement('div');
74+
75+
var el = ReactDOM.render(<input type="text" value={null} defaultValue="0" />, container);
76+
var node = ReactDOM.findDOMNode(el);
77+
78+
expect(node.value).toBe('0');
79+
80+
ReactDOM.render(<input type="text" value={null} defaultValue="1" />, container);
81+
82+
expect(node.value).toBe('1');
83+
84+
ReactDOM.render(<input type="text" value={3} defaultValue="2" />, container);
85+
86+
expect(node.value).toBe('3');
87+
88+
ReactDOM.render(<input type="text" value={5} defaultValue="4" />, container);
89+
90+
expect(node.value).toBe('5');
91+
expect(node.getAttribute('value')).toBe('4');
92+
expect(node.defaultValue).toBe('4');
93+
});
94+
95+
it('should update `value` when changing to controlled input', function() {
96+
var container = document.createElement('div');
97+
98+
var el = ReactDOM.render(<input type="text" defaultValue="0" />, container);
99+
var node = ReactDOM.findDOMNode(el);
100+
101+
expect(node.value).toBe('0');
102+
103+
ReactDOM.render(<input type="text" value="1" defaultValue="0" />, container);
104+
105+
expect(node.value).toBe('1');
106+
});
107+
108+
it('should clear `value` when changing to uncontrolled input', function() {
109+
var container = document.createElement('div');
110+
111+
var el = ReactDOM.render(<input type="text" value="0" readOnly="true" />, container);
112+
var node = ReactDOM.findDOMNode(el);
113+
114+
expect(node.value).toBe('0');
115+
116+
ReactDOM.render(<input type="text" defaultValue="1" />, container);
117+
118+
expect(node.value).toBe('');
119+
});
120+
58121
it('should display "foobar" for `defaultValue` of `objToString`', function() {
59122
var objToString = {
60123
toString: function() {

src/renderers/dom/shared/HTMLDOMPropertyConfig.js

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ var HTMLDOMPropertyConfig = {
8181
data: null, // For `<object />` acts as `src`.
8282
dateTime: MUST_USE_ATTRIBUTE,
8383
default: HAS_BOOLEAN_VALUE,
84+
defaultValue: MUST_USE_PROPERTY,
8485
defer: HAS_BOOLEAN_VALUE,
8586
dir: MUST_USE_ATTRIBUTE,
8687
disabled: MUST_USE_ATTRIBUTE | HAS_BOOLEAN_VALUE,

0 commit comments

Comments
 (0)