Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

DisableClearSearchText parameter added #275

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/Blazored.Typeahead/BlazoredTypeahead.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public partial class BlazoredTypeahead<TItem, TValue> : ComponentBase, IDisposab
private Timer _debounceTimer;
private string _searchText = string.Empty;
private bool _eventsHookedUp = false;
private bool _initialized = false;
private ElementReference _searchInput;
private ElementReference _mask;

Expand Down Expand Up @@ -52,6 +53,7 @@ public partial class BlazoredTypeahead<TItem, TValue> : 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;
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -240,7 +249,7 @@ private async Task HandleKeydown(KeyboardEventArgs args)
{
await ResetControl();
}

}

private async Task HandleKeyup(KeyboardEventArgs args)
Expand Down Expand Up @@ -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<TValue>();
Expand All @@ -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);
}
Expand Down