Autocomplete
Demos
Basic Trigger
Set suggest-trigger
specify trigger suggestions.
suggest on input
suggest on focus
<template>
<article class="autocomplete-normal-demo">
<section>
<h3>suggest on input</h3>
<veui-autocomplete
:datasource="suggestions"
placeholder="请输入"
clearable
/>
</section>
<section>
<h3>suggest on focus</h3>
<veui-autocomplete
:datasource="coffees"
placeholder="请输入"
suggest-trigger="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
Set strict
property to strict mode. If the input value does not exactly match the recommended value, the input value will be cleared when the focus is out of focus.
<template>
<article class="autocomplete-normal-demo">
<section>
<veui-autocomplete
:datasource="suggestions"
placeholder="请输入"
suggest-trigger="focus"
strict
/>
</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'
}
]
}
}
}
</script>
<style lang="less" scoped>
.autocomplete-normal-demo {
display: flex;
section + section {
margin-left: 60px;
}
}
</style>
Custom Search
Set match
property to customize the highlighting logic; Set filter
property to customize the search hit logic.
大小写敏感搜索叶子节点
<template>
<article class="autocomplete-normal-demo">
<section>
<h3>大小写敏感搜索叶子节点</h3>
<veui-autocomplete
:datasource="suggestions"
placeholder="请输入"
suggest-trigger="focus"
:match="match"
:filter="filter"
/>
</section>
</article>
</template>
<script>
import { Autocomplete } from 'veui'
export default {
components: {
'veui-autocomplete': Autocomplete
},
data () {
return {
suggestions: [
{
label: 'Milk coffee',
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>= | [] | Data source array, item is
| ||||||||||||
value | * | - |
Selected value. | ||||||||||||
disabled | boolean= | false | Whether disabled state. | ||||||||||||
readonly | boolean= | false | Whether read only status. | ||||||||||||
match | (item, keyword, { ancestors }) => boolean | [number, number] | Array<[number, number]> | - | Supports custom highlighting logic, and matches case-insensitive substrings by default. | ||||||||||||
filter | (item, keyword, { ancestors, offsets }) => boolean | - | Support custom search hit logic. | ||||||||||||
suggest-trigger | string | Array<string>= | 'input' | Trigger the suggestion drop-down panel, the available values are: 'input' , 'focus' . | ||||||||||||
expanded | boolean= | false |
Whether the panel should be expanded (if there is no option in | ||||||||||||
placeholder | string= | - | Placeholder. | ||||||||||||
clearable | boolean= | false | Whether clear button is displayed. | ||||||||||||
composition | boolean= | false | Whether perceive value of input method input process. | ||||||||||||
select-on-focus | boolean= | false | Whether to automatically select the input box text when focused. | ||||||||||||
maxlength | number= | - | The length of the input string is limited. | ||||||||||||
strict | boolean= | false | Maximum input length. | ||||||||||||
get-length | function(string): number= | Customize character length calculation function. | |||||||||||||
trim | boolean | string= | false | Remove before and after spaces. When it is
| ||||||||||||
autofocus | boolean= | false | Focus automatically. |
Slots
Name | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
suggestions | Pull down to suggest the panel slot.
| ||||||||||||
option-label | Option slot in the drop down panel.
|
Events
Name | Description |
---|---|
input |
Event will be triggered when inputting in |
select | Triggered when suggestion is adopted, the parameter is current value. |
toggle | Triggered when expanded state of prompt panel is switched, callback parameter is (expanded: boolean) . expanded indicates whether operation will trigger prompt panel to expand or collapse. |
clear | Triggers when current value is cleared. |