Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the correct initial value on input range #12939

Merged
merged 4 commits into from May 31, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/react-dom/src/client/ReactDOMFiberInput.js
Expand Up @@ -210,7 +210,11 @@ export function postMountWrapper(element: Element, props: Object) {

if (props.hasOwnProperty('value') || props.hasOwnProperty('defaultValue')) {
const initialValue = '' + node._wrapperState.initialValue;
const currentValue = node.value;

// With range inputs node.value may be a default value calculated from the
// min/max attributes. This ensures that node.value is set with the correct
// value coming from props.
const currentValue = props.type === 'range' ? '' : node.value;

// Do not assign value if it is already set. This prevents user text input
// from being lost during SSR hydration.
Expand All @@ -220,6 +224,8 @@ export function postMountWrapper(element: Element, props: Object) {
// prematurely marking required inputs as invalid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woah nice. You added the isHydrating part! Very cool. I have a few questions:

  1. Can you use isHydrating as the condition on line 129, like if (!isHydrating)?
  2. Are the lines between (218-225) necessary? I think the bug was that range inputs never reported "", as required by the old code on line 229, that prevented the value assignment from happening.

Basically, I think the only change we need to make is on line 229:

if (!isHydrating) {
  // ....
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you're absolutely right! I'll update it right away.

if (initialValue !== currentValue) {
node.value = initialValue;
} else if (props.type === 'range') {
node.value = initialValue;
}
}

Expand Down