事件处理
事件处理
SunlightSelect 支持多种事件处理,包括选择变化、焦点、失焦、清除、下拉框显示/隐藏和搜索事件。
基础事件处理
您可以监听选择器的各种事件,实现自定义的业务逻辑。
基础事件处理示例
选择变化事件
change事件:选择值变化时触发
选择变化事件示例
焦点与失焦事件
focus事件:选择器获得焦点时触发blur事件:选择器失去焦点时触发
焦点与失焦事件示例
清除事件
clear事件:点击清除按钮时触发
清除事件示例
下拉框显示/隐藏事件
visible-change事件:下拉框显示或隐藏时触发
下拉框显示/隐藏事件示例
搜索事件
search事件:输入搜索关键词时触发(仅在 filterable 为 true 时生效)
搜索事件示例
文档说明
事件处理概述
SunlightSelect 支持多种事件,涵盖了选择、焦点、下拉框状态和搜索等各种交互场景,方便开发者实现复杂的业务逻辑。
支持的事件列表
| 事件名 | 说明 | 触发时机 | 参数 |
|---|---|---|---|
| update:modelValue | 绑定值更新事件,用于双向数据绑定 | 选择值变化时 | val: string/number/array - 更新后的值 |
| change | 选择变化事件 | 选择值变化时 | val: string/number/array - 选择后的值 |
| focus | 焦点事件 | 选择器获得焦点时 | event: Event - 原生事件对象 |
| blur | 失焦事件 | 选择器失去焦点时 | event: Event - 原生事件对象 |
| clear | 清除事件 | 点击清除按钮时 | event: Event - 原生事件对象 |
| visible-change | 下拉框显示/隐藏切换事件 | 下拉框显示或隐藏时 | val: boolean - 下拉框是否可见 |
| search | 搜索事件(仅在 filterable 为 true 时触发) | 输入搜索关键词时 | val: string - 搜索关键词 |
事件使用方式
SunlightSelect 组件支持两种事件绑定方式:直接通过 v-on 绑定和通过 eventFunction 配置项绑定。
1. 直接通过 v-on 绑定事件
vue
<template>
<SunlightSelect
v-model="selectValue"
:item="{
placeholder: '请选择选项',
options: options,
clearable: true
}"
@change="handleChange"
@focus="handleFocus"
@blur="handleBlur"
/>
</template>
<script setup>
import { ref } from 'vue';
import { SunlightSelect } from 'sunlight-ui';
import { ElMessage } from 'element-plus';
const selectValue = ref('');
const options = [
{ label: '选项1', value: 'option1' },
{ label: '选项2', value: 'option2' },
{ label: '选项3', value: 'option3' }
];
function handleChange(val) {
ElMessage({
message: 'change事件已触发',
type: 'success',
});
}
function handleFocus(event) {
ElMessage({
message: 'focus事件已触发',
type: 'success',
});
}
function handleBlur(event) {
ElMessage({
message: 'blur事件已触发',
type: 'success',
});
}
</script>2. 通过 eventFunction 配置项绑定事件
vue
<template>
<SunlightSelect
v-model="selectValue"
:item="{
placeholder: '通过 eventFunction 绑定事件',
options: options,
clearable: true,
eventFunction: {
change: (val) => {
ElMessage({
message: 'change事件已触发',
type: 'success',
});
},
focus: (event) => {
ElMessage({
message: 'focus事件已触发',
type: 'success',
});
},
blur: (event) => {
ElMessage({
message: 'blur事件已触发',
type: 'success',
});
}
}
}"
/>
</template>
<script setup>
import { ref } from 'vue';
import { SunlightSelect } from 'sunlight-ui';
import { ElMessage } from 'element-plus';
const selectValue = ref('');
const options = [
{ label: '选项1', value: 'option1' },
{ label: '选项2', value: 'option2' },
{ label: '选项3', value: 'option3' }
];
</script>3. 两种方式结合使用
直接绑定的事件和通过 eventFunction 配置的事件可以同时使用,都会被触发。
vue
<template>
<SunlightSelect
v-model="selectValue"
:item="{
placeholder: '两种方式结合使用',
options: options,
clearable: true,
eventFunction: {
change: (val) => {
ElMessage({
message: 'eventFunction - change事件已触发',
type: 'success',
});
}
}
}"
@change="(val) => {
ElMessage({
message: '直接绑定 - change事件已触发',
type: 'success',
});
}"
/>
</template>
<script setup>
import { ref } from 'vue';
import { SunlightSelect } from 'sunlight-ui';
import { ElMessage } from 'element-plus';
const selectValue = ref('');
const options = [
{ label: '选项1', value: 'option1' },
{ label: '选项2', value: 'option2' },
{ label: '选项3', value: 'option3' }
];
</script>