diff --git a/BlazorFluentUI/Components/Layout/NavMenu.razor b/BlazorFluentUI/Components/Layout/NavMenu.razor index 935c1be..e843a2e 100644 --- a/BlazorFluentUI/Components/Layout/NavMenu.razor +++ b/BlazorFluentUI/Components/Layout/NavMenu.razor @@ -33,6 +33,7 @@ NumberField Radio Rating + Search diff --git a/BlazorFluentUI/Components/Pages/FormInputs/FormInputSearch.razor b/BlazorFluentUI/Components/Pages/FormInputs/FormInputSearch.razor new file mode 100644 index 0000000..f9fec0d --- /dev/null +++ b/BlazorFluentUI/Components/Pages/FormInputs/FormInputSearch.razor @@ -0,0 +1,217 @@ +@page "/forminputsearch" + +@using System.Timers; + +@rendermode RenderMode.InteractiveServer + +Search + + + The FluentSearch wraps a web component of a search element. + + +Default Example + + + Without a label: + + + + With a label: + + + + +Interactive + + + + + You searched for: @interactiveSearchValue + + + +Interactive with debounce + + DebounceSearchValue = e.Value?.ToString())"/> + + + You searched for: @debounceSearchValue + + + +@code { + string? value; + + FluentSearch interactiveTest; + string? interactiveSearchValue = string.Empty; + + static string interactiveDefaultsText = "No results"; + static List defaultSearchResult() + { + return new List() { interactiveDefaultsText }; + } + + List interactiveSearchResults = defaultSearchResult(); + + void HandleSearchInput() + { + if (string.IsNullOrWhiteSpace(interactiveSearchValue)) + { + interactiveSearchResults = defaultSearchResult(); + interactiveSearchValue = string.Empty; + } + else + { + string searchTerm = interactiveSearchValue.ToLower(); + if (searchTerm.Length < 1) + return; + + List temp = searchData.Where(str => str.ToLower().Contains(searchTerm)).Select(str => str).ToList(); + if (temp.Count > 0) + interactiveSearchResults = temp; + } + } + + List searchData = new() + { + "Alabama", + "Alaska", + "Arizona", + "Arkansas", + "California", + "Colorado", + "Connecticut", + "Delaware", + "Florida", + "Georgia", + "Hawaii", + "Idaho", + "Illinois", + "Indiana", + "Iowa", + "Kansas", + "Kentucky", + "Louisiana", + "Maine", + "Maryland", + "Massachussets", + "Michigain", + "Minnesota", + "Mississippi", + "Missouri", + "Montana", + "Nebraska", + "Nevada", + "New Hampshire", + "New Jersey", + "New Mexico", + "New York", + "North Carolina", + "North Dakota", + "Ohio", + "Oklahoma", + "Oregon", + "Pennsylvania", + "Rhode Island", + "South Carolina", + "South Dakota", + "Texas", + "Tennessee", + "Utah", + "Vermont", + "Virginia", + "Washington", + "Wisconsin", + "West Virginia", + "Wyoming" + }; + + Timer? debounceTimer = null; + FluentSearch? debounceTest; + string? debounceSearchValue = string.Empty; + + string? DebounceSearchValue + { + get => debounceSearchValue; + set + { + if (value != debounceSearchValue) + { + debounceSearchValue = value; + DisposeDebounceTimer(); + debounceTimer = new Timer(400); + debounceTimer.Elapsed += TimerElapsed_TickAsync; + debounceTimer.Enabled = true; + debounceTimer.Start(); + } + } + } + + private async void TimerElapsed_TickAsync(object? sender, EventArgs e) + { + DisposeDebounceTimer(); + await InvokeAsync(OnDebounceSearch); + } + + private void DisposeDebounceTimer() + { + if (debounceTimer != null) + { + debounceTimer.Enabled = false; + debounceTimer.Elapsed -= TimerElapsed_TickAsync; + debounceTimer.Dispose(); + debounceTimer = null; + } + } + + List debounceSearchResults = defaultSearchResult(); + + private void OnDebounceSearch() + { + if (!string.IsNullOrWhiteSpace(DebounceSearchValue)) + { + string searchTerm = DebounceSearchValue.ToLower(); + + List temp = searchData.Where(str => str.ToLower().Contains(searchTerm)).Select(str => str).ToList(); + if (temp.Count() > 0) + { + debounceSearchResults = temp; + } + else + { + debounceSearchResults = defaultSearchResult(); + } + StateHasChanged(); + } + else + { + debounceSearchResults = defaultSearchResult(); + StateHasChanged(); + } + } + + private void HandleDebounceClear() + { + if (string.IsNullOrWhiteSpace(DebounceSearchValue)) + return; + + DisposeDebounceTimer(); + debounceSearchResults = defaultSearchResult(); + DebounceSearchValue = string.Empty; + StateHasChanged(); + } +}
FluentSearch
search
+ You searched for: @interactiveSearchValue +
+ You searched for: @debounceSearchValue +