| | | 1 | | <style> |
| | | 2 | | .search-input { |
| | | 3 | | background: var(--mud-palette-primary); |
| | | 4 | | } |
| | | 5 | | </style> |
| | | 6 | | @using Chronicis.Client.Services |
| | | 7 | | @inject ITreeStateService TreeState |
| | | 8 | | |
| | | 9 | | <div class="search-box"> |
| | | 10 | | <MudTextField @bind-Value="_searchText" |
| | | 11 | | Placeholder="Search entries..." |
| | | 12 | | Variant="Variant.Outlined" |
| | | 13 | | Margin="Margin.Dense" |
| | | 14 | | Adornment="Adornment.End" |
| | | 15 | | AdornmentIcon="@(_hasText ? Icons.Material.Filled.Close : Icons.Material.Filled.Search)" |
| | | 16 | | OnAdornmentClick="OnAdornmentClick" |
| | | 17 | | @onkeydown="OnKeyDown" |
| | | 18 | | Immediate="true" |
| | | 19 | | Class="search-input" |
| | | 20 | | tabindex="1" /> |
| | | 21 | | </div> |
| | | 22 | | |
| | | 23 | | @code { |
| | 0 | 24 | | private string _searchText = string.Empty; |
| | 0 | 25 | | private bool _hasText => !string.IsNullOrWhiteSpace(_searchText); |
| | | 26 | | |
| | | 27 | | protected override void OnInitialized() |
| | | 28 | | { |
| | 0 | 29 | | TreeState.OnStateChanged += StateHasChanged; |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | private async Task OnAdornmentClick() |
| | | 33 | | { |
| | 0 | 34 | | if (_hasText) |
| | | 35 | | { |
| | | 36 | | // Clear button clicked |
| | 0 | 37 | | _searchText = string.Empty; |
| | 0 | 38 | | TreeState.ClearSearch(); |
| | | 39 | | } |
| | | 40 | | else |
| | | 41 | | { |
| | | 42 | | // Search button clicked |
| | 0 | 43 | | await ExecuteSearch(); |
| | | 44 | | } |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | private async Task OnKeyDown(KeyboardEventArgs e) |
| | | 48 | | { |
| | 0 | 49 | | if (e.Key == "Enter") |
| | | 50 | | { |
| | 0 | 51 | | await ExecuteSearch(); |
| | | 52 | | } |
| | 0 | 53 | | else if (e.Key == "Escape") |
| | | 54 | | { |
| | 0 | 55 | | _searchText = string.Empty; |
| | 0 | 56 | | TreeState.ClearSearch(); |
| | | 57 | | } |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | private Task ExecuteSearch() |
| | | 61 | | { |
| | 0 | 62 | | TreeState.SetSearchQuery(_searchText); |
| | 0 | 63 | | return Task.CompletedTask; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | public void Dispose() |
| | | 67 | | { |
| | 0 | 68 | | TreeState.OnStateChanged -= StateHasChanged; |
| | 0 | 69 | | } |
| | | 70 | | } |