Skip to content

Commit 24aea42

Browse files
authored
ComboBox: SearchString an Id Fix (#309)
* ComboBox SearchString Fix * Add SearchField Id
1 parent 42029ec commit 24aea42

4 files changed

Lines changed: 56 additions & 10 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@namespace MudExtensions.UnitTests.TestComponents
2+
<MudPopoverProvider></MudPopoverProvider>
3+
4+
<MudComboBox @bind-SelectedValues="values" MultiSelection="true" Editable="true">
5+
<MudComboBoxItem Value="@("one1")">one1</MudComboBoxItem>
6+
<MudComboBoxItem Value="@("one2")">one2</MudComboBoxItem>
7+
<MudComboBoxItem Value="@("two1")">two1</MudComboBoxItem>
8+
<MudComboBoxItem Value="@("two2")">two2</MudComboBoxItem>
9+
</MudComboBox>
10+
11+
12+
@code {
13+
IEnumerable<string> values = new HashSet<string>();
14+
}

CodeBeam.MudBlazor.Extensions.UnitTests/Components/ComboboxTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,33 @@ public void MultiSelect_SelectAll()
244244
combobox.Instance.SelectedValues.Should().BeEquivalentTo(new HashSet<string>());
245245
}
246246

247+
[Test]
248+
public async Task Combobox_MultiSelectEditable()
249+
{
250+
var comp = Context.RenderComponent<ComboboxMultiSelectEditableTest>();
251+
// print the generated html
252+
Console.WriteLine(comp.Markup);
253+
// select elements needed for the test
254+
var combobox = comp.FindComponent<MudComboBox<string>>();
255+
var menu = comp.Find("div.mud-popover");
256+
var input = combobox.Find("div.mud-input-control");
257+
// check initial state
258+
combobox.Instance.Value.Should().BeNullOrEmpty();
259+
comp.WaitForAssertion(() =>
260+
comp.Find("div.mud-popover").ClassList.Should().Contain("d-none"));
261+
// click and check if it has toggled the menu
262+
await comp.InvokeAsync(() => input.MouseDown());
263+
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("d-none"));
264+
// now click an item and see the value change
265+
comp.WaitForAssertion(() => comp.FindAll("div.mud-combobox-item").Count.Should().Be(4));
266+
comp.WaitForAssertion(() => combobox.Instance.GetEligibleAndNonDisabledItems().Count.Should().Be(4));
267+
var items = comp.FindAll("div.mud-combobox-item").ToArray();
268+
await comp.InvokeAsync(() => combobox.Instance.HandleInternalValueChanged("t"));
269+
await comp.InvokeAsync(() => combobox.Instance.ForceRenderItems());
270+
items = comp.FindAll("div.mud-combobox-item").ToArray();
271+
comp.WaitForAssertion(() => combobox.Instance.GetEligibleAndNonDisabledItems().Count.Should().Be(2));
272+
}
273+
247274
/// <summary>
248275
/// Click should not close the menu and selecting multiple values should update the bindable value with a comma separated list.
249276
/// </summary>

CodeBeam.MudBlazor.Extensions/Components/ComboBox/MudComboBox.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
@if (Editable)
120120
{
121121
<div class="pa-2">
122-
<MudTextFieldExtended @ref="_searchbox" T="string" Value="_searchString" ValueChanged="@HandleInternalValueChanged" Margin="Margin.Dense"
122+
<MudTextFieldExtended id="@($"{_elementId}-autocomplete")" @ref="_searchbox" T="string" Value="_searchString" ValueChanged="@HandleInternalValueChanged" Margin="Margin.Dense"
123123
Variant="Variant.Outlined" Immediate="true" OnKeyDown="SearchBoxHandleKeyDown" OnKeyUp="SearchBoxHandleKeyUp" Clearable="@SearchBoxClearable">
124124
<AdornmentEnd>
125125
<MudIcon Icon="@Icons.Material.Filled.Search" Color="@Color" />

CodeBeam.MudBlazor.Extensions/Components/ComboBox/MudComboBox.razor.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ public MudComboBox()
2525
[Inject] IScrollManagerExtended ScrollManagerExtended { get; set; }
2626
[Inject] IScrollManager ScrollManager { get; set; }
2727

28-
internal string _searchString;
28+
protected internal void SetSearchString(T value)
29+
{
30+
_searchString = Converter.Set(value);
31+
}
32+
33+
internal string _searchString { get; set; }
2934
private string multiSelectionText;
3035
private IKeyInterceptor _keyInterceptor;
3136

@@ -321,7 +326,7 @@ public MudComboBox()
321326
public string Delimiter { get; set; } = ", ";
322327

323328
/// <summary>
324-
/// If true popover width will be the same as the select component.
329+
/// If true popover width will be the same as the combobox component.
325330
/// </summary>
326331
[Parameter]
327332
[Category(CategoryTypes.FormComponent.Behavior)]
@@ -335,7 +340,7 @@ public MudComboBox()
335340
public int MaxHeight { get; set; } = 300;
336341

337342
/// <summary>
338-
/// Set the anchor origin point to determen where the popover will open from.
343+
/// Set the anchor origin point to determine where the popover will open from.
339344
/// </summary>
340345
[Parameter]
341346
[Category(CategoryTypes.FormComponent.ListAppearance)]
@@ -908,16 +913,16 @@ protected internal async Task HandleOnBlur(FocusEventArgs obj)
908913
{
909914
await Clear();
910915
}
911-
912-
_searchString = Text;
916+
917+
//_searchString = Text;
913918
}
914919
}
915920

916921

917922
await OnBlurredAsync(obj);
918923
}
919924

920-
protected void HandleInternalValueChanged(string val)
925+
protected internal void HandleInternalValueChanged(string val)
921926
{
922927
_searchString = val;
923928
}
@@ -1236,13 +1241,13 @@ protected async Task SelectAllItems()
12361241
return null;
12371242
}
12381243

1239-
protected void ForceRenderItems()
1244+
protected internal void ForceRenderItems()
12401245
{
12411246
Items.ForEach((x) => x.ForceRender());
12421247
StateHasChanged();
12431248
}
12441249

1245-
protected async Task ForceUpdateItems()
1250+
protected internal async Task ForceUpdateItems()
12461251
{
12471252
Items.ForEach(async (x) => await x.ForceUpdate());
12481253
}
@@ -1406,7 +1411,7 @@ public async Task ActiveLastItem()
14061411

14071412
#endregion
14081413

1409-
protected List<MudComboBoxItem<T>> GetEligibleAndNonDisabledItems()
1414+
protected internal List<MudComboBoxItem<T>> GetEligibleAndNonDisabledItems()
14101415
{
14111416
if (Items == null)
14121417
{

0 commit comments

Comments
 (0)