Select
A select field that lets users pick one option from a list, with a search box to filter them and support for options loaded asynchronously.
Showcase
This is a helper message
This is a helper message
<LnxSelect v-model="..." <-- Pass a reactive variable here to bind its value :options="[{'label':'Option 1','value':'option-1'},{'label':'Option 2','value':'option-2'},{'label':'Option 3','value':'option-3'}]" label-property="label" :search-placeholder-text="undefined" @update:modelValue="..." <-- Two way binding of the component @query="..." <-- When the input search is written @select="..." <-- When an option is selected > Enter something <template #notFound> Nothing found </template> <template #error> This is an error message </template> <template #helper> This is a helper message </template> </LnxSelect>
v-model
UBinds the value of the select to the reactive variable used as prop Default value: undefinedconvertFn
(option: T) => T | UFunction to convert the value before emitting it. It receives the option and must return the converted value Default value: undefinedsearchPlaceholderText
stringThe placeholder text of the search input. By default, the `search` text of `createLnx` Default value: "Search..."loadingOptions
booleanIndicates if the options are being loaded asynchronously at the moment Default value: falsecustomValidity
stringThe error message of the input. It is the default value for the `error` slot Default value: undefinedWhen to use
- To pick one option from a list. The search box filters it as the user types, so long lists are fine.
- For free text, use an Input.
Examples
Text options
vue
<LnxSelect v-model="country" :options="['Spain', 'Portugal', 'France', 'Italy']" placeholder="Choose a country">
Country
</LnxSelect>1
2
3
2
3
Object options
label-property names the field shown for each option, and v-model gets the whole object. The search looks in every field of the options, ignoring case and accents.
vue
<LnxSelect v-model="assignee" :options="users" label-property="name">
Assignee
</LnxSelect>1
2
3
2
3
Custom options
The option slot decides how each option looks in the list.
vue
<LnxSelect v-model="reviewer" :options="users" label-property="name">
Reviewer
<template #option="{ option }">
<strong>{{ option.name }}</strong> · {{ option.role }}
</template>
</LnxSelect>1
2
3
4
5
6
2
3
4
5
6
Options from a server
With are-options-async, the select stops filtering by itself: listen to query, fetch the matching options and set loading-options while they load.
vue
<LnxSelect
v-model="city"
:options="cities"
are-options-async
:loading-options="isSearchingCities"
@query="searchCities"
>
City
</LnxSelect>1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
ts
async function searchCities(query: string) {
isSearchingCities.value = true;
cities.value = await api.getCities(query);
isSearchingCities.value = false;
}1
2
3
4
5
2
3
4
5
API
Props
| Prop | Type | Description |
|---|---|---|
v-model | U | Binds the value of the select to the reactive variable used as prop |
options | T[] | The options to be shown in the select |
convert-fn | (option: T) => T | U | Function to convert the value before emitting it. It receives the option and must return the converted value |
label-property | string | The property of the option to be shown as a label Default: "" |
search-placeholder-text | string | The placeholder text of the search input. By default, the search text of createLnxDefault: "Search..." |
are-options-async | boolean | Indicates if the options are loaded asynchronously Default: false |
loading-options | boolean | Indicates if the options are being loaded asynchronously at the moment Default: false |
is-loading | boolean | When loading, it is disabled and shows different content Default: false |
has-error | boolean | Indicates if it should show the error slot Default: false |
custom-validity | string | The error message of the input. It is the default value for the error slot |
Native attributes
| Attribute | Type | Description |
|---|---|---|
placeholder | string | Changes the placeholder of the input |
disabled | boolean | Disables all actions on the input Default: false |
Events
| Event | Payload | Description |
|---|---|---|
update:modelValue | T | Two way binding of the component |
query | string | When the input search is written |
select | T | When an option is selected |
Slots
| Slot | Scope | Description |
|---|---|---|
default | — | Label content of the input |
option | option: T | Custom content for each option |
optionInput | value: U | How the option will be shown in the input when selected |
notFound | — | What to display when no options are found |
loadingOptions | — | What will be shown when async options are being loaded |
icon | — | Content to be displayed at the right of the input value |
error | — | Message to be shown when the input has an error |
helper | — | Message to be shown below the input to add extra context |
CSS variables
| Variable | Description |
|---|---|
--lnx-select-list-color-bg | Background color of the options list dropdown |