Skip to content

Commit d568974

Browse files
committed
docs(kb): add kb for automatically select
1 parent 989148b commit d568974

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
title: How to Automatically Select Preselected Item on Blur
3+
description: Learn how to configure the Telerik ComboBox for Blazor to automatically select the first matching item when the input loses focus.
4+
type: how-to
5+
page_title: How to Automatically Select Preselected Item on Blur
6+
slug: combobox-kb-autoselect-on-blur
7+
tags: telerik, blazor, combobox, blur, auto-select
8+
res_type: kb
9+
---
10+
11+
## Environment
12+
13+
<table>
14+
<tbody>
15+
<tr>
16+
<td>Product</td>
17+
<td>ComboBox for Blazor</td>
18+
</tr>
19+
</tbody>
20+
</table>
21+
22+
## Description
23+
24+
The article asnwers to the following question:
25+
26+
* How can I configure the ComboBox to automatically select the first matching item when the input loses focus (e.g., when the user tabs away or clicks outside)?
27+
* How do I auto-select a ComboBox item based on user input when focus is lost?
28+
* Can the ComboBox select a suggested item on blur without pressing Enter?
29+
* How to set the ComboBox value when the user leaves the input field?
30+
31+
## Solution
32+
To automatically select the first matching item in the ComboBox when the input loses focus (on blur), follow these steps:
33+
34+
1. Handle the [`OnBlur` event](slug:components/combobox/events#onblur) of the ComboBox to detect when the input loses focus.
35+
2. Retrieve the currently highlighted or filtered item using a JavaScript helper function via `JS interop`.
36+
3. Check for a matching item in the ComboBox data based on the user's input.
37+
4. Set the matching item as the selected value programmatically.
38+
39+
>caption Auto-select the first matching item on blur
40+
41+
````RAZOR
42+
@inject IJSRuntime js
43+
44+
<p>Selected value: @ComboBoxValue</p>
45+
<p>First Filtered value: @FirstFilteredItem</p>
46+
47+
48+
<span onkeyup="@GetFirstFilteredItem">
49+
<TelerikComboBox Data="@ComboBoxData"
50+
@bind-Value="@ComboBoxValue"
51+
TextField="@nameof(ListItem.Text)"
52+
ValueField="@nameof(ListItem.Value)"
53+
Filterable="true"
54+
OnBlur="@SelectItemOnTab"
55+
OnOpen="@( () => IsComboBoxOpen = true )"
56+
OnClose="@( () => IsComboBoxOpen = false )"
57+
Width="200px">
58+
<ComboBoxSettings>
59+
<ComboBoxPopupSettings Class="select-on-tab" />
60+
</ComboBoxSettings>
61+
</TelerikComboBox>
62+
</span>
63+
<br />
64+
<br />
65+
<TelerikTextBox Width="200px" Placeholder="another element" />
66+
67+
<script suppress-error="BL9992">
68+
function getHighligtedComboItem() {
69+
var focusedItem = document.querySelector(".select-on-tab .k-list-item.k-focus");
70+
if (focusedItem) {
71+
return focusedItem.innerText;
72+
}
73+
}
74+
</script>
75+
76+
@code {
77+
private IEnumerable<ListItem> ComboBoxData = Enumerable.Range(1, 123).Select(x => new ListItem { Text = "Item " + x, Value = x });
78+
private int ComboBoxValue { get; set; }
79+
private string FirstFilteredItem { get; set; } = string.Empty;
80+
private bool IsComboBoxOpen { get; set; }
81+
82+
private async Task GetFirstFilteredItem()
83+
{
84+
FirstFilteredItem = await js.InvokeAsync<string>("getHighligtedComboItem");
85+
}
86+
87+
private void SelectItemOnTab()
88+
{
89+
if (!string.IsNullOrEmpty(FirstFilteredItem))
90+
{
91+
var matchingItem = ComboBoxData.FirstOrDefault(x => x.Text.ToLowerInvariant().Contains(FirstFilteredItem.Trim().ToLowerInvariant()));
92+
if (matchingItem != null)
93+
{
94+
ComboBoxValue = matchingItem.Value;
95+
FirstFilteredItem = string.Empty;
96+
}
97+
}
98+
}
99+
100+
public class ListItem
101+
{
102+
public int Value { get; set; }
103+
public string Text { get; set; } = string.Empty;
104+
}
105+
}
106+
````
107+
## See Also
108+
109+
- [ComboBox Events in Telerik UI for Blazor](slug:components/combobox/events)

0 commit comments

Comments
 (0)