Skip to content

Commit 8be87d4

Browse files
redgp0gclaudemckaragoz
authored
ChipField: Respect parent form Disabled and ReadOnly state (#653)
* ChipField: Respect parent form Disabled and ReadOnly state Chips used the component's own Disabled parameter, and Closed() checked Disabled/ReadOnly directly, so chips stayed enabled and removable inside a disabled or read-only MudForm. Use GetDisabledState() and GetReadOnlyState() so the cascaded form state is honored. Fixes #270 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add Defensive Checks --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Mehmet Can Karagöz <m.cankaragoz@outlook.com>
1 parent 6167c24 commit 8be87d4

4 files changed

Lines changed: 60 additions & 3 deletions

File tree

‎src/CodeBeam.MudBlazor.Extensions/Components/ChipField/MudChipField.razor‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@
5050
@for (int i = 0; i < (MaxChips == 0 ? _valuesState.Value?.Count ?? 0 : (MaxChips < (_valuesState.Value?.Count ?? 0) ? MaxChips : _valuesState.Value?.Count ?? 0)); i++)
5151
{
5252
int a = i;
53-
<MudChip T="T" Class="@ClassChip" Style="@StyleChip" Ripple="false" Text="@(_valuesState.Value != null ? _valuesState.Value[a] : string.Empty)" Color="@ChipColor" Variant="@ChipVariant" Size="@ChipSize" Disabled="@Disabled" />
53+
<MudChip T="T" Class="@ClassChip" Style="@StyleChip" Ripple="false" Text="@(_valuesState.Value != null ? _valuesState.Value[a] : string.Empty)" Color="@ChipColor" Variant="@ChipVariant" Size="@ChipSize" Disabled="@GetDisabledState()" />
5454
}
5555
</MudChipSet>
5656
@if (_valuesState.Value != null && MaxChips != 0 && MaxChips < _valuesState.Value.Count)
5757
{
58-
<MudChip T="T" Ripple="false" Text="@($"+{_valuesState.Value.Count - MaxChips}")" Color="@ChipColor" Variant="@ChipVariant" Size="@ChipSize" Disabled="@Disabled" />
58+
<MudChip T="T" Ripple="false" Text="@($"+{_valuesState.Value.Count - MaxChips}")" Color="@ChipColor" Variant="@ChipVariant" Size="@ChipSize" Disabled="@GetDisabledState()" />
5959
}
6060
</DataVisualiser>
6161

‎src/CodeBeam.MudBlazor.Extensions/Components/ChipField/MudChipField.razor.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ protected Task HandleKeyUp(KeyboardEventArgs args)
178178
/// <returns></returns>
179179
protected internal async Task HandleBeforeInput(BeforeInputEventArgs args)
180180
{
181+
if (GetDisabledState() || GetReadOnlyState())
182+
return;
183+
181184
if (args.IsComposing)
182185
{
183186
return;
@@ -273,7 +276,7 @@ protected async Task SetChips()
273276
/// <returns></returns>
274277
public async Task Closed(MudChip<T> chip)
275278
{
276-
if (Disabled || ReadOnly)
279+
if (GetDisabledState() || GetReadOnlyState())
277280
{
278281
return;
279282
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@namespace MudExtensions.UnitTests.TestComponents
2+
3+
<MudSwitch T="bool" @bind-Value="FormDisabled" Label="Form Disabled" Color="Color.Primary" />
4+
<MudSwitch T="bool" @bind-Value="FormReadOnly" Label="Form ReadOnly" Color="Color.Primary" />
5+
6+
<MudForm Disabled="@FormDisabled" ReadOnly="@FormReadOnly">
7+
<MudChipField T="string" @bind-Values="Values" Label="Chip Field" Variant="Variant.Outlined" />
8+
</MudForm>
9+
10+
@code {
11+
public static string __description__ = "Chips should respect the parent form's Disabled and ReadOnly state";
12+
13+
[Parameter]
14+
public bool FormDisabled { get; set; }
15+
16+
[Parameter]
17+
public bool FormReadOnly { get; set; }
18+
19+
[Parameter]
20+
public List<string>? Values { get; set; } = new() { "first", "second" };
21+
}

‎tests/CodeBeam.MudBlazor.Extensions.UnitTests/Components/ChipFieldTests.cs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Bunit;
22
using AwesomeAssertions;
33
using Microsoft.AspNetCore.Components;
4+
using MudBlazor;
45
using MudBlazor.Extensions;
6+
using MudExtensions.UnitTests.TestComponents;
57

68
namespace MudExtensions.UnitTests.Components
79
{
@@ -21,5 +23,36 @@ public async Task ChipFieldBasicTest()
2123
comp.Instance.GetState(x => x.Values).Should().BeEquivalentTo(new List<string> { "asdf", "asd", "sdfg" });
2224
comp.Instance.GetState(x => x.Value).Should().BeEquivalentTo(null);
2325
}
26+
27+
[Test]
28+
public async Task ChipField_ParentFormDisabled_ShouldDisableChipsAndPreventRemoval()
29+
{
30+
var comp = Context.Render<ChipFieldFormStateTest>(opt =>
31+
{
32+
opt.Add(a => a.FormDisabled, true);
33+
});
34+
var chipField = comp.FindComponent<MudChipField<string>>();
35+
var chips = comp.FindComponents<MudChip<string>>();
36+
37+
chips.Should().HaveCount(2);
38+
chips.Should().AllSatisfy(chip => chip.Instance.Disabled.Should().BeTrue());
39+
40+
await comp.InvokeAsync(() => chipField.Instance.Closed(chips[0].Instance));
41+
chipField.Instance.GetState(x => x.Values).Should().BeEquivalentTo(new List<string> { "first", "second" });
42+
}
43+
44+
[Test]
45+
public async Task ChipField_ParentFormReadOnly_ShouldPreventRemoval()
46+
{
47+
var comp = Context.Render<ChipFieldFormStateTest>(opt =>
48+
{
49+
opt.Add(a => a.FormReadOnly, true);
50+
});
51+
var chipField = comp.FindComponent<MudChipField<string>>();
52+
var chips = comp.FindComponents<MudChip<string>>();
53+
54+
await comp.InvokeAsync(() => chipField.Instance.Closed(chips[0].Instance));
55+
chipField.Instance.GetState(x => x.Values).Should().BeEquivalentTo(new List<string> { "first", "second" });
56+
}
2457
}
2558
}

0 commit comments

Comments
 (0)