自动补全输入框

根据输入内容提供对应的输入建议。

基础用法

Autocomplete 组件提供输入建议。

fetch-suggestions 属性是返回建议输入的方法。在此示例中, querySearch(queryString, cb) 方法通过 cb(data) 给 Autocomplete 组件返回建议。

list suggestions when activated
list suggestions on input

自定义模板

自定义如何显示输入建议。

使用 scoped slot 自定义输入建议。在这个范围中,你可以使用 item 键来访问当前输入建议对象。

远程搜索

从服务端搜索数据。

自定义加载 2.5.0

修改加载区域内容

loading icon1
loading icon2

自定义头部与底部 2.10.6

你可以通过插槽来自定义下拉菜单的头部和底部。

使用插槽自定义内容。

Custom header content

Custom footer content

Autocomplete API

Autocomplete Attributes

属性名说明类型默认值
value绑定值string / number''
placeholder占位文本string—
clearable是否可清空booleanfalse
disabled是否禁用booleanfalse
read-only原生 readonlybooleanfalse
size尺寸enum—
name同原生 input 的 namestring—
aria-label原生 aria-labelstring—
value-key显示与回填用的字段名stringvalue
debounce输入防抖延迟(毫秒)number300
placement下拉方位enumbottom-start
fetch-suggestions拉取建议,返回数组或调用 cbarray / Function—
trigger-on-focus聚焦时是否拉取建议booleantrue
select-when-unmatched无匹配时回车是否派发 selectbooleanfalse
hide-loading远程搜索是否隐藏 loading 图标booleanfalse
popper-class下拉附加类名string''
popper-style下拉附加样式string / object—
popper-optionspopper.js 配置object{}
show-arrow下拉是否显示箭头booleantrue
teleported下拉是否挂在 body 上booleantrue
append-to下拉的挂载目标(选择器或元素)string / HTMLElement—
highlight-first-item是否默认高亮第一条booleanfalse
fit-input-width下拉宽度是否与输入框一致booleanfalse
loop-navigation键盘导航是否回绕booleantrue
prefix / suffix / prepend / appendInput 前后缀内容ReactNode—

Autocomplete Events

事件名说明参数
onChange输入值变化Function
onValueChange失焦或回车时触发Function
onSelect点击某条建议Function
onFocus输入框聚焦Function
onBlur输入框失焦Function
onClear点击清空图标Function

Autocomplete Slots

插槽名说明React 对应
default自定义建议项renderItem(item)
header下拉顶部内容header
footer下拉底部内容footer
loading覆盖加载内容loading
prefix / suffix / prepend / appendInput 前后缀同名 prop

Autocomplete Exposes

通过 ref 拿到实例,暴露 focus / blur / close / handleSelect / handleKeyEnter / highlight / getData,以及只读的 activated、 loading、highlightedIndex、suggestions(官方是 Ref,这里用 getter 暴露当前值)。 官方 inputRef / popperRef 对应为 input(原生 input 元素)与 popper(浮层容器元素)。