Skip to content

Commit 9e7fa69

Browse files
docs(combobox): info for onchange and selected items
1 parent 25c80d6 commit 9e7fa69

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

components/combobox/events.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ position: 20
1313
This article explains the events available in the Telerik ComboBox for Blazor:
1414

1515
* [ValueChanged](#valuechanged)
16-
* `OnChange` - inherited event that you should not use, but you may see in the intellisense
16+
* [OnChange](#onchange)
1717

1818

1919
## ValueChanged
@@ -94,6 +94,81 @@ from model: @MyItem
9494
}
9595
````
9696

97+
98+
## OnChange
99+
100+
The `OnChange` event is suitable for handling custom values the user can enter as if the combo box were an input. The key differences with `ValueChanged` are:
101+
102+
* `OnChange` does not prevent two-way binding (the `@bind-Value` syntax)
103+
* `OnChange` fires only when the user presses `Enter`, or blurs the input (for example, clicks outside of the combo box). It does not fire on every keystroke, even when `AllowCustom="true"`, and it does not fire when an item is selected from the dropdown.
104+
105+
See the [ComboBox Overview - Selected Item]({%slug components/combobox/overview%}#selected-item) article for details on when the event fires and how item selection works.
106+
107+
>caption Handle OnChange without custom values - you must write text that will match an item.
108+
109+
````CSHTML
110+
@result
111+
<br />
112+
@selectedValue
113+
<br /><br />
114+
<TelerikComboBox Data="@myComboData" TextField="MyTextField" ValueField="MyValueField"
115+
@bind-Value="@selectedValue" OnChange="@MyOnChangeHandler">
116+
</TelerikComboBox>
117+
118+
@code {
119+
string result;
120+
int selectedValue { get; set; } = 3;
121+
122+
private void MyOnChangeHandler(object theUserInput)
123+
{
124+
// the handler receives an object that you may need to cast to the type of the component
125+
// if you do not provide a Value, you must provide the Type parameter to the component
126+
result = string.Format("The user entered: {0}", (int)theUserInput);
127+
}
128+
129+
public class MyComboModel
130+
{
131+
public int MyValueField { get; set; }
132+
public string MyTextField { get; set; }
133+
}
134+
135+
IEnumerable<MyComboModel> myComboData = Enumerable.Range(1, 20).Select(x => new MyComboModel { MyTextField = "item " + x, MyValueField = x });
136+
}
137+
````
138+
139+
>caption Handle OnChange with custom values - the event fires on blur or enter
140+
141+
````CSHTML
142+
@result
143+
<br />
144+
@selectedValue
145+
<br /><br />
146+
<TelerikComboBox Data="@myComboData" TextField="MyTextField" ValueField="MyValueField"
147+
@bind-Value="@selectedValue" OnChange="@MyOnChangeHandler" AllowCustom="true">
148+
</TelerikComboBox>
149+
150+
@code {
151+
string result;
152+
string selectedValue { get; set; } = "3";
153+
154+
private void MyOnChangeHandler(object theUserInput)
155+
{
156+
// the handler receives an object that you may need to cast to the type of the component
157+
// if you do not provide a Value, you must provide the Type parameter to the component
158+
result = string.Format("The user entered: {0}", (string)theUserInput);
159+
}
160+
161+
public class MyComboModel
162+
{
163+
public string MyValueField { get; set; }
164+
public string MyTextField { get; set; }
165+
}
166+
167+
IEnumerable<MyComboModel> myComboData = Enumerable.Range(1, 20).Select(x => new MyComboModel { MyTextField = "item " + x, MyValueField = x.ToString() });
168+
}
169+
````
170+
171+
97172
## See Also
98173

99174
* [ValueChanged and Validation]({%slug value-changed-validation-model%})

components/combobox/overview.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,27 @@ The ComboBox is a generic component and its type is determined by the type of th
8080
* Validation - see the [Input Validation]({%slug common-features/input-validation%}) article for more details.
8181

8282

83-
>note If the initial `Value` is not present in the data source, the component will **not** select the first item of the data source - there will simply be no selection. For such cases you may want to set `AllowCustom="true"`. This scenario is most common when the initial value is `null` as data sources rarely have items with a `null` value, and when you want to let your users type in values that are not in your predefined set of options.
83+
## Selected Item
84+
85+
By default, if no `Value` is provided, the first item from the data source will be selected. If a `Value` is provided, the first item from the data source whose `ValueField` matches will be selected.
86+
87+
The ComboBox will not always have a selected item, however, because it can act as an input. There will be no selected item in the following cases that depend on the settings of the component that the developer can control:
88+
89+
* the initial `Value` is not present in the data source,
90+
* the user clears the value through the Clear button,
91+
* `AllowCustom="true"` - only initial selection is possible, through the `Value` parameter and user actions remove it (see the table below).
92+
93+
94+
Missing selection is most common when the initial value is `null` as data sources rarely have items with a `null` value, and/or when you want to let your users type in values that are not in your predefined set of options.
95+
96+
If the user types text in the input, selection behaves according to the following table:
97+
98+
99+
| User input matches an item | AllowCustom=`true` | AllowCustom=`false` |
100+
|----------------------------|----------------------|------------------------------------------|
101+
| Yes | No item is selected. | Matching item is selected. |
102+
| No | No item is selected. | If there was no previous selection, no item is selected. If there was previous selection, the previously selected item is selected. The `OnChange` event does not fire. |
103+
84104

85105

86106
## See Also

0 commit comments

Comments
 (0)