Skip to content

Select 选择器

下拉选择。

基础用法

设置 option 组件 value/label/disabled

控制台
桃子
查看代码
vue
<template>
	<demo-container class="gov-demo-select">
		<gov-select v-model="value">
			<gov-select-option
				v-for="option in options"
				:key="option.value"
				:label="option.label"
				:value="option.value"
				:disabled="option.disabled"
			/>
		</gov-select>
		<template #console>{{ value }}</template>
	</demo-container>
</template>

<script setup>
import { ref } from "vue";

const options = ref([
	{ label: "西瓜", value: "西瓜" },
	{ label: "桃子", value: "桃子" },
	{ label: "火龙果", value: "火龙果", disabled: true },
	{ label: "葡萄", value: "葡萄" },
	{ label: "橘子", value: "橘子" },
	{ label: "荔枝", value: "荔枝" },
]);

const value = ref(options.value[1].value); // 默认选中桃子
</script>

设置禁用

设置 disabled 后,组件展开的下来会自动关闭。

控制台
桃子
查看代码
vue
<template>
	<demo-container class="gov-demo-select">
		<gov-select v-model="value" disabled>
			<gov-select-option
				v-for="option in options"
				:key="option.value"
				:label="option.label"
				:value="option.value"
				:disabled="option.disabled"
			/>
		</gov-select>
		<template #console>{{ value }}</template>
	</demo-container>
</template>

<script setup>
import { ref } from "vue";

const options = ref([
	{ label: "西瓜", value: "西瓜" },
	{ label: "桃子", value: "桃子" },
	{ label: "火龙果", value: "火龙果", disabled: true },
	{ label: "葡萄", value: "葡萄" },
	{ label: "橘子", value: "橘子" },
	{ label: "荔枝", value: "荔枝" },
]);

const value = ref(options.value[1].value); // 默认选中桃子
</script>

禁用清空

默认带有清空按钮,你可以设置 clearfalse 来禁用清空。

控制台
桃子
查看代码
vue
<template>
	<demo-container class="gov-demo-select">
		<gov-select v-model="value" :clear="false">
			<gov-select-option
				v-for="option in options"
				:key="option.value"
				:label="option.label"
				:value="option.value"
				:disabled="option.disabled"
			/>
		</gov-select>
		<template #console>{{ value }}</template>
	</demo-container>
</template>

<script setup>
import { ref } from "vue";

const options = ref([
	{ label: "西瓜", value: "西瓜" },
	{ label: "桃子", value: "桃子" },
	{ label: "火龙果", value: "火龙果", disabled: true },
	{ label: "葡萄", value: "葡萄" },
	{ label: "橘子", value: "橘子" },
	{ label: "荔枝", value: "荔枝" },
]);

const value = ref(options.value[1].value); // 默认选中桃子
</script>

设置尺寸

根据 size 设置不同尺寸。

查看代码
vue
<template>
	<demo-container class="gov-demo-select">
		<gov-select v-model="value" size="large">
			<gov-select-option
				v-for="option in options"
				:key="option.value"
				:label="option.label"
				:value="option.value"
				:disabled="option.disabled"
			/>
		</gov-select>
		<br /><br />
		<gov-select v-model="value">
			<gov-select-option
				v-for="option in options"
				:key="option.value"
				:label="option.label"
				:value="option.value"
				:disabled="option.disabled"
			/>
		</gov-select>
		<br /><br />
		<gov-select v-model="value" size="small">
			<gov-select-option
				v-for="option in options"
				:key="option.value"
				:label="option.label"
				:value="option.value"
				:disabled="option.disabled"
			/>
		</gov-select>
	</demo-container>
</template>

<script setup>
import { ref } from "vue";

const options = ref([
	{ label: "西瓜", value: "西瓜" },
	{ label: "桃子", value: "桃子" },
	{ label: "火龙果", value: "火龙果", disabled: true },
	{ label: "葡萄", value: "葡萄" },
	{ label: "橘子", value: "橘子" },
	{ label: "荔枝", value: "荔枝" },
]);

const value = ref(null); // 默认选中桃子
</script>

Select-option Attributes

属性名说明类型可选值默认值
value选项的值String/Numberundefined
label选项的显示文本String/Numberundefined
disabled是否禁用选项Booleantrue, falsefalse

Select-option Slots

插槽名说明
default用于自定义选项显示的内容,可以放置如文本或 HTML 结构,默认为 label

Select Attributes

属性名说明类型可选值默认值
modelValue绑定的值String/Numberundefined
size输入框尺寸String
placeholder占位符String"请输入"
disabled是否禁用Booleantrue, falsefalse
clearable是否可清空Booleantrue, falsetrue
popperHeightpopper 组件的高度String"200px"
triggerForm是否触发表单验证(可能会被弃用)Booleantrue, falsetrue

Select Events

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

Select Slots

插槽名说明
default用于放置下拉选项内容,可以放置 GovSelectOption 组件实例。

Released under the MIT License.