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

Match select value against primitives to string but not undefined #24077

Merged
merged 1 commit into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,34 @@ describe('ReactDOMServerIntegrationSelect', () => {
expect(option.selected).toBe(true);
},
);

itRenders(
'a boolean true select value match the string "true"',
async render => {
const e = await render(
<select value={true} readOnly={true}>
<option value="first">First</option>
<option value="true">True</option>
</select>,
1,
);
expect(e.firstChild.selected).toBe(false);
expect(e.lastChild.selected).toBe(true);
},
);

itRenders(
'a missing select value does not match the string "undefined"',
async render => {
const e = await render(
<select readOnly={true}>
<option value="first">First</option>
<option value="undefined">Undefined</option>
</select>,
1,
);
expect(e.firstChild.selected).toBe(true);
expect(e.lastChild.selected).toBe(false);
},
);
});
11 changes: 8 additions & 3 deletions packages/react-dom/src/server/ReactDOMServerFormatConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ function pushStartOption(
}
}

if (selectedValue !== null) {
if (selectedValue != null) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The type of selectedValue is a lie. We should probably toString it and resolve the undefined earlier.

let stringValue;
if (value !== null) {
if (__DEV__) {
Expand Down Expand Up @@ -782,8 +782,13 @@ function pushStartOption(
break;
}
}
} else if (selectedValue === stringValue) {
target.push(selectedMarkerAttribute);
} else {
if (__DEV__) {
checkAttributeStringCoercion(selectedValue, 'select.value');
}
if ('' + selectedValue === stringValue) {
target.push(selectedMarkerAttribute);
}
}
} else if (selected) {
target.push(selectedMarkerAttribute);
Expand Down