Skip to content

Commit 3e1a768

Browse files
committed
Use strict equality as a guard before assigning input.value
This commit adds back the guard around assigning the value property to an input, however it does it using a strict equals. This prevents validated inputs, like emails and urls from losing the cursor position. It also adds associated test fixtures.
1 parent 0df1709 commit 3e1a768

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

fixtures/dom/src/components/fixtures/text-inputs/index.js

+28
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,34 @@ class TextInputFixtures extends React.Component {
4343
</p>
4444
</TestCase>
4545

46+
<TestCase title="Cursor when editing email inputs">
47+
<TestCase.Steps>
48+
<li>Type "[email protected]"</li>
49+
<li>Select "@"</li>
50+
<li>Type ".", to replace "@" with a period</li>
51+
</TestCase.Steps>
52+
53+
<TestCase.ExpectedResult>
54+
The text field's cursor should not jump to the end.
55+
</TestCase.ExpectedResult>
56+
57+
<InputTestCase type="email" defaultValue="" />
58+
</TestCase>
59+
60+
<TestCase title="Cursor when editing url inputs">
61+
<TestCase.Steps>
62+
<li>Type "http://www.example.com"</li>
63+
<li>Select "www."</li>
64+
<li>Press backspace/delete</li>
65+
</TestCase.Steps>
66+
67+
<TestCase.ExpectedResult>
68+
The text field's cursor should not jump to the end.
69+
</TestCase.ExpectedResult>
70+
71+
<InputTestCase type="url" defaultValue="" />
72+
</TestCase>
73+
4674
<TestCase title="All inputs" description="General test of all inputs">
4775
<InputTestCase type="text" defaultValue="Text" />
4876
<InputTestCase type="email" defaultValue="[email protected]"/>

src/renderers/dom/fiber/wrappers/ReactDOMFiberInput.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,7 @@ var ReactDOMInput = {
209209
// browsers typically do this as necessary, jsdom doesn't.
210210
node.value = '' + value;
211211
}
212-
// eslint-disable-next-line
213-
} else {
212+
} else if (node.value !== value) {
214213
// Cast `value` to a string to ensure the value is set correctly. While
215214
// browsers typically do this as necessary, jsdom doesn't.
216215
node.value = '' + value;

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ var ReactDOMInput = {
201201
// browsers typically do this as necessary, jsdom doesn't.
202202
node.value = '' + value;
203203
}
204-
// eslint-disable-next-line
205-
} else {
204+
} else if (node.value !== value) {
206205
// Cast `value` to a string to ensure the value is set correctly. While
207206
// browsers typically do this as necessary, jsdom doesn't.
208207
node.value = '' + value;

0 commit comments

Comments
 (0)