diff --git a/src/Blazored.Typeahead/BlazoredTypeahead.razor.cs b/src/Blazored.Typeahead/BlazoredTypeahead.razor.cs index 623aaf6..9032889 100644 --- a/src/Blazored.Typeahead/BlazoredTypeahead.razor.cs +++ b/src/Blazored.Typeahead/BlazoredTypeahead.razor.cs @@ -18,6 +18,7 @@ public partial class BlazoredTypeahead : ComponentBase, IDisposab private Timer _debounceTimer; private string _searchText = string.Empty; private bool _eventsHookedUp = false; + private bool _initialized = false; private ElementReference _searchInput; private ElementReference _mask; @@ -52,6 +53,7 @@ public partial class BlazoredTypeahead : ComponentBase, IDisposab [Parameter] public bool EnableDropDown { get; set; } = false; [Parameter] public bool ShowDropDownOnFocus { get; set; } = false; [Parameter] public bool DisableClear { get; set; } = false; + [Parameter] public bool DisableClearSearchText { get; set; } = false; [Parameter] public bool StopPropagation { get; set; } = false; [Parameter] public bool PreventDefault { get; set; } = false; @@ -121,6 +123,7 @@ protected override void OnInitialized() _fieldIdentifier = IsMultiselect ? FieldIdentifier.Create(ValuesExpression) : FieldIdentifier.Create(ValueExpression); Initialize(); + _initialized = true; } protected override async Task OnAfterRenderAsync(bool firstRender) @@ -134,7 +137,10 @@ protected override async Task OnAfterRenderAsync(bool firstRender) private void Initialize() { - SearchText = ""; + if (!DisableClearSearchText || !_initialized || IsMultiselect) + { + SearchText = ""; + } IsShowingSuggestions = false; IsShowingMask = Value != null; } @@ -173,9 +179,12 @@ private async Task HandleClear() private async Task HandleClickOnMask() { - SearchText = ""; - IsShowingMask = false; + if (!DisableClearSearchText) + { + SearchText = ""; + } + IsShowingMask = false; await Task.Delay(250); // Possible race condition here. await Interop.Focus(JSRuntime, _searchInput); await HookOutsideClick(); @@ -240,7 +249,7 @@ private async Task HandleKeydown(KeyboardEventArgs args) { await ResetControl(); } - + } private async Task HandleKeyup(KeyboardEventArgs args) @@ -400,10 +409,10 @@ private async Task HookOutsideClick() await JSRuntime.OnOutsideClick(_searchInput, this, "ResetControlBlur", true); } - private async Task SelectResult(TItem item) + public async Task SelectResult(TItem item) { var value = ConvertMethod(item); - + if (IsMultiselect) { var valueList = Values ?? new List(); @@ -417,7 +426,7 @@ private async Task SelectResult(TItem item) } else { - if (Value != null && Value.Equals(value)) return; + if (Value != null && Value.Equals(value) && !DisableClearSearchText) return; Value = value; await ValueChanged.InvokeAsync(value); }