Skip to content

Commit

Permalink
Merge pull request #2957 from preactjs/compat-default-value
Browse files Browse the repository at this point in the history
Compat: Fix defaultValue not applied when value==null/undefined
  • Loading branch information
marvinhagemeister authored Jan 26, 2021
2 parents 5044515 + e0842e8 commit d34b413
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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

0 comments on commit d34b413

Please sign in to comment.