Skip to content

Commit

Permalink
Let value override defaultValue if both are specified (#21369)
Browse files Browse the repository at this point in the history
There's a DEV warning for this case but we still test for the production
behavior.
  • Loading branch information
sebmarkbage authored Apr 27, 2021
1 parent 29faeb2 commit 2182563
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions packages/react-dom/src/server/ReactDOMServerFormatConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,11 @@ function pushInput(

target.push(startChunkForTag('input'));

let value = null;
let defaultValue = null;
let checked = null;
let defaultChecked = null;

for (const propKey in props) {
if (hasOwnProperty.call(props, propKey)) {
const propValue = props[propKey];
Expand All @@ -827,21 +832,35 @@ function pushInput(
);
// eslint-disable-next-line-no-fallthrough
case 'defaultChecked':
// Previously "checked" would win but now it's enumeration order dependent.
// There's a warning in either case.
pushAttribute(target, responseState, 'checked', propValue);
defaultChecked = propValue;
break;
case 'defaultValue':
// Previously "value" would win but now it's enumeration order dependent.
// There's a warning in either case.
pushAttribute(target, responseState, 'value', propValue);
defaultValue = propValue;
break;
case 'checked':
checked = propValue;
break;
case 'value':
value = propValue;
break;
default:
pushAttribute(target, responseState, propKey, propValue);
break;
}
}
}

if (checked !== null) {
pushAttribute(target, responseState, 'checked', checked);
} else if (defaultChecked !== null) {
pushAttribute(target, responseState, 'checked', defaultChecked);
}
if (value !== null) {
pushAttribute(target, responseState, 'value', value);
} else if (defaultValue !== null) {
pushAttribute(target, responseState, 'value', defaultValue);
}

if (assignID !== null) {
pushID(target, responseState, assignID, props.id);
}
Expand Down Expand Up @@ -877,6 +896,7 @@ function pushStartTextArea(
target.push(startChunkForTag('textarea'));

let value = null;
let defaultValue = null;
let children = null;
for (const propKey in props) {
if (hasOwnProperty.call(props, propKey)) {
Expand All @@ -889,11 +909,11 @@ function pushStartTextArea(
children = propValue;
break;
case 'value':
case 'defaultValue':
// Previously "checked" would win but now it's enumeration order dependent.
// There's a warning in either case.
value = propValue;
break;
case 'defaultValue':
defaultValue = propValue;
break;
case 'dangerouslySetInnerHTML':
invariant(
false,
Expand All @@ -906,6 +926,10 @@ function pushStartTextArea(
}
}
}
if (value === null && defaultValue !== null) {
value = defaultValue;
}

if (assignID !== null) {
pushID(target, responseState, assignID, props.id);
}
Expand Down

0 comments on commit 2182563

Please sign in to comment.