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

Compat: Fix defaultValue not applied when value==null/undefined #2957

Merged
merged 2 commits into from
Jan 26, 2021
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
10 changes: 9 additions & 1 deletion compat/src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,15 @@ options.vnode = vnode => {
for (let i in props) {
let value = props[i];

if (i === 'defaultValue' && 'value' in props && props.value == null) {
if (i === 'value' && 'defaultValue' in props && value == null) {
// Skip applying value if it is null/undefined and we already set
// a default value
continue;
} else if (
i === 'defaultValue' &&
'value' in props &&
props.value == null
) {
// `defaultValue` is treated as a fallback `value` when a value prop is present but null/undefined.
// `defaultValue` for Elements with no value prop is the same as the DOM defaultValue property.
i = 'value';
Expand Down
8 changes: 8 additions & 0 deletions compat/test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ describe('compat render', () => {
expect(scratch.firstElementChild).to.have.property('value', 'foo');
});

it('should add defaultValue when value is null/undefined', () => {
render(<input defaultValue="foo" value={null} />, scratch);
expect(scratch.firstElementChild).to.have.property('value', 'foo');

render(<input defaultValue="foo" value={undefined} />, scratch);
expect(scratch.firstElementChild).to.have.property('value', 'foo');
});

it('should support defaultValue for select tag', () => {
function App() {
return (
Expand Down