Skip to content
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
22 changes: 12 additions & 10 deletions src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,23 @@ private async Task<bool> InternalStateChanged(CheckboxState state)
if (IsBoolean)
{
CurrentValue = (TValue)(object)(state == CheckboxState.Checked);
}

if (State != state)
{
State = state;
if (StateChanged.HasDelegate)
if (ValueChanged.HasDelegate)
{
await StateChanged.InvokeAsync(State);
ret = false;
}
}

if (OnStateChanged != null)
{
await OnStateChanged(State, Value);
}
State = state;
if (StateChanged.HasDelegate)
{
await StateChanged.InvokeAsync(State);
ret = false;
}

if (OnStateChanged != null)
{
await OnStateChanged(State, Value);
}
return ret;
}
Expand Down
21 changes: 21 additions & 0 deletions test/UnitTest/Components/CheckboxListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ public async Task Checkbox_OnTriggerClickAsync()
Assert.Equal(CheckboxState.Checked, cut.Instance.State);
}

[Fact]
public async Task Bool_TriggerStateChanged_Ok()
{
bool value = false;
// 测试 bool 值改变值时触发 StateChanged 回调方法
var cut = Context.RenderComponent<Checkbox<bool>>(pb =>
{
pb.Add(a => a.Value, false);
pb.Add(a => a.OnStateChanged, (state, v) =>
{
value = v;
return Task.CompletedTask;
});
});

// JavaScript 调用 OnTriggerClickAsync 方法
await cut.InvokeAsync(() => cut.Instance.OnTriggerClickAsync());
Assert.Equal(CheckboxState.Checked, cut.Instance.State);
Assert.True(value);
}

[Fact]
public void Checkbox_Dispose()
{
Expand Down