Blazored / Typeahead

Typeahead control for Blazor applications
https://blazored.github.io/Typeahead/
MIT License
433 stars 103 forks source link

[Question] #264

Open CopperBeardy opened 2 years ago

CopperBeardy commented 2 years ago

ran into a edge case when adding new items.

if you had a list with "Foo bar" and ""Foo boo" in it and you wanted to add "Foo" the typeahead provide no way to do this, as it has found suggestions, even though none match exactly.

you can change the search to look for exact match instead of look for terms that contain the search value such as private async Task<IEnumerable<string>> SearchItems(string searchText) => Task.FromResult(Items.Where(x => x.Name.ToLower().Equals(searchText.ToLower())).ToList()); even though this would show the notfoundtemplate, you would not get any suggestions unless they were exactly the same as the search text which would negate the purpose of the suggestions

I can easily write code to add the item to the collection, however I can access the methods to close the results template and select the item added as the methods built in are all private.

Is there a workaround so that if the exact match is not found it will show display a hybrid view of the notfoundtemplate and the resulttemplate so you can either choose to add it as a new item or select one of the existing options

Kampfmoehre commented 2 years ago

Unfortunately there doesn't seem to be any way to do this, at least when I check the code here: https://github.com/Blazored/Typeahead/blob/main/src/Blazored.Typeahead/BlazoredTypeahead.razor.cs#L486

thejaff2 commented 2 years ago

Because of this, I have resigned to only use the control as a dropdown with add-new-functionality.

I have done this by always return empty result when typing (triggering Add-new..,), otherwise return all categories (for dropdown):

private async Task<IEnumerable<string>> SearchCategories(string searchText)
{
      var result = string.IsNullOrEmpty(searchText)
      ? categories
      : new List<string>();

      return await Task.FromResult(result);
}

EDIT: exact hit should return the item, not show the "Add-new"