Skip to content

Commit 4c1262d

Browse files
Fix radio button reset after submitting enhanced form (#51796)
* fix radio button reset when submitting enhanced form * rename test * new fix * Update src/Components/Web.JS/test/DomSync.test.ts Co-authored-by: Steve Sanderson <[email protected]> * Update src/Components/Web.JS/src/Rendering/DomMerging/DomSync.ts Co-authored-by: Steve Sanderson <[email protected]> --------- Co-authored-by: Steve Sanderson <[email protected]>
1 parent 8642004 commit 4c1262d

File tree

7 files changed

+49
-5
lines changed

7 files changed

+49
-5
lines changed

src/Components/Web.JS/dist/Release/blazor.server.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/dist/Release/blazor.web.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/dist/Release/blazor.webview.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/src/Rendering/DomMerging/DomSync.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ function ensureEditableValueSynchronized(destination: Element, value: any) {
354354
} else if (destination instanceof HTMLSelectElement && destination.selectedIndex !== value) {
355355
destination.selectedIndex = value as number;
356356
} else if (destination instanceof HTMLInputElement) {
357-
if (destination.type === 'checkbox') {
357+
if (destination.type === 'checkbox' || destination.type === 'radio') {
358358
if (destination.checked !== value) {
359359
destination.checked = value as boolean;
360360
}
@@ -368,7 +368,7 @@ function getEditableElementValue(elem: Element): string | boolean | number | nul
368368
if (elem instanceof HTMLSelectElement) {
369369
return elem.selectedIndex;
370370
} else if (elem instanceof HTMLInputElement) {
371-
return elem.type === 'checkbox' ? elem.checked : (elem.getAttribute('value') || '');
371+
return elem.type === 'checkbox' || elem.type === 'radio' ? elem.checked : (elem.getAttribute('value') || '');
372372
} else if (elem instanceof HTMLTextAreaElement) {
373373
return elem.value;
374374
} else {

src/Components/Web.JS/test/DomSync.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,22 @@ describe('DomSync', () => {
485485
expect(checkboxElem.value).toBe('second');
486486
});
487487

488+
test('should handle radio buttons with value attribute', () => {
489+
// Radio buttons require even more special-case handling because their 'value' attribute
490+
// has to be handled as a regular attribute, and 'checked' must be handled similarly
491+
// to 'value' on other inputs
492+
493+
const destination = makeExistingContent(`<input type='radio' value='first' checked />`);
494+
const newContent = makeNewContent(`<input type='radio' value='second' checked />`);
495+
496+
const checkboxElem = destination.startExclusive.nextSibling as HTMLInputElement;
497+
498+
// Act/Assert
499+
synchronizeDomContent(destination, newContent);
500+
expect(checkboxElem.checked).toBeTruthy();
501+
expect(checkboxElem.value).toBe('second');
502+
});
503+
488504
test('should treat doctype nodes as unchanged', () => {
489505
// Can't update a doctype after the document is created, nor is there a use case for doing so
490506
// We just have to skip them, as it would be an error to try removing or inserting them

src/Components/test/E2ETest/ServerRenderingTests/FormHandlingTests/FormWithParentBindingContextTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,26 @@ void AssertUiState(string expectedStringValue, bool expectedBoolValue)
13371337
}
13381338
}
13391339

1340+
[Fact]
1341+
public void RadioButtonGetsResetAfterSubmittingEnhancedForm()
1342+
{
1343+
GoTo("forms/form-with-checkbox-and-radio-button");
1344+
1345+
Assert.False(Browser.Exists(By.Id("checkbox")).Selected);
1346+
Assert.False(Browser.Exists(By.Id("radio-button")).Selected);
1347+
1348+
Browser.Exists(By.Id("checkbox")).Click();
1349+
Browser.Exists(By.Id("radio-button")).Click();
1350+
1351+
Assert.True(Browser.Exists(By.Id("checkbox")).Selected);
1352+
Assert.True(Browser.Exists(By.Id("radio-button")).Selected);
1353+
1354+
Browser.Exists(By.Id("submit-button")).Click();
1355+
1356+
Assert.False(Browser.Exists(By.Id("checkbox")).Selected);
1357+
Assert.False(Browser.Exists(By.Id("radio-button")).Selected);
1358+
}
1359+
13401360
// Can't just use GetAttribute or GetDomAttribute because they both auto-resolve it
13411361
// to an absolute URL. We want to be able to assert about the attribute's literal value.
13421362
private string ReadFormActionAttribute(IWebElement form)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@page "/forms/form-with-checkbox-and-radio-button"
2+
<h1>Form With Radio Button</h1>
3+
4+
<form data-enhance>
5+
<input type="checkbox" id="checkbox" value="test-checkbox">
6+
<input type="radio" id="radio-button" value="test-radio">
7+
<button type="submit" id="submit-button">Submit</button>
8+
</form>

0 commit comments

Comments
 (0)