Skip to content

Fix radio button reset after submitting enhanced form #51796

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 5 commits into from
Nov 1, 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.

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

Large diffs are not rendered by default.

4 changes: 2 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,7 +354,7 @@ 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') {
if (destination.type === 'checkbox' || destination.type === 'radio') {
if (destination.checked !== value) {
destination.checked = value as boolean;
}
Expand All @@ -368,7 +368,7 @@ function getEditableElementValue(elem: Element): string | boolean | number | nul
if (elem instanceof HTMLSelectElement) {
return elem.selectedIndex;
} else if (elem instanceof HTMLInputElement) {
return elem.type === 'checkbox' ? elem.checked : (elem.getAttribute('value') || '');
return elem.type === 'checkbox' || elem.type === 'radio' ? elem.checked : (elem.getAttribute('value') || '');
} else if (elem instanceof HTMLTextAreaElement) {
return elem.value;
} else {
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 @@ -485,6 +485,22 @@ describe('DomSync', () => {
expect(checkboxElem.value).toBe('second');
});

test('should handle radio buttons with value attribute', () => {
// Radio buttons 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='radio' value='first' checked />`);
const newContent = makeNewContent(`<input type='radio' 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
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,26 @@ void AssertUiState(string expectedStringValue, bool expectedBoolValue)
}
}

[Fact]
public void RadioButtonGetsResetAfterSubmittingEnhancedForm()
{
GoTo("forms/form-with-checkbox-and-radio-button");

Assert.False(Browser.Exists(By.Id("checkbox")).Selected);
Assert.False(Browser.Exists(By.Id("radio-button")).Selected);

Browser.Exists(By.Id("checkbox")).Click();
Browser.Exists(By.Id("radio-button")).Click();

Assert.True(Browser.Exists(By.Id("checkbox")).Selected);
Assert.True(Browser.Exists(By.Id("radio-button")).Selected);

Browser.Exists(By.Id("submit-button")).Click();

Assert.False(Browser.Exists(By.Id("checkbox")).Selected);
Assert.False(Browser.Exists(By.Id("radio-button")).Selected);
}

// Can't just use GetAttribute or GetDomAttribute because they both auto-resolve it
// to an absolute URL. We want to be able to assert about the attribute's literal value.
private string ReadFormActionAttribute(IWebElement form)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@page "/forms/form-with-checkbox-and-radio-button"
<h1>Form With Radio Button</h1>

<form data-enhance>
<input type="checkbox" id="checkbox" value="test-checkbox">
<input type="radio" id="radio-button" value="test-radio">
<button type="submit" id="submit-button">Submit</button>
</form>