Search Mixin
SearchMixin
Strongly typed mixin for asking the server for values that match a string the user types in and displaying those matches for the user to choose from.
How to use this mixin
SearchMixin gives a widget “ask the server for values that match a string the user types in,
then let them choose from the results” behaviour - it does not render anything on its own.
1. Extend
export class MySearchingWidget extends SearchMixin(Et2InputWidget(LitElement))
{
// ...
}
2. Override searchResultSelected()
This is called when the user picks a result. Call super.searchResultSelected() first, then
update this.value from this.selectedResults:
protected searchResultSelected()
{
super.searchResultSelected();
this.value = this.selectedResults[0].value;
}
Other methods can be overridden if needed.
3. Call the two template methods from render()
render()
{
return html`
${this.searchInputTemplate()}
${this.searchResultsTemplate()}
`;
}
Example
Et2TreeDropdown is a real, working example - a
tree picker that searches the tree for matching nodes as you type. Simplified to the parts that matter for
SearchMixin specifically:
export class Et2TreeDropdown extends SearchMixin(Et2WidgetWithSelectMixin(LitElement))
{
protected searchResultSelected()
{
super.searchResultSelected();
// this.selectedResults holds whatever the server returned for the chosen result(s)
if (this.multiple)
{
this.value = [...new Set([...this.value, ...this.selectedResults.map(el => el.value)])];
}
else
{
this.value = this.selectedResults[0].value;
}
}
render()
{
return html`
${this.searchInputTemplate()}
${this.searchResultsTemplate()}
`;
}
}
Listen for the et2-select event on the widget if you need to react to selection changes from
the outside, rather than only inside searchResultSelected().
Used by
This mixin is applied by the following widgets.
Properties
| Name | Description | Type | Default |
|---|---|---|---|
resultsOpen |
Indicates whether the search results are open. You can toggle this attribute to show and hide the results list. |
boolean
|
false
|
search |
Enable or disable searching |
boolean
|
true
|
searchOptions |
Additional search parameters that are passed to the server when we query searchUrl |
object
|
{}
|
searchUrl |
Get [additional] options from the server when you search, instead of just searching in the browser |
string
|
""
|
Methods
| Name | Description | Arguments |
|---|---|---|
searchMatch()
|
Check if one of our [local] items matches the search |
search: string, searchOptions: Object, option: DataType
|
startSearch()
|
Start searching for results matching what has been typed | - |