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

respect default value #4341

Merged
merged 7 commits into from
Apr 18, 2024
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
6 changes: 6 additions & 0 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,12 @@ function diffElementNodes(
} else if (i == 'dangerouslySetInnerHTML') {
oldHtml = value;
} else if (i !== 'key' && !(i in newProps)) {
if (
(i == 'value' && 'defaultValue' in newProps) ||
(i == 'checked' && 'defaultChecked' in newProps)
) {
continue;
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
}
setProperty(dom, i, null, value, isSvg);
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/browser/hydrate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ describe('hydrate()', () => {
teardown(scratch);
});

// Test for preactjs/preact#4340
it('should respect defaultValue in hydrate', () => {
scratch.innerHTML = '<input value="foo">';
hydrate(<input defaultValue="foo" />, scratch);
expect(scratch.firstChild.value).to.equal('foo');
});
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved

it('should respect defaultChecked in hydrate', () => {
scratch.innerHTML = '<input checked="true">';
hydrate(<input defaultChecked />, scratch);
expect(scratch.firstChild.checked).to.equal(true);
});

it('should reuse existing DOM', () => {
const onClickSpy = sinon.spy();
const html = ul([li('1'), li('2'), li('3')]);
Expand Down
29 changes: 29 additions & 0 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,35 @@ describe('render()', () => {
expect(scratch.firstChild.spellcheck).to.equal(false);
});

// Test for preactjs/preact#4340
it('should respect defaultValue in render', () => {
scratch.innerHTML = '<input value="foo">';
render(<input defaultValue="foo" />, scratch);
expect(scratch.firstChild.value).to.equal('foo');
});

it('should support subsequent renders w/ defaultValue', () => {
scratch.innerHTML = '<input value="foo">';
render(<input defaultValue="foo" value="bar" />, scratch);
expect(scratch.firstChild.value).to.equal('bar');
render(<input defaultValue="foo" value="baz" />, scratch);
expect(scratch.firstChild.value).to.equal('baz');
});

it('should respect defaultChecked in render', () => {
scratch.innerHTML = '<input checked="true">';
render(<input defaultChecked />, scratch);
expect(scratch.firstChild.checked).to.equal(true);
});

it('should support subsequent renders w/ defaultChecked', () => {
scratch.innerHTML = '<input checked="true">';
render(<input defaultChecked checked />, scratch);
expect(scratch.firstChild.checked).to.equal(true);
render(<input defaultChecked checked={false} />, scratch);
expect(scratch.firstChild.checked).to.equal(false);
});

it('should render download attribute', () => {
render(<a download="" />, scratch);
expect(scratch.firstChild.getAttribute('download')).to.equal('');
Expand Down
Loading