Skip to content

InputAuto 自动补全输入框

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

基本用法

使用 v-model 绑定值;使用 fetch 作为输入时数据过滤函数。

查看代码
vue
<template>
	<demo-container class="gov-demo-inputauto">
		<gov-input-auto v-model="value" :fetch="querySearch" />
	</demo-container>
</template>

<script setup>
import { ref } from "vue";
import { fruits } from "./data.js";

const value = ref("");
const querySearch = (str) => {
	return fruits.filter((el) => el.toLowerCase().includes(str.toLowerCase()));
};
</script>
js
export const fruits = [
	"Apple",
	"Banana",
	"Cherry",
	"Date",
	"Elderberry",
	"Fig",
	"Grape",
	"Honeydew",
	"Kiwi",
	"Lemon",
	"Mango",
	"Nectarine",
	"Orange",
	"Papaya",
	"Quince",
	"Raspberry",
	"Strawberry",
	"Tomato",
	"Ugli fruit",
	"Vanilla",
	"Watermelon",
	"Xigua",
	"Yumberry",
	"Zucchini",
];

export const fruitInfos = fruits.map((item) => ({
	value: item,
	label: item.toUpperCase(),
}));

自定义模板

可以在默认插槽内书写渲染模板。

  • fetch 可以返回字符串集合或数据对象集合。
  • 当返回对象集合时,valueKey 可指定用于 Input 的字段,默认为字符串 value
查看代码
vue
<template>
	<demo-container class="gov-demo-inputauto">
		<gov-input-auto v-model="value" :fetch="querySearch">
			<template #default="{ item }">
				<div>大写:{{ item.value.toUpperCase() }}</div>
				<div style="font-size: 12px; color: #666">
					正常:<i>{{ item.value }}</i>
				</div>
			</template>
		</gov-input-auto>
	</demo-container>
</template>

<script setup>
import { ref } from "vue";
import { fruitInfos } from "./data.js";

const value = ref("");
const querySearch = (str) => {
	return fruitInfos.filter((el) =>
		el.value.toLowerCase().includes(str.toLowerCase()),
	);
};
</script>
js
export const fruits = [
	"Apple",
	"Banana",
	"Cherry",
	"Date",
	"Elderberry",
	"Fig",
	"Grape",
	"Honeydew",
	"Kiwi",
	"Lemon",
	"Mango",
	"Nectarine",
	"Orange",
	"Papaya",
	"Quince",
	"Raspberry",
	"Strawberry",
	"Tomato",
	"Ugli fruit",
	"Vanilla",
	"Watermelon",
	"Xigua",
	"Yumberry",
	"Zucchini",
];

export const fruitInfos = fruits.map((item) => ({
	value: item,
	label: item.toUpperCase(),
}));

Attributes

基于 InputPopper 组件,更多内容不再赘述。

属性名说明类型可选值默认值
modelValue绑定在输入框的值Stringundefined
fetch获取数据的函数,返回一个数组Function
valueKey显示在input里的值的键名String"value"
popperHeightpopper 组件的高度String"200px"
size输入框尺寸String
triggerForm是否触发表单验证Booleantrue, falsetrue

Events

事件名说明回调参数
update:modelValue绑定值更新时触发新的值
input输入时触发输入的值
change值改变时触发新的值
select选择列表中的一项时触发选中的项
focus输入框获得焦点时触发
blur输入框失去焦点时触发
clear清空按钮点击时触发

Slots

插槽名说明
addonBefore前置标签内容;和 Input 组件同理。
addonAfter后置标签内容;和 Input 组件同理。
prefix输入框前缀内容;和 Input 组件同理。
suffix输入框后缀内容;和 Input 组件同理。
default列表项内容,可以放置自定义的内容,用于渲染列表项。

Released under the MIT License.