Skip to content

Commit 36546b5

Browse files
Illunhunzaker
authored andcommitted
Set the correct initial value on input range (#12939)
* Set the correct initial value on input range * Add description and update value diff check for input range * add isHydrating argument and tests * update node value according to isHydrating
1 parent 65ab536 commit 36546b5

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

Diff for: packages/react-dom/src/__tests__/ReactDOMServerIntegrationForms-test.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,13 @@ describe('ReactDOMServerIntegration', () => {
348348
ControlledSelect;
349349
beforeEach(() => {
350350
ControlledInput = class extends React.Component {
351+
static defaultProps = {
352+
type: 'text',
353+
initialValue: 'Hello',
354+
};
351355
constructor() {
352-
super();
353-
this.state = {value: 'Hello'};
356+
super(...arguments);
357+
this.state = {value: this.props.initialValue};
354358
}
355359
handleChange(event) {
356360
if (this.props.onChange) {
@@ -361,6 +365,7 @@ describe('ReactDOMServerIntegration', () => {
361365
render() {
362366
return (
363367
<input
368+
type={this.props.type}
364369
value={this.state.value}
365370
onChange={this.handleChange.bind(this)}
366371
/>
@@ -551,6 +556,27 @@ describe('ReactDOMServerIntegration', () => {
551556
expect(changeCount).toBe(0);
552557
});
553558

559+
it('should not blow away user-interaction on successful reconnect to an uncontrolled range input', () =>
560+
testUserInteractionBeforeClientRender(
561+
<input type="text" defaultValue="0.5" />,
562+
'0.5',
563+
'1',
564+
));
565+
566+
it('should not blow away user-interaction on successful reconnect to a controlled range input', async () => {
567+
let changeCount = 0;
568+
await testUserInteractionBeforeClientRender(
569+
<ControlledInput
570+
type="range"
571+
initialValue="0.25"
572+
onChange={() => changeCount++}
573+
/>,
574+
'0.25',
575+
'1',
576+
);
577+
expect(changeCount).toBe(0);
578+
});
579+
554580
it('should not blow away user-entered text on successful reconnect to an uncontrolled checkbox', () =>
555581
testUserInteractionBeforeClientRender(
556582
<input type="checkbox" defaultChecked={true} />,

Diff for: packages/react-dom/src/client/ReactDOMFiberComponent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ export function setInitialProperties(
530530
// TODO: Make sure we check if this is still unmounted or do any clean
531531
// up necessary since we never stop tracking anymore.
532532
inputValueTracking.track((domElement: any));
533-
ReactDOMFiberInput.postMountWrapper(domElement, rawProps);
533+
ReactDOMFiberInput.postMountWrapper(domElement, rawProps, false);
534534
break;
535535
case 'textarea':
536536
// TODO: Make sure we check if this is still unmounted or do any clean
@@ -1077,7 +1077,7 @@ export function diffHydratedProperties(
10771077
// TODO: Make sure we check if this is still unmounted or do any clean
10781078
// up necessary since we never stop tracking anymore.
10791079
inputValueTracking.track((domElement: any));
1080-
ReactDOMFiberInput.postMountWrapper(domElement, rawProps);
1080+
ReactDOMFiberInput.postMountWrapper(domElement, rawProps, true);
10811081
break;
10821082
case 'textarea':
10831083
// TODO: Make sure we check if this is still unmounted or do any clean

Diff for: packages/react-dom/src/client/ReactDOMFiberInput.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ export function updateWrapper(element: Element, props: Object) {
205205
}
206206
}
207207

208-
export function postMountWrapper(element: Element, props: Object) {
208+
export function postMountWrapper(
209+
element: Element,
210+
props: Object,
211+
isHydrating: boolean,
212+
) {
209213
const node = ((element: any): InputWithWrapperState);
210214

211215
if (props.hasOwnProperty('value') || props.hasOwnProperty('defaultValue')) {
@@ -214,7 +218,7 @@ export function postMountWrapper(element: Element, props: Object) {
214218

215219
// Do not assign value if it is already set. This prevents user text input
216220
// from being lost during SSR hydration.
217-
if (currentValue === '') {
221+
if (!isHydrating) {
218222
// Do not re-assign the value property if there is no change. This
219223
// potentially avoids a DOM write and prevents Firefox (~60.0.1) from
220224
// prematurely marking required inputs as invalid

0 commit comments

Comments
 (0)