Skip to content

Commit 26156f0

Browse files
committed
Moved sorting logic to component
1 parent 741ac90 commit 26156f0

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

TrieTypeAHead/Pages/Index.razor

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
<InputTypeAHead AllWords="_words" AutoCompleteCount="5"></InputTypeAHead>
99
</div>
1010
@code {
11-
private readonly string[] _words = new[] { "Cat", "Car", "Dog", "Day", "Hamster", "School", "Sand", "Tea", "Water",
12-
"Cold", "Weather", "Blazor", "Dotnet", "CSharp", "Steven", "Giesel", "Example", "Blog", "Stamp", "Stencil" }
13-
.OrderBy(w => w)
14-
.ToArray();
11+
12+
private readonly string[] _words =
13+
{
14+
"Cat", "Car", "Dog", "Day", "Hamster", "School", "Sand", "Tea", "Water",
15+
"Cold", "Weather", "Blazor", "Dotnet", "CSharp", "Steven", "Giesel", "Example", "Blog", "Stamp", "Stencil"
16+
};
1517

1618
}

TrieTypeAHead/Shared/InputTypeAHead.razor

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,53 @@
11
@using LinkDotNet.Blog.Web.Features.Components.Typeahead
22

3+
@* We are using Bootstrap 5 example here. I want my input and the suggestion directly under each other*@
34
<div class="row">
5+
@*
6+
Every time the user inputs a text we update our suggestion
7+
You could also add debouncing and stuff here
8+
*@
49
<input @oninput="UpdateAutoCompletion" value="@content"/>
10+
@* Only show suggestions if we have at least one *@
511
@if (similarWords.Any())
612
{
13+
@*
14+
Not super sexy but it does what we want. We are using a select element
15+
which shows all the words and we can change/click on it so that the
16+
input element will change
17+
*@
718
<select size="@AutoCompleteCount" @onchange="UseSelectedWord">
8-
@foreach (var similarWord in similarWords)
9-
{
10-
<option value="@similarWord">@similarWord</option>
11-
}
19+
@foreach (var similarWord in similarWords)
20+
{
21+
<option value="@similarWord">@similarWord</option>
22+
}
1223
</select>
1324
}
1425
</div>
1526
@code {
27+
// Let the parent tell which words we have to "index"
1628
[Parameter] public IEnumerable<string> AllWords { get; set; }
29+
// The maximum amount of suggestion we will give
1730
[Parameter] public int AutoCompleteCount { get; set; } = 10;
1831

1932
private readonly Trie trie = new();
2033
private string content = string.Empty;
2134
private List<string> similarWords = new();
2235

23-
protected override void OnParametersSet()
36+
// Create the Trie once
37+
// You could also use OnParameterSet and re-create the trie when the parameters change
38+
protected override void OnInitialized()
2439
{
25-
foreach (var word in AllWords)
40+
// Lets order the words alphabetically - so it is easier for the user to select her/his word
41+
foreach (var word in AllWords.OrderBy(w => w))
2642
{
2743
trie.Add(word);
2844
}
2945
}
3046

31-
private void UpdateAutoCompletion(ChangeEventArgs obj)
47+
private void UpdateAutoCompletion(ChangeEventArgs inputEventArgs)
3248
{
33-
content = obj.Value.ToString();
49+
content = inputEventArgs.Value.ToString();
50+
// Get all words from our Trie which share the prefix (at most AutoCompleteCount)
3451
similarWords = trie.GetWordsWithPrefix(content).Take(AutoCompleteCount).ToList();
3552
}
3653

0 commit comments

Comments
 (0)