Skip to content

Commit 962594b

Browse files
authored
respect default value (#4341)
* respect default value * Update src/diff/index.js * Update test/browser/render.test.js * comments * add test * add checked * feedback
1 parent f3edc90 commit 962594b

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/diff/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,12 @@ function diffElementNodes(
443443
} else if (i == 'dangerouslySetInnerHTML') {
444444
oldHtml = value;
445445
} else if (i !== 'key' && !(i in newProps)) {
446+
if (
447+
(i == 'value' && 'defaultValue' in newProps) ||
448+
(i == 'checked' && 'defaultChecked' in newProps)
449+
) {
450+
continue;
451+
}
446452
setProperty(dom, i, null, value, isSvg);
447453
}
448454
}

test/browser/hydrate.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ describe('hydrate()', () => {
5757
teardown(scratch);
5858
});
5959

60+
// Test for preactjs/preact#4340
61+
it('should respect defaultValue in hydrate', () => {
62+
scratch.innerHTML = '<input value="foo">';
63+
hydrate(<input defaultValue="foo" />, scratch);
64+
expect(scratch.firstChild.value).to.equal('foo');
65+
});
66+
67+
it('should respect defaultChecked in hydrate', () => {
68+
scratch.innerHTML = '<input checked="true">';
69+
hydrate(<input defaultChecked />, scratch);
70+
expect(scratch.firstChild.checked).to.equal(true);
71+
});
72+
6073
it('should reuse existing DOM', () => {
6174
const onClickSpy = sinon.spy();
6275
const html = ul([li('1'), li('2'), li('3')]);

test/browser/render.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,35 @@ describe('render()', () => {
472472
expect(scratch.firstChild.spellcheck).to.equal(false);
473473
});
474474

475+
// Test for preactjs/preact#4340
476+
it('should respect defaultValue in render', () => {
477+
scratch.innerHTML = '<input value="foo">';
478+
render(<input defaultValue="foo" />, scratch);
479+
expect(scratch.firstChild.value).to.equal('foo');
480+
});
481+
482+
it('should support subsequent renders w/ defaultValue', () => {
483+
scratch.innerHTML = '<input value="foo">';
484+
render(<input defaultValue="foo" value="bar" />, scratch);
485+
expect(scratch.firstChild.value).to.equal('bar');
486+
render(<input defaultValue="foo" value="baz" />, scratch);
487+
expect(scratch.firstChild.value).to.equal('baz');
488+
});
489+
490+
it('should respect defaultChecked in render', () => {
491+
scratch.innerHTML = '<input checked="true">';
492+
render(<input defaultChecked />, scratch);
493+
expect(scratch.firstChild.checked).to.equal(true);
494+
});
495+
496+
it('should support subsequent renders w/ defaultChecked', () => {
497+
scratch.innerHTML = '<input checked="true">';
498+
render(<input defaultChecked checked />, scratch);
499+
expect(scratch.firstChild.checked).to.equal(true);
500+
render(<input defaultChecked checked={false} />, scratch);
501+
expect(scratch.firstChild.checked).to.equal(false);
502+
});
503+
475504
it('should render download attribute', () => {
476505
render(<a download="" />, scratch);
477506
expect(scratch.firstChild.getAttribute('download')).to.equal('');

0 commit comments

Comments
 (0)