Skip to content

Commit ffb38e6

Browse files
committed
🔥 Stop syncing the value attribute on inputs
1 parent 0452c9b commit ffb38e6

File tree

3 files changed

+14
-39
lines changed

3 files changed

+14
-39
lines changed

packages/react-dom/src/__tests__/ReactDOMInput-test.js

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ describe('ReactDOMInput', () => {
271271

272272
dispatchEventOnNode(node, 'input');
273273

274-
expect(node.getAttribute('value')).toBe('2.0');
274+
expect(node.getAttribute('value')).toBe('2');
275275
expect(node.value).toBe('2.0');
276276
});
277277
});
@@ -667,7 +667,7 @@ describe('ReactDOMInput', () => {
667667
const node = container.firstChild;
668668

669669
expect(node.value).toBe('0');
670-
expect(node.defaultValue).toBe('0');
670+
expect(node.defaultValue).toBe('');
671671
});
672672

673673
it('should properly transition from 0 to an empty value', function() {
@@ -683,7 +683,7 @@ describe('ReactDOMInput', () => {
683683
const node = container.firstChild;
684684

685685
expect(node.value).toBe('');
686-
expect(node.defaultValue).toBe('');
686+
expect(node.defaultValue).toBe('0');
687687
});
688688

689689
it('should properly transition a text input from 0 to an empty 0.0', function() {
@@ -699,7 +699,7 @@ describe('ReactDOMInput', () => {
699699
const node = container.firstChild;
700700

701701
expect(node.value).toBe('0.0');
702-
expect(node.defaultValue).toBe('0.0');
702+
expect(node.defaultValue).toBe('0');
703703
});
704704

705705
it('should properly transition a number input from "" to 0', function() {
@@ -715,7 +715,7 @@ describe('ReactDOMInput', () => {
715715
const node = container.firstChild;
716716

717717
expect(node.value).toBe('0');
718-
expect(node.defaultValue).toBe('0');
718+
expect(node.defaultValue).toBe('');
719719
});
720720

721721
it('should properly transition a number input from "" to "0"', function() {
@@ -731,7 +731,7 @@ describe('ReactDOMInput', () => {
731731
const node = container.firstChild;
732732

733733
expect(node.value).toBe('0');
734-
expect(node.defaultValue).toBe('0');
734+
expect(node.defaultValue).toBe('');
735735
});
736736

737737
it('should have the correct target value', () => {
@@ -1605,15 +1605,15 @@ describe('ReactDOMInput', () => {
16051605
};
16061606
}
16071607

1608-
it('always sets the attribute when values change on text inputs', function() {
1608+
it('retains the initial value attribute when values change on text inputs', function() {
16091609
const Input = getTestInput();
16101610
const stub = ReactDOM.render(<Input type="text" />, container);
16111611
const node = ReactDOM.findDOMNode(stub);
16121612

16131613
setUntrackedValue.call(node, '2');
16141614
dispatchEventOnNode(node, 'input');
16151615

1616-
expect(node.getAttribute('value')).toBe('2');
1616+
expect(node.getAttribute('value')).toBe('');
16171617
});
16181618

16191619
it('does not set the value attribute on number inputs if focused', () => {
@@ -1632,21 +1632,6 @@ describe('ReactDOMInput', () => {
16321632
expect(node.getAttribute('value')).toBe('1');
16331633
});
16341634

1635-
it('sets the value attribute on number inputs on blur', () => {
1636-
const Input = getTestInput();
1637-
const stub = ReactDOM.render(
1638-
<Input type="number" value="1" />,
1639-
container,
1640-
);
1641-
const node = ReactDOM.findDOMNode(stub);
1642-
1643-
setUntrackedValue.call(node, '2');
1644-
dispatchEventOnNode(node, 'input');
1645-
dispatchEventOnNode(node, 'blur');
1646-
1647-
expect(node.getAttribute('value')).toBe('2');
1648-
});
1649-
16501635
it('an uncontrolled number input will not update the value attribute on blur', () => {
16511636
const node = ReactDOM.render(
16521637
<input type="number" defaultValue="1" />,
@@ -1784,7 +1769,7 @@ describe('ReactDOMInput', () => {
17841769
expect(node.getAttribute('value')).toBe('');
17851770
});
17861771

1787-
it('treats updated Symbol value as an empty string', function() {
1772+
it('treats updated Symbol value as initial value', function() {
17881773
ReactDOM.render(<input value="foo" onChange={() => {}} />, container);
17891774
expect(() =>
17901775
ReactDOM.render(
@@ -1795,7 +1780,7 @@ describe('ReactDOMInput', () => {
17951780
const node = container.firstChild;
17961781

17971782
expect(node.value).toBe('');
1798-
expect(node.getAttribute('value')).toBe('');
1783+
expect(node.getAttribute('value')).toBe('foo');
17991784
});
18001785

18011786
it('treats initial Symbol defaultValue as an empty string', function() {
@@ -1832,7 +1817,7 @@ describe('ReactDOMInput', () => {
18321817
expect(node.getAttribute('value')).toBe('');
18331818
});
18341819

1835-
it('treats updated function value as an empty string', function() {
1820+
it('treats updated function value as initial value', function() {
18361821
ReactDOM.render(<input value="foo" onChange={() => {}} />, container);
18371822
expect(() =>
18381823
ReactDOM.render(
@@ -1843,7 +1828,7 @@ describe('ReactDOMInput', () => {
18431828
const node = container.firstChild;
18441829

18451830
expect(node.value).toBe('');
1846-
expect(node.getAttribute('value')).toBe('');
1831+
expect(node.getAttribute('value')).toBe('foo');
18471832
});
18481833

18491834
it('treats initial function defaultValue as an empty string', function() {

packages/react-dom/src/client/ReactDOMFiberInput.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,7 @@ export function updateWrapper(element: Element, props: Object) {
194194
return;
195195
}
196196

197-
if (props.hasOwnProperty('value')) {
198-
setDefaultValue(node, props.type, value);
199-
} else if (props.hasOwnProperty('defaultValue')) {
197+
if (props.hasOwnProperty('defaultValue')) {
200198
setDefaultValue(node, props.type, getToStringValue(props.defaultValue));
201199
}
202200

@@ -321,11 +319,7 @@ function updateNamedCousins(rootNode, props) {
321319
// when the user is inputting text
322320
//
323321
// https://github.com/facebook/react/issues/7253
324-
export function setDefaultValue(
325-
node: InputWithWrapperState,
326-
type: ?string,
327-
value: *,
328-
) {
322+
function setDefaultValue(node: InputWithWrapperState, type: ?string, value: *) {
329323
if (
330324
// Focused number inputs synchronize on blur. See ChangeEventPlugin.js
331325
type !== 'number' ||

packages/react-dom/src/events/ChangeEventPlugin.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import getEventTarget from './getEventTarget';
2727
import isEventSupported from './isEventSupported';
2828
import {getNodeFromInstance} from '../client/ReactDOMComponentTree';
2929
import * as inputValueTracking from '../client/inputValueTracking';
30-
import {setDefaultValue} from '../client/ReactDOMFiberInput';
3130

3231
const eventTypes = {
3332
change: {
@@ -237,9 +236,6 @@ function handleControlledInputBlur(node) {
237236
if (!state || !state.controlled || node.type !== 'number') {
238237
return;
239238
}
240-
241-
// If controlled, assign the value attribute to the current value on blur
242-
setDefaultValue(node, 'number', node.value);
243239
}
244240

245241
/**

0 commit comments

Comments
 (0)