Skip to content

Commit 21fdbcd

Browse files
authored
Allow @bind-value to be specified on its own (#11401)
1 parent e8181ae commit 21fdbcd

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

src/Components/Components/ref/Microsoft.AspNetCore.Components.netstandard2.0.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ protected void NotifyAuthenticationStateChanged(System.Threading.Tasks.Task<Micr
2020
[Microsoft.AspNetCore.Components.BindElementAttribute("textarea", null, "value", "onchange")]
2121
[Microsoft.AspNetCore.Components.BindInputElementAttribute("checkbox", null, "checked", "onchange")]
2222
[Microsoft.AspNetCore.Components.BindInputElementAttribute("text", null, "value", "onchange")]
23+
[Microsoft.AspNetCore.Components.BindInputElementAttribute(null, "value", "value", "onchange")]
2324
[Microsoft.AspNetCore.Components.BindInputElementAttribute(null, null, "value", "onchange")]
2425
public static partial class BindAttributes
2526
{

src/Components/Components/src/BindAttributes.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ namespace Microsoft.AspNetCore.Components
1515
// when a specific type attribute is applied.
1616
[BindInputElement(null, null, "value", "onchange")]
1717

18+
// Handles cases like <input @bind-value="..." /> - this is a fallback and will be ignored
19+
// when a specific type attribute is applied.
20+
[BindInputElement(null, "value", "value", "onchange")]
21+
1822
[BindInputElement("checkbox", null, "checked", "onchange")]
1923
[BindInputElement("text", null, "value", "onchange")]
2024

src/Components/test/E2ETest/Tests/BindTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@ public void CanBindTextbox_InitiallyPopulated()
8181
Assert.Equal(string.Empty, mirrorValue.GetAttribute("value"));
8282
}
8383

84+
[Fact]
85+
public void CanBindTextbox_WithBindSuffixInitiallyPopulated()
86+
{
87+
var target = Browser.FindElement(By.Id("bind-with-suffix-textbox-initially-populated"));
88+
var boundValue = Browser.FindElement(By.Id("bind-with-suffix-textbox-initially-populated-value"));
89+
var mirrorValue = Browser.FindElement(By.Id("bind-with-suffix-textbox-initially-populated-mirror"));
90+
var setNullButton = Browser.FindElement(By.Id("bind-with-suffix-textbox-initially-populated-setnull"));
91+
Assert.Equal("Hello", target.GetAttribute("value"));
92+
Assert.Equal("Hello", boundValue.Text);
93+
Assert.Equal("Hello", mirrorValue.GetAttribute("value"));
94+
95+
// Modify target; verify value is updated and that textboxes linked to the same data are updated
96+
target.Clear();
97+
target.SendKeys("Changed value\t");
98+
Browser.Equal("Changed value", () => boundValue.Text);
99+
Assert.Equal("Changed value", mirrorValue.GetAttribute("value"));
100+
101+
// Remove the value altogether
102+
setNullButton.Click();
103+
Browser.Equal(string.Empty, () => target.GetAttribute("value"));
104+
Assert.Equal(string.Empty, boundValue.Text);
105+
Assert.Equal(string.Empty, mirrorValue.GetAttribute("value"));
106+
}
107+
84108
[Fact]
85109
public void CanBindTextArea_InitiallyBlank()
86110
{

src/Components/test/testassets/BasicTestApp/BindCasesComponent.razor

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
<input id="textbox-initially-populated-mirror" @bind="textboxInitiallyPopulatedValue" readonly />
1616
<button id="textbox-initially-populated-setnull" @onclick="@(() => { textboxInitiallyPopulatedValue = null; })">Set null</button>
1717
</p>
18+
<p>
19+
Bind with value-suffix, Initially populated:
20+
<input id="bind-with-suffix-textbox-initially-populated" @bind-value="textboxInitiallyPopulatedValue" />
21+
<span id="bind-with-suffix-textbox-initially-populated-value">@textboxInitiallyPopulatedValue</span>
22+
<input type="text" id="bind-with-suffix-textbox-initially-populated-mirror" @bind-value="textboxInitiallyPopulatedValue" readonly />
23+
<button id="bind-with-suffix-textbox-initially-populated-setnull" @onclick="@(() => { textboxInitiallyPopulatedValue = null; })">Set null</button>
24+
</p>
1825

1926
<h2>Numeric Textboxes</h2>
2027
<p>

0 commit comments

Comments
 (0)