Skip to content

Commit

Permalink
Autocomplete: Add MoreItemsTemplate RenderFragment. (MudBlazor#4566)
Browse files Browse the repository at this point in the history
* Autocomplete: Add MoreItemsTemplate RenderFragment.

* Fix unit test.

* Always count returned items on search.
  • Loading branch information
Mr-Technician authored and jammerware committed Sep 20, 2022
1 parent e507fd6 commit 8cadb1d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
@@ -0,0 +1,43 @@
@namespace MudBlazor.UnitTests.TestComponents
<MudPopoverProvider></MudPopoverProvider>

<MudAutocomplete T="string" Label="US States" @bind-Value="value1" SearchFunc="@Search1" MaxItems="10">
<MoreItemsTemplate>
<MudText>Not all items are shown</MudText>
</MoreItemsTemplate>
</MudAutocomplete>

@code {
public static string __description__ = "There are more than 10 items, so the more items template should render";

private string value1;

private string[] states =
{
"Alabama", "Alaska", "American Samoa", "Arizona",
"Arkansas", "California", "Colorado", "Connecticut",
"Delaware", "District of Columbia", "Federated States of Micronesia",
"Florida", "Georgia", "Guam", "Hawaii", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
"Louisiana", "Maine", "Marshall Islands", "Maryland",
"Massachusetts", "Michigan", "Minnesota", "Mississippi",
"Missouri", "Montana", "Nebraska", "Nevada",
"New Hampshire", "New Jersey", "New Mexico", "New York",
"North Carolina", "North Dakota", "Northern Mariana Islands", "Ohio",
"Oklahoma", "Oregon", "Palau", "Pennsylvania", "Puerto Rico",
"Rhode Island", "South Carolina", "South Dakota", "Tennessee",
"Texas", "Utah", "Vermont", "Virgin Island", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming",
};

private async Task<IEnumerable<string>> Search1(string value)
{
// In real life use an asynchronous function for fetching data from an api.
await Task.Delay(5);

// if text is null or empty, show complete list
if (string.IsNullOrEmpty(value))
return states;
return states.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase));
}
}
19 changes: 18 additions & 1 deletion src/MudBlazor.UnitTests/Components/AutocompleteTests.cs
Expand Up @@ -221,6 +221,22 @@ public async Task Autocomplete_ReadOnly_Should_Not_Open()
comp.WaitForAssertion(() => comp.FindAll("div.mud-popover-open").Count.Should().Be(0));
}

/// <summary>
/// MoreItemsTemplate should render when there are more items than the MaxItems limit
/// </summary>
[Test]
public async Task AutocompleteTest6()
{
var comp = Context.RenderComponent<AutocompleteTest6>();

var inputControl = comp.Find("div.mud-input-control");
inputControl.Click();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("mud-popover-open"));

var mudText = comp.FindAll("p.mud-typography");
mudText[mudText.Count - 1].InnerHtml.Should().Contain("Not all items are shown"); //ensure the text is shown
}

/// <summary>
/// After press Enter key down, the selected value should be shown in the input value
/// </summary>
Expand Down Expand Up @@ -655,7 +671,8 @@ public async Task Autocomplete_Should_Not_Select_Disabled_Item()
[Test]
public async Task Autocomplete_ChangeBoundValue()
{
await ImproveChanceOfSuccess(async () => {
await ImproveChanceOfSuccess(async () =>
{
var comp = Context.RenderComponent<AutocompleteChangeBoundObjectTest>();
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
var autocomplete = autocompletecomp.Instance;
Expand Down
8 changes: 7 additions & 1 deletion src/MudBlazor/Components/Autocomplete/MudAutocomplete.razor
Expand Up @@ -9,7 +9,7 @@
<InputContent>
<MudInput @ref="_elementReference" @key="_elementKey" InputType="InputType.Text"
Class="mud-select-input" Margin="@Margin"
Variant="@Variant"
Variant="@Variant"
TextUpdateSuppression="@TextUpdateSuppression"
Value="@Text" DisableUnderLine="@DisableUnderLine"
Disabled="@Disabled" ReadOnly="@ReadOnly" Error="@Error"
Expand Down Expand Up @@ -53,6 +53,12 @@
}
</MudListItem>
}
@if (MoreItemsTemplate != null && _itemsReturned > MaxItems)
{
<div class="pa-1">
@MoreItemsTemplate
</div>
}
</MudList>
}
</MudPopover>
Expand Down
12 changes: 12 additions & 0 deletions src/MudBlazor/Components/Autocomplete/MudAutocomplete.razor.cs
Expand Up @@ -183,6 +183,13 @@ public bool Dense
[Category(CategoryTypes.FormComponent.ListBehavior)]
public RenderFragment<T> ItemDisabledTemplate { get; set; }

/// <summary>
/// Optional template when more items were returned from the Search function than the MaxItems limit
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.ListBehavior)]
public RenderFragment MoreItemsTemplate { get; set; }

/// <summary>
/// On drop-down close override Text with selected Value. This makes it clear to the user
/// which list value is currently selected and disallows incomplete values in Text.
Expand Down Expand Up @@ -356,6 +363,8 @@ protected override async Task UpdateValuePropertyAsync(bool updateText)

private void OnTimerComplete(object stateInfo) => InvokeAsync(OnSearchAsync);

private int _itemsReturned; //the number of items returned by the search function

/// <remarks>
/// This async method needs to return a task and be awaited in order for
/// unit tests that trigger this method to work correctly.
Expand All @@ -378,8 +387,11 @@ private async Task OnSearchAsync()
{
Console.WriteLine("The search function failed to return results: " + e.Message);
}
_itemsReturned = searched_items.Count();
if (MaxItems.HasValue)
{
searched_items = searched_items.Take(MaxItems.Value);
}
_items = searched_items.ToArray();

_enabledItemIndices = _items.Select((item, idx) => (item, idx)).Where(tuple => ItemDisabledFunc?.Invoke(tuple.item) != true).Select(tuple => tuple.idx).ToList();
Expand Down

0 comments on commit 8cadb1d

Please sign in to comment.