Skip to content

Fix binding <select> to a null value #23221

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 2 commits into from
Jun 22, 2020
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.webassembly.js

Large diffs are not rendered by default.

24 changes: 20 additions & 4 deletions src/Components/Web.JS/src/Rendering/BrowserRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ export class BrowserRenderer {
// added as an opaque markup block rather than individually
// Right here we implement [2]
if (newDomElementRaw instanceof HTMLSelectElement && selectValuePropname in newDomElementRaw) {
const selectValue = newDomElementRaw[selectValuePropname];
newDomElementRaw.value = selectValue;
const selectValue: string | null = newDomElementRaw[selectValuePropname];
setSelectElementValue(newDomElementRaw, selectValue);
}
}

Expand Down Expand Up @@ -357,16 +357,20 @@ export class BrowserRenderer {
case 'SELECT':
case 'TEXTAREA': {
const value = attributeFrame ? frameReader.attributeValue(attributeFrame) : null;
(element as any).value = value;

if (element.tagName === 'SELECT') {
if (element instanceof HTMLSelectElement) {
setSelectElementValue(element, value);

// <select> is special, in that anything we write to .value will be lost if there
// isn't yet a matching <option>. To maintain the expected behavior no matter the
// element insertion/update order, preserve the desired value separately so
// we can recover it when inserting any matching <option> or after inserting an
// entire markup block of descendants.
element[selectValuePropname] = value;
} else {
(element as any).value = value;
}

return true;
}
case 'OPTION': {
Expand Down Expand Up @@ -519,3 +523,15 @@ function stripOnPrefix(attributeName: string) {

throw new Error(`Attribute should be an event name, but doesn't start with 'on'. Value: '${attributeName}'`);
}

function setSelectElementValue(element: HTMLSelectElement, value: string | null) {
// There's no sensible way to represent a select option with value 'null', because
// (1) HTML attributes can't have null values - the closest equivalent is absence of the attribute
// (2) When picking an <option> with no 'value' attribute, the browser treats the value as being the
// *text content* on that <option> element. Trying to suppress that default behavior would involve
// a long chain of special-case hacks, as well as being breaking vs 3.x.
// So, the most plausible 'null' equivalent is an empty string. It's unfortunate that people can't
// write <option value=@someNullVariable>, and that we can never distinguish between null and empty
// string in a bound <select>, but that's a limit in the representational power of HTML.
element.value = value || '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@guardrex guardrex Jun 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it more general tho than just forms validation? Seems like a binding topic subject that we could cross-link to from the forms validation topic.

Copy link
Contributor

@guardrex guardrex Jun 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well ... we already have radio buttons covered there, so I suppose that's fine. I'll look at cross-linking this the other way. I'll cross-link from the binding topic to the forms validation topic for the new section the same way that we do for radio button coverage.

}
2 changes: 2 additions & 0 deletions src/Components/test/E2ETest/Tests/BindTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public void CanBindSelect()
// https://github.com/dotnet/aspnetcore/issues/17735
target.SelectByText("Empty value");
Browser.Equal(string.Empty, () => boundValue.Text);
Browser.Equal("Empty value", () => target.SelectedOption.Text);
}

[Fact]
Expand All @@ -237,6 +238,7 @@ public void CanBindSelectToMarkup()
// https://github.com/dotnet/aspnetcore/issues/17735
target.SelectByText("Empty value");
Browser.Equal(string.Empty, () => boundValue.Text);
Browser.Equal("Empty value", () => target.SelectedOption.Text);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@
<select id="select-box" @bind="selectValue">
<optgroup label="Some choices">
<!-- Show it also works with optgroup -->
<option value="">Empty value</option>
<option value=@string.Empty>Empty value</option>
<option [email protected]>First choice</option>
<option [email protected]>Second choice</option>
<option [email protected]>Third choice</option>
Expand Down