Skip to content

Commit ed69147

Browse files
authored
ChipField String Delimiter and AllowSameValues (#421)
1 parent 8eac330 commit ed69147

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

CodeBeam.MudBlazor.Extensions.Docs/Pages/Components/ChipField/ChipFieldPage.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<ChipFieldExampleIntro />
77
</ExampleCard>
88

9-
<ExampleCard ComponentName="ChipField" ExampleName="ChipFieldExample1" Title="Playground" Description="Chip field create chips when user press delimiter key.">
9+
<ExampleCard ComponentName="ChipField" ExampleName="ChipFieldExample1" Title="Playground" Description="Chip field create chips when user press delimiter key. Delimiter also accepts longer key names like 'Enter', 'Delete' etc.">
1010
<ChipFieldExample1 />
1111
</ExampleCard>
1212
</ExamplePage>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<MudGrid>
44
<MudItem xs="12" sm="8" Class="d-flex flex-column gap-8 align-center justify-center">
55
<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" />
6+
ChipColor="_color" ChipVariant="_chipVariant" ChipSize="_chipSize" WrapChips="_wrapChips" MaxChips="_maxChips" ChipsMaxWidth="_chipsMaxWidth" Closeable="_closeable" Variant="_variant" Label="ChipField" AllowSameValues="@_allowSameValues" />
77
</MudItem>
88

99
<MudItem xs="12" sm="4">
@@ -47,12 +47,13 @@
4747
<MudSwitchM3 @bind-Value="@_readonly" Label="ReadOnly" Color="Color.Secondary" />
4848
<MudSwitchM3 @bind-Value="@_disableRadius" Label="Disable Border Radius" Color="Color.Secondary" />
4949
<MudSwitchM3 @bind-Value="@_closeable" Label="Closeable" Color="Color.Secondary" />
50+
<MudSwitchM3 @bind-Value="@_allowSameValues" Label="Allow Same Values" Color="Color.Secondary" />
5051
</MudStack>
5152
</MudItem>
5253
</MudGrid>
5354

5455
@code{
55-
char _delimiter = ' ';
56+
string _delimiter = " ";
5657
string? _value;
5758
List<string>? _values;
5859
int _maxChips = 0;
@@ -66,4 +67,5 @@
6667
bool _readonly;
6768
bool _disableRadius;
6869
bool _closeable = true;
70+
bool _allowSameValues = false;
6971
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public partial class MudChipField<T> : MudTextFieldExtended<T>
4343
[Parameter]
4444
public EventCallback<List<string>> ValuesChanged { get; set; }
4545

46+
/// <summary>
47+
/// If false, pressing delimeter key has no effect if the value is already in values. Default is false.
48+
/// </summary>
49+
[Parameter]
50+
public bool AllowSameValues { get; set; }
51+
4652
/// <summary>
4753
/// Determines chip size with small, medium or large values.
4854
/// </summary>
@@ -53,7 +59,7 @@ public partial class MudChipField<T> : MudTextFieldExtended<T>
5359
/// The char that created a new chip with current value.
5460
/// </summary>
5561
[Parameter]
56-
public char? Delimiter { get; set; }
62+
public string? Delimiter { get; set; } = " ";
5763

5864
/// <summary>
5965
/// CSS classes of the chips, seperated by space.
@@ -110,9 +116,17 @@ public partial class MudChipField<T> : MudTextFieldExtended<T>
110116
/// <returns></returns>
111117
protected async Task HandleKeyDown(KeyboardEventArgs args)
112118
{
113-
var result = args.Code == "Space" ? " " : args.Key;
114-
if (result == Delimiter.ToString() && _internalValue != null)
119+
var result = args.Key;
120+
if (result.Equals(Delimiter, StringComparison.InvariantCultureIgnoreCase) && _internalValue != null)
115121
{
122+
if (AllowSameValues == false && Values?.Contains(Converter.Set(_internalValue)) == true)
123+
{
124+
await Task.Delay(10);
125+
_internalValue = Converter.Get(Converter.Set(_internalValue)?.Replace(result, null).ToString());
126+
await SetValueAsync(_internalValue);
127+
StateHasChanged();
128+
return;
129+
}
116130
await SetChips();
117131
StateHasChanged();
118132
}

0 commit comments

Comments
 (0)