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
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@
@for (int i = 0; i < (MaxChips == 0 ? _valuesState.Value?.Count ?? 0 : (MaxChips < (_valuesState.Value?.Count ?? 0) ? MaxChips : _valuesState.Value?.Count ?? 0)); i++)
{
int a = i;
<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" />
<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()" />
}
</MudChipSet>
@if (_valuesState.Value != null && MaxChips != 0 && MaxChips < _valuesState.Value.Count)
{
<MudChip T="T" Ripple="false" Text="@($"+{_valuesState.Value.Count - MaxChips}")" Color="@ChipColor" Variant="@ChipVariant" Size="@ChipSize" Disabled="@Disabled" />
<MudChip T="T" Ripple="false" Text="@($"+{_valuesState.Value.Count - MaxChips}")" Color="@ChipColor" Variant="@ChipVariant" Size="@ChipSize" Disabled="@GetDisabledState()" />
}
</DataVisualiser>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@
/// <returns></returns>
protected internal async Task HandleBeforeInput(BeforeInputEventArgs args)
{
if (GetDisabledState() || GetReadOnlyState())
return;

if (args.IsComposing)
{
return;
Expand Down Expand Up @@ -222,9 +225,9 @@

foreach (var part in parts)
{
if (AllowSameValues || !_valuesState.Value.Contains(part))

Check warning on line 228 in src/CodeBeam.MudBlazor.Extensions/Components/ChipField/MudChipField.razor.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
_valuesState.Value.Add(part);

Check warning on line 230 in src/CodeBeam.MudBlazor.Extensions/Components/ChipField/MudChipField.razor.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}
}

Expand All @@ -247,7 +250,7 @@
{
await _valuesState.SetValueAsync(new List<string>());
}
_valuesState.Value.Add(ConvertSet(_internalValue) ?? "");

Check warning on line 253 in src/CodeBeam.MudBlazor.Extensions/Components/ChipField/MudChipField.razor.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
await ValuesChanged.InvokeAsync(_valuesState.Value);
if (RuntimeLocation.IsServerSide)
{
Expand All @@ -273,7 +276,7 @@
/// <returns></returns>
public async Task Closed(MudChip<T> chip)
{
if (Disabled || ReadOnly)
if (GetDisabledState() || GetReadOnlyState())
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@namespace MudExtensions.UnitTests.TestComponents

<MudSwitch T="bool" @bind-Value="FormDisabled" Label="Form Disabled" Color="Color.Primary" />
<MudSwitch T="bool" @bind-Value="FormReadOnly" Label="Form ReadOnly" Color="Color.Primary" />

<MudForm Disabled="@FormDisabled" ReadOnly="@FormReadOnly">
<MudChipField T="string" @bind-Values="Values" Label="Chip Field" Variant="Variant.Outlined" />
</MudForm>

@code {
public static string __description__ = "Chips should respect the parent form's Disabled and ReadOnly state";

[Parameter]
public bool FormDisabled { get; set; }

[Parameter]
public bool FormReadOnly { get; set; }

[Parameter]
public List<string>? Values { get; set; } = new() { "first", "second" };
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Bunit;
using AwesomeAssertions;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using MudBlazor.Extensions;
using MudExtensions.UnitTests.TestComponents;

namespace MudExtensions.UnitTests.Components
{
Expand All @@ -21,5 +23,36 @@ public async Task ChipFieldBasicTest()
comp.Instance.GetState(x => x.Values).Should().BeEquivalentTo(new List<string> { "asdf", "asd", "sdfg" });
comp.Instance.GetState(x => x.Value).Should().BeEquivalentTo(null);
}

[Test]
public async Task ChipField_ParentFormDisabled_ShouldDisableChipsAndPreventRemoval()
{
var comp = Context.Render<ChipFieldFormStateTest>(opt =>
{
opt.Add(a => a.FormDisabled, true);
});
var chipField = comp.FindComponent<MudChipField<string>>();
var chips = comp.FindComponents<MudChip<string>>();

chips.Should().HaveCount(2);
chips.Should().AllSatisfy(chip => chip.Instance.Disabled.Should().BeTrue());

await comp.InvokeAsync(() => chipField.Instance.Closed(chips[0].Instance));
chipField.Instance.GetState(x => x.Values).Should().BeEquivalentTo(new List<string> { "first", "second" });
}

[Test]
public async Task ChipField_ParentFormReadOnly_ShouldPreventRemoval()
{
var comp = Context.Render<ChipFieldFormStateTest>(opt =>
{
opt.Add(a => a.FormReadOnly, true);
});
var chipField = comp.FindComponent<MudChipField<string>>();
var chips = comp.FindComponents<MudChip<string>>();

await comp.InvokeAsync(() => chipField.Instance.Closed(chips[0].Instance));
chipField.Instance.GetState(x => x.Values).Should().BeEquivalentTo(new List<string> { "first", "second" });
}
}
}
Loading