|
| 1 | +@page "/Lists/Autocomplete/Debug" |
| 2 | + |
| 3 | +@using static FluentUI.Demo.SampleData.Olympics2024 |
| 4 | + |
| 5 | +<div style="margin: 24px;"> |
| 6 | + |
| 7 | + <div>Selected items: <b>@string.Join("; ", SelectedCountries.Select(c => c.Name))</b></div> |
| 8 | + <div>Search Text: <b>@SearchText</b></div> |
| 9 | + |
| 10 | + <FluentAutocomplete TOption="Country" |
| 11 | + TValue="string" |
| 12 | + Width="100%" |
| 13 | + Label="Select countries" |
| 14 | + Placeholder="Type to search..." |
| 15 | + ShowProgressIndicator="@ShowProgressIndicator" |
| 16 | + MaxAutoHeight="@((MaxAutoHeight) ? "unset" : null)" |
| 17 | + OnOptionsSearch="@OnSearchAsync" |
| 18 | + Items="@Countries" |
| 19 | + OptionText="@(item => item.Name)" |
| 20 | + MaxSelectedWidth="@(MaxSelectedWidth ? "40px" : null)" |
| 21 | + ShowDismiss="@ShowDismiss" |
| 22 | + Multiple="@Multiple" |
| 23 | + MaximumSelectedOptions="@(SetMaximumSelectedOptions ? 4 : (int?)null)" |
| 24 | + @bind-Value="@SearchText" |
| 25 | + @bind-SelectedItems="@SelectedCountries"> |
| 26 | + |
| 27 | + @* Drop-down item template *@ |
| 28 | + <OptionTemplate> |
| 29 | + <FluentStack Style="pointer-events: none;" VerticalAlignment="VerticalAlignment.Center"> |
| 30 | + <FluentAvatar Image="@context.Flag()" |
| 31 | + Name="@context.Name" |
| 32 | + Size="AvatarSize.Size20" /> |
| 33 | + <FluentText Margin="@Margin.Left4"> |
| 34 | + @context.Name |
| 35 | + </FluentText> |
| 36 | + </FluentStack> |
| 37 | + </OptionTemplate> |
| 38 | + |
| 39 | + @* Content displayed at the top of the drop-down list *@ |
| 40 | + <HeaderContent> |
| 41 | + <FluentText Size="TextSize.Size200" Color="Color.Primary" Align="TextAlign.Center"> |
| 42 | + Suggested contacts |
| 43 | + </FluentText> |
| 44 | + <FluentProgressBar Thickness="ProgressThickness.Large" Visible="@context.InProgress" /> |
| 45 | + </HeaderContent> |
| 46 | + |
| 47 | + @* Content displayed at the bottom of the drop-down list *@ |
| 48 | + <FooterContent> |
| 49 | + @if (!context.Items.Any()) |
| 50 | + { |
| 51 | + <FluentText Size="TextSize.Size200" Align="TextAlign.Center" Color="Color.Error" Style="width: 100%;"> |
| 52 | + No results found |
| 53 | + </FluentText> |
| 54 | + } |
| 55 | + </FooterContent> |
| 56 | + |
| 57 | + </FluentAutocomplete> |
| 58 | + |
| 59 | + <FluentStack Orientation="Orientation.Vertical"> |
| 60 | + <FluentSwitch @bind-Value="@ShowProgressIndicator" Label="Show progress indicator" /> |
| 61 | + <FluentSwitch @bind-Value="@MaxAutoHeight" Label="Auto height" /> |
| 62 | + <FluentSwitch @bind-Value="@MaxSelectedWidth" Label="Set a max selected width (40px)" /> |
| 63 | + <FluentSwitch @bind-Value="@ShowDismiss" Label="Show search or dismiss button" /> |
| 64 | + <FluentSwitch @bind-Value="@Multiple" Label="Multiple selection" /> |
| 65 | + <FluentSwitch @bind-Value="@SetMaximumSelectedOptions" Label="Set MaximumSelectedOptions to 4" /> |
| 66 | + <FluentButton OnClick="@SetSelectedCountriesAsync"> |
| 67 | + Set SelectedCountries to [be, fr] |
| 68 | + </FluentButton> |
| 69 | + </FluentStack> |
| 70 | + |
| 71 | + |
| 72 | + <FluentAutocomplete TOption="Country" |
| 73 | + TValue="string" |
| 74 | + Width="100%" |
| 75 | + MaximumOptionsSearch="int.MaxValue" |
| 76 | + Label="Select countries from Items" |
| 77 | + Items="@Countries" |
| 78 | + OptionText="@(item => item.Name)" |
| 79 | + @bind-SelectedItems="@SelectedCountries" /> |
| 80 | + |
| 81 | +</div> |
| 82 | + |
| 83 | +@code |
| 84 | +{ |
| 85 | + bool ShowProgressIndicator { get; set; } |
| 86 | + bool MaxAutoHeight { get; set; } |
| 87 | + bool MaxSelectedWidth { get; set; } |
| 88 | + bool ShowDismiss { get; set; } = true; |
| 89 | + bool Multiple { get; set; } = true; |
| 90 | + bool SetMaximumSelectedOptions { get; set; } |
| 91 | + string? SearchText { get; set; } |
| 92 | + IEnumerable<Country> SelectedCountries { get; set; } = []; |
| 93 | + |
| 94 | + async Task OnSearchAsync(OptionsSearchEventArgs<Country> e) |
| 95 | + { |
| 96 | + if (ShowProgressIndicator) |
| 97 | + { |
| 98 | + await Task.Delay(500); // Simulate async search |
| 99 | + } |
| 100 | + |
| 101 | + e.Items = Countries.Where(i => i.Name.StartsWith(e.Text, StringComparison.OrdinalIgnoreCase)) |
| 102 | + .OrderBy(i => i.Name); |
| 103 | + } |
| 104 | + |
| 105 | + async Task SetSelectedCountriesAsync() |
| 106 | + { |
| 107 | + SelectedCountries = Countries.Where(i => i.Code == "be" || i.Code == "fr"); |
| 108 | + await Task.CompletedTask; |
| 109 | + } |
| 110 | +} |
0 commit comments