Skip to content

Commit 110e41f

Browse files
Fix InputCheckbox when initially checked. Fixes #8502
1 parent d62d33c commit 110e41f

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/Components/Components/src/Forms/InputComponents/InputCheckbox.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
2727
builder.AddAttribute(1, "type", "checkbox");
2828
builder.AddAttribute(2, "id", Id);
2929
builder.AddAttribute(3, "class", CssClass);
30-
builder.AddAttribute(4, "value", BindMethods.GetValue(CurrentValue));
30+
builder.AddAttribute(4, "checked", BindMethods.GetValue(CurrentValue));
3131
builder.AddAttribute(5, "onchange", BindMethods.SetValueHandler(__value => CurrentValue = __value, CurrentValue));
3232
builder.CloseElement();
3333
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,27 @@ public void InputCheckboxInteractsWithEditContext()
244244
{
245245
var appElement = MountTestComponent<TypicalValidationComponent>();
246246
var acceptsTermsInput = appElement.FindElement(By.ClassName("accepts-terms")).FindElement(By.TagName("input"));
247+
var isEvilInput = appElement.FindElement(By.ClassName("is-evil")).FindElement(By.TagName("input"));
247248
var messagesAccessor = CreateValidationMessagesAccessor(appElement);
248249

250+
// Correct initial checkedness
251+
Assert.False(acceptsTermsInput.Selected);
252+
Assert.True(isEvilInput.Selected);
253+
249254
// Validates on edit
250255
Browser.Equal("valid", () => acceptsTermsInput.GetAttribute("class"));
256+
Browser.Equal("valid", () => isEvilInput.GetAttribute("class"));
251257
acceptsTermsInput.Click();
258+
isEvilInput.Click();
252259
Browser.Equal("modified valid", () => acceptsTermsInput.GetAttribute("class"));
260+
Browser.Equal("modified valid", () => isEvilInput.GetAttribute("class"));
253261

254262
// Can become invalid
255263
acceptsTermsInput.Click();
264+
isEvilInput.Click();
256265
Browser.Equal("modified invalid", () => acceptsTermsInput.GetAttribute("class"));
257-
Browser.Equal(new[] { "Must accept terms" }, messagesAccessor);
266+
Browser.Equal("modified invalid", () => isEvilInput.GetAttribute("class"));
267+
Browser.Equal(new[] { "Must accept terms", "Must not be evil" }, messagesAccessor);
258268
}
259269

260270
[Fact]

src/Components/test/testassets/BasicTestApp/FormsTest/TypicalValidationComponent.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
<p class="accepts-terms">
4040
Accepts terms: <InputCheckbox bind-Value="@person.AcceptsTerms" />
4141
</p>
42+
<p class="is-evil">
43+
Is evil: <InputCheckbox bind-Value="@person.IsEvil" />
44+
</p>
4245

4346
<button type="submit">Submit</button>
4447

@@ -72,6 +75,9 @@
7275
[Required, Range(typeof(bool), "true", "true", ErrorMessage = "Must accept terms")]
7376
public bool AcceptsTerms { get; set; }
7477

78+
[Required, Range(typeof(bool), "false", "false", ErrorMessage = "Must not be evil")]
79+
public bool IsEvil { get; set; } = true;
80+
7581
[Required, StringLength(20, ErrorMessage = "Description is max 20 chars")]
7682
public string Description { get; set; }
7783

0 commit comments

Comments
 (0)