Skip to content

Fix checkbox DOM sync #50991

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

Merged
merged 3 commits into from
Sep 28, 2023
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
2 changes: 1 addition & 1 deletion src/Components/Web.JS/dist/Release/blazor.server.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Components/Web.JS/dist/Release/blazor.web.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/Components/Web.JS/src/Rendering/DomMerging/DomSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,10 @@ function ensureEditableValueSynchronized(destination: Element, value: any) {
} else if (destination instanceof HTMLSelectElement && destination.selectedIndex !== value) {
destination.selectedIndex = value as number;
} else if (destination instanceof HTMLInputElement) {
if (destination.type === 'checkbox' && destination.checked !== value) {
destination.checked = value as boolean;
if (destination.type === 'checkbox') {
if (destination.checked !== value) {
destination.checked = value as boolean;
}
} else if (destination.value !== value) {
destination.value = value as string;
}
Expand Down
16 changes: 16 additions & 0 deletions src/Components/Web.JS/test/DomSync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,22 @@ describe('DomSync', () => {
expect(selectElem.selectedIndex).toBe(2);
});

test('should handle checkboxes with value attribute', () => {
// Checkboxes require even more special-case handling because their 'value' attribute
// has to be handled as a regular attribute, and 'checked' must be handled similarly
// to 'value' on other inputs

const destination = makeExistingContent(`<input type='checkbox' value='first' checked />`);
const newContent = makeNewContent(`<input type='checkbox' value='second' checked />`);

const checkboxElem = destination.startExclusive.nextSibling as HTMLInputElement;

// Act/Assert
synchronizeDomContent(destination, newContent);
expect(checkboxElem.checked).toBeTruthy();
expect(checkboxElem.value).toBe('second');
});

test('should treat doctype nodes as unchanged', () => {
// Can't update a doctype after the document is created, nor is there a use case for doing so
// We just have to skip them, as it would be an error to try removing or inserting them
Expand Down