# Select 选择器
下拉选择器,支持单选、多选、搜索和异步加载。
# 何时使用
- 弹出一个下拉菜单给用户选择操作,用于代替原生的选择器
- 当选项少时(少于 5 项),建议直接将选项平铺,使用 Radio 是更好的选择
# 代码演示
# API
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 当前选中的值 | string \| string[] \| object | null |
| options | 选项数据或异步加载函数 | Array \| (page, search) => Promise | [] |
| structure | 字段映射配置 | { label, value, icon, children } | { label: 'label', value: 'key' } |
| mode | 选择模式 | 'default' \| 'multiple' \| 'tags' | 'default' |
| labelInValue | 是否把选项的 label 包装到 value 中 | boolean | false |
| allowClear | 是否允许清除 | boolean | true |
| showSearch | 是否启用搜索 | boolean | true |
| placeholder | 选择框默认文字 | string | '请选择' |
| getPopupContainer | 菜单渲染父节点 | () => HTMLElement | () => document.body |
# 事件
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| change | 选中值变化时回调 | (value, source) => void |
# 异步加载
当 options 传入函数时,支持异步分页加载:
async function loadOptions(pageNum, searchValue) {
return [{ key: '1', label: '选项1' }]
// 或返回带分页的对象
return { data: [...], total: 100, res: true }
}
1
2
3
4
5
6
2
3
4
5
6