Autocomplete
Examples
Suggestion trigger
Set the suggest-trigger
prop to specify when to trigger suggestions.
Suggest on input
Suggest on focus
<template>
<article class="autocomplete-normal-demo">
<section>
<h3>Suggest on input</h3>
<veui-autocomplete
:datasource="suggestions"
clearable
/>
</section>
<section>
<h3>Suggest on focus</h3>
<veui-autocomplete
:datasource="coffees"
suggest-on-focus="focus"
/>
</section>
</article>
</template>
<script>
import { Autocomplete } from 'veui'
export default {
components: {
'veui-autocomplete': Autocomplete
},
data () {
return {
suggestions: [
{
value: 'Moka'
},
{
value: 'Turkish'
},
{
value: 'Latte'
},
{
value: 'Cappuccino'
}
],
coffees: [
{
label: 'Infused',
value: 'infused',
options: [
{
label: 'French press',
value: 'french-press'
},
{
label: 'Cold brew',
value: 'cold-brew'
}
]
},
{
label: 'Espresso',
value: 'espresso',
options: [
{
label: 'Espresso Romano',
value: 'espresso-romano'
},
{
label: 'Guillermo',
value: 'guillermo'
},
{
label: 'Ristretto',
value: 'ristretto'
}
]
},
{
label: 'Milk coffee',
value: 'milk-coffee',
options: [
{
label: 'Latte',
value: 'latte'
},
{
label: 'Macchiato',
value: 'macchiato'
},
{
label: 'Cappuccino',
value: 'cappuccino'
},
{
label: 'White coffee',
value: 'white-coffee'
}
]
}
]
}
}
}
</script>
<style lang="less" scoped>
.autocomplete-normal-demo {
display: flex;
section + section {
margin-left: 60px;
}
}
</style>
Strict mode
Set the strict
prop to enable strict mode. { maxlength?: boolean }
is used to specify whether the input length is strictly limited and will be automatically truncated if exceeded; { select?: boolean }
is used to specify whether the input item must match exactly one of the search suggestions. If no match is found, the input will be automatically cleared when it loses focus.
<template>
<article class="autocomplete-normal-demo">
<section>
<veui-checkbox v-model="strict.maxlength">
Strict maxlength
</veui-checkbox>
<veui-checkbox v-model="strict.select">
Strict select
</veui-checkbox>
</section>
<section>
<veui-autocomplete
:datasource="suggestions"
:maxlength="6"
:strict="strict"
/>
</section>
</article>
</template>
<script>
import { Autocomplete, Checkbox } from 'veui'
export default {
components: {
'veui-autocomplete': Autocomplete,
'veui-checkbox': Checkbox
},
data () {
return {
strict: {
maxlength: false,
select: true
},
suggestions: [
{
value: 'Moka'
},
{
value: 'Turkish'
},
{
value: 'Latte'
},
{
value: 'Cappuccino'
}
]
}
}
}
</script>
<style lang="less" scoped>
section {
display: flex;
align-items: center;
gap: 12px;
& + & {
margin-top: 20px;
}
}
</style>
Custom search logic
Set the match
prop to customize highlight logic and the filter
prop to customize search hit logic.
大小写敏感搜索叶子节点
<template>
<article class="autocomplete-normal-demo">
<section>
<h3>大小写敏感搜索叶子节点</h3>
<veui-autocomplete
:datasource="suggestions"
:match="match"
:filter="filter"
/>
</section>
</article>
</template>
<script>
import { Autocomplete } from 'veui'
export default {
components: {
'veui-autocomplete': Autocomplete
},
data () {
return {
suggestions: [
{
value: 'Milk coffee',
options: [
{
value: 'Moka'
}
]
},
{
value: 'Turkish'
},
{
value: 'Latte'
},
{
value: 'Cappuccino'
}
]
}
},
methods: {
match ({ label }, keyword) {
const index = label.indexOf(keyword)
return index >= 0
? [[index, index + keyword.length]]
: false
},
filter ({ options }, _, { offsets }) {
// 不要父节点,只要叶子节点
return options && options.length
? false
: offsets === true || (!!offsets && !!offsets.length)
}
}
}
</script>
<style lang="less" scoped>
.autocomplete-normal-demo {
display: flex;
section + section {
margin-left: 60px;
}
}
</style>
API
Props
Name | Type | Default | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
datasource | Array<string | Object> | [] | An array of data sources. If an item is an object, it should have the following properties:
| ||||||||||||
value | * | - | The value of the input.
The currently selected value. | ||||||||||||
disabled | boolean | false | Whether the input is disabled. | ||||||||||||
readonly | boolean | false | Whether the input is read-only. | ||||||||||||
match | (item, keyword, { ancestors }) => boolean | [number, number] | Array<[number, number]> | - | Supports custom highlighting logic. The default is a case-insensitive substring match. | ||||||||||||
filter | (item, keyword, { ancestors, offsets }) => boolean | - | Supports custom search hit logic. | ||||||||||||
suggest-trigger | string | Array<string> | 'input' | Specifies when to trigger the suggestion dropdown panel. Valid values are 'input' and 'focus' . | ||||||||||||
expanded | boolean | false | Whether the suggestion panel is expanded.
Whether the suggestion panel is expanded. If there are no options in | ||||||||||||
placeholder | string | - | The placeholder text. | ||||||||||||
clearable | boolean | false | Whether to show the clear button. | ||||||||||||
composition | boolean | false | Whether to perceive the value during the input method editor (IME) input process. | ||||||||||||
autofocus | boolean | false | Whether to automatically focus the input. | ||||||||||||
select-on-focus | boolean | false | Whether to automatically select the input box text when focused. | ||||||||||||
maxlength | number | - | The maximum length of the input string. | ||||||||||||
strict | boolean | Object | false | When
| ||||||||||||
get-length | function(string): number | A custom function to calculate the length of characters. | |||||||||||||
trim | boolean | string | false | Whether to remove leading and trailing spaces. When
| ||||||||||||
overlay-class | string | Array | Object | - | Refers to the overlay-class prop of the Overlay component. | ||||||||||||
overlay-style | string | Array | Object | - | Refers to the overlay-style prop of the Overlay component. |
Slots
Name | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
suggestions | Slot for the dropdown suggestion panel.
| ||||||||||||
option-label | Slot for each option in the dropdown panel.
| ||||||||||||
no-data | Content to be displayed when there are no matching options. By default, the dropdown menu is closed when there are no options.
|
Events
Name | Description |
---|---|
input |
Triggered when the input value changes or a value is selected from the dropdown panel. The parameter is the current input value or the |
select | Triggered when a suggestion is accepted, with the current value as the parameter. |
toggle | Triggered when the suggestion panel toggles its expanded state, with (expanded: boolean) as the callback parameter. expanded indicates if the panel will be expanded or collapsed after the operation. |
clear | Triggered when the current value is cleared. |
CSS
Name | Type | Default | Description |
---|---|---|---|
--dls-dropdown-max-display-items | <integer> | 8 | The maximum number of items to display simultaneously in the dropdown options. The maximum height of the dropdown will be calculated based on this value. |