Skip to content

Commit 1f2b359

Browse files
authored
ChipField Control Backspace Behavior (#443)
1 parent c8462d0 commit 1f2b359

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

CodeBeam.MudBlazor.Extensions.Docs/Pages/Components/ChipField/Examples/ChipFieldExample1.razor

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
<MudGrid>
44
<MudItem xs="12" sm="8" Class="d-flex flex-column gap-8 align-center justify-center">
5-
<MudChipField T="string" @bind-Value="_value" @bind-Values="_values" StyleChip="@(_disableRadius ? "border-radius: 0 !important" : null)" Delimiter="@_delimiter" FullWidth="true" Disabled="_disabled" ReadOnly="_readonly"
6-
ChipColor="_color" ChipVariant="_chipVariant" ChipSize="_chipSize" WrapChips="_wrapChips" MaxChips="_maxChips" ChipsMaxWidth="_chipsMaxWidth" Closeable="_closeable" Variant="_variant" Label="ChipField" AllowSameValues="@_allowSameValues" />
5+
<MudChipField @ref="_chipField" T="string" @bind-Value="_value" @bind-Values="_values" StyleChip="@(_disableRadius ? "border-radius: 0 !important" : null)" Delimiter="@_delimiter" FullWidth="true"
6+
Disabled="_disabled" ReadOnly="_readonly" ChipColor="_color" ChipVariant="_chipVariant" ChipSize="_chipSize" WrapChips="_wrapChips" BackspaceChipRemoval="@_backspaceChipRemoval"
7+
MaxChips="_maxChips" ChipsMaxWidth="_chipsMaxWidth" Closeable="_closeable" Variant="_variant" Label="ChipField" AllowSameValues="@_allowSameValues" />
78
</MudItem>
89

910
<MudItem xs="12" sm="4">
@@ -48,11 +49,14 @@
4849
<MudSwitchM3 @bind-Value="@_disableRadius" Label="Disable Border Radius" Color="Color.Secondary" />
4950
<MudSwitchM3 @bind-Value="@_closeable" Label="Closeable" Color="Color.Secondary" />
5051
<MudSwitchM3 @bind-Value="@_allowSameValues" Label="Allow Same Values" Color="Color.Secondary" />
52+
<MudSwitchM3 @bind-Value="@_backspaceChipRemoval" Label="Backspace Chip Removal" Color="Color.Secondary" />
53+
<MudButton Color="Color.Secondary" Variant="Variant. Outlined" OnClick="@(async() => await _chipField.ClearTextField())">Clear Text Field</MudButton>
5154
</MudStack>
5255
</MudItem>
5356
</MudGrid>
5457

5558
@code{
59+
MudChipField<string> _chipField = new();
5660
string _delimiter = " ";
5761
string? _value;
5862
List<string>? _values;
@@ -68,4 +72,5 @@
6872
bool _disableRadius;
6973
bool _closeable = true;
7074
bool _allowSameValues = false;
75+
bool _backspaceChipRemoval = true;
7176
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public partial class MudChipField<T> : MudTextFieldExtended<T>
9797
[Parameter]
9898
public bool Closeable { get; set; } = true;
9999

100+
/// <summary>
101+
/// Removes last created chip value when press Backspace. Default is true.
102+
/// </summary>
103+
[Parameter]
104+
public bool BackspaceChipRemoval { get; set; } = true;
105+
100106
/// <summary>
101107
/// Maximum chip count. Set 0 to unlimited. Default is 0.
102108
/// </summary>
@@ -119,7 +125,7 @@ protected async Task HandleKeyDown(KeyboardEventArgs args)
119125
var result = args.Key;
120126
if (result.Equals(Delimiter, StringComparison.InvariantCultureIgnoreCase) && _internalValue != null)
121127
{
122-
if (AllowSameValues == false && Values?.Contains(Converter.Set(_internalValue)) == true)
128+
if (AllowSameValues == false && Values?.Contains(Converter.Set(_internalValue) ?? string.Empty) == true)
123129
{
124130
await Task.Delay(10);
125131
_internalValue = Converter.Get(Converter.Set(_internalValue)?.Replace(result, null).ToString());
@@ -131,7 +137,7 @@ protected async Task HandleKeyDown(KeyboardEventArgs args)
131137
StateHasChanged();
132138
}
133139

134-
if (args.Key == "Backspace" && string.IsNullOrEmpty(Converter.Set(_internalValue)) && Values != null && Values.Any())
140+
if (args.Key == "Backspace" && string.IsNullOrEmpty(Converter.Set(_internalValue)) && Values != null && Values.Any() && BackspaceChipRemoval == true)
135141
{
136142
Values.RemoveAt(Values.Count - 1);
137143
await ValuesChanged.InvokeAsync(Values);

0 commit comments

Comments
 (0)