Skip to content

Select 选择器

选择器封装,和z-form组件配合食用,风味更佳。

基础用法

传入options自动生成选项

<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  { label: '选项一', value: 1 },
  { label: '选项二', value: 2 },
  { label: '选项三', value: 3 },
])

const selectVal = ref(1)

const handleSelectChange = (val) => {
  console.log(val, 'handleSelectChange')
}
</script>

<template>
  <z-select v-model="selectVal" :options="options" @change="handleSelectChange" />
</template>

禁用

option中的某项设置disabledtrue

<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  { label: '选项一', value: 1 },
  { label: '选项二', value: 2, disabled: true },
  { label: '选项三', value: 3 },
])

const selectVal = ref(1)
</script>

<template>
  <z-select v-model="selectVal" :options="options" />
</template>

全部禁用,组件传入disabledtrue

<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  { label: '选项一', value: 1 },
  { label: '选项二', value: 2 },
  { label: '选项三', value: 3 },
])

const selectVal = ref(1)
</script>

<template>
  <z-select v-model="selectVal" :disabled="true" :options="options" />
</template>

键值对配置

配置alias,自定义labelvaluedisabled的键名

<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  { data: { title: '选项一' }, key: 1 },
  { data: { title: '选项二' }, key: 2 },
  { data: { title: '选项三' }, key: 3 },
])

const alias = {
  label: 'data.title',
  value: 'key',
}

const selectVal = ref(1)
</script>

<template>
  <z-select v-model="selectVal" :alias="alias" :options="options" />
</template>

多选

multipletrue 时,启用多选。此时绑定的 model-value 为数组格式

<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  { label: '选项一', value: 1 },
  { label: '选项二', value: 2 },
  { label: '选项三', value: 3 },
])

const selectVal = ref([1])
</script>

<template>
  <div class="flex flex-col">
    <z-select v-model="selectVal" :options="options" multiple />
    <z-select
      v-model="selectVal"
      class="mt-4"
      :options="options"
      collapse-tags
      collapse-tags-tooltip
      multiple
    />
  </div>
</template>

选择全部

TIP

前提 multiple 为 true 时,才会生效

alltrue 时,options会拼接{ label: '全部', value: 'all' }

<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  { label: '选项一', value: 1 },
  { label: '选项二', value: 2 },
  { label: '选项三', value: 3 },
])

const selectVal = ref([1])
</script>

<template>
  <div class="flex flex-col">
    <z-select v-model="selectVal" :options="options" multiple all />
  </div>
</template>

分组

通过 option 中的 options 字段配置可以轻松生成分组展示

<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  {
    label: '选项一',
    options: [
      { label: '选项一-1', value: 11 },
      { label: '选项一-2', value: 12 },
    ],
  },
  {
    label: '选项二',
    options: [
      { label: '选项二-1', value: 21 },
      { label: '选项二-2', value: 22 },
    ],
  },
  {
    label: '选项三',
    disabled: true,
    options: [
      { label: '选项三-1', value: 31 },
      { label: '选项三-2', value: 32 },
    ],
  },
])

const selectVal = ref(11)
</script>

<template>
  <z-select v-model="selectVal" :options="options" />
</template>

内容自定义

option项配置render函数即可自定义内容,或者render字段传入自定义字符 + Slot使用插槽功能

<script lang="ts" setup>
import { ref } from 'vue'
import type { OptionsItem } from 'ideaz-element'

const options = ref([
  {
    label: '选项一',
    value: 1,
    render: ({ option }: OptionsItem) => {
      return h('span', {}, `自定义${option.value}`)
    },
  },
  {
    label: '选项二',
    value: 2,
    render: 'optionSlot',
  },
  { label: '选项三', value: 3 },
])

const selectVal = ref(1)
</script>

<template>
  <z-select v-model="selectVal" :options="options">
    <template #optionSlot="{ option }">
      <span>自定义{{ option.label }}</span>
    </template>
  </z-select>
</template>

插槽

如果想要自定义组件头部内容或无选项时的列表,配置prefixempty属性,或者使用prefixempty插槽

<script lang="ts" setup>
import { h, ref, resolveComponent } from 'vue'

const options = ref([])

const selectVal = ref()

const setHeader = () => {
  return h(resolveComponent('el-icon'), {}, () => h(resolveComponent('i-search')))
}

const setEmpty = () => {
  return h('div', {}, '自定义内容')
}
</script>

<template>
  <div class="flex flex-col">
    <z-select v-model="selectVal" :options="options" :prefix="setHeader" :empty="setEmpty" />
    <z-select v-model="selectVal" :options="options" prefix="header" empty="自定义内容" class="mt-4" />
    <z-select v-model="selectVal" :options="options" class="mt-4">
      <template #prefix>
        <span>头部</span>
      </template>
      <template #empty>
        <span>自定义内容</span>
      </template>
    </z-select>
  </div>
</template>

支持tagloadingheaderfooterlabel等插槽

<script lang="ts" setup>
import { ref } from 'vue'
import type { OptionsItem } from 'ideaz-element'

const options = ref([
  { label: '选项一', value: 1, render: ({ option }: { option: OptionsItem }) => h('span', `${option.label}${option.value}`) },
  { label: '选项二', value: 2 },
  { label: '选项三', value: 3 },
])

const selectVal = ref()
</script>

<template>
  <div class="flex flex-col">
    <z-select v-model="selectVal" :options="options" class="mt-4">
      <template #header>
        <span>头部</span>
      </template>
      <template #footer>
        <span>自定义内容</span>
      </template>
      <template #label="{ label, value }">
        <span>{{ label }}: </span>
        <span style="font-weight: bold">{{ value }}</span>
      </template>
    </z-select>
  </div>
</template>

z-select 属性

属性名说明类型可选值默认值
model-value / v-model选中项绑定值array / string / number / boolean / object
options可配置项array
alias键值对配置object{ label: 'label', value: 'value', disabled: 'disabled' }
prefixSelect 组件头部内容string /function
empty无选项时的列表string / function
multiple是否多选booleantrue/falsefalse
all是否全部(multipletrue时才会生效)booleantrue/falsefalse
disabled是否禁用booleantrue / falsefalse
value-key作为 value 唯一标识的键名,绑定值为对象类型时必填stringvalue
size输入框尺寸stringlarge/default/smalldefault
clearable是否可以清空选项booleantrue / falsefalse
collapse-tags多选时是否将选中值按文字的形式展示booleantrue/falsefalse
collapse-tags-tooltip当鼠标悬停于折叠标签的文本时,是否显示所有选中的标签。 要使用此属性,collapse-tags属性必须设定为 truebooleantrue / falsefalse
multiple-limitmultiple 属性设置为 true 时,代表多选场景下用户最多可以选择的项目数, 为 0 则不限制number0
nameSelect 输入框的原生 name 属性string
effectTooltip 主题,内置了 dark / light 两种stringstringlight
autocompleteSelect 输入框的原生 autocomplete 属性stringoff
placeholder占位文字stringSelect
filterableSelect 组件是否可筛选booleantrue / falsefalse
allow-create是否允许用户创建新条目, 只有当 filterable 设置为 true 时才会生效。booleantrue/falsefalse
filter-method自定义筛选方法function
remote其中的选项是否从服务器远程加载booleantrue / falsefalse
remote-method自定义远程搜索方法function
remote-show-suffix远程搜索方法显示后缀图标booleantrue / falsefalse
loading是否正在从远程获取数据booleantrue / falsefalse
loading-text从服务器加载内容时显示的文本stringLoading
no-match-text搜索条件无匹配时显示的文字,也可以使用 empty 插槽设置stringNo matching data
no-data-text无选项时显示的文字,也可以使用 empty 插槽设置自定义内容stringNo data
popper-class选择器下拉菜单的自定义类名string
reserve-keywordmultiplefilter被设置为 true 时,是否在选中一个选项后保留当前的搜索关键词booleantrue / falsetrue
default-first-option是否在输入框按下回车时,选择第一个匹配项。 需配合 filterableremote 使用booleantrue / falsefalse
popper-append-to-body deprecated是否将弹出框插入至 body 元素 当弹出框的位置出现问题时,你可以尝试将该属性设置为false。booleantrue / falsetrue
teleported该下拉菜单是否使用teleport插入body元素booleantrue / falsetrue
persistent当下拉选择器未被激活并且persistent设置为false,选择器会被删除。booleantrue / falsetrue
automatic-dropdown对于不可过滤的 Select 组件,此属性决定是否在输入框获得焦点后自动弹出选项菜单booleantrue / falsefalse
clear-icon自定义清除图标stringComponent
fit-input-width下拉框的宽度是否与输入框相同booleantrue / falsefalse
suffix-icon自定义后缀图标组件stringComponent
suffix-transition deprecated下拉菜单显示/消失时后缀图标的动画booleantrue / falsetrue
tag-type标签类型stringsuccess/info/warning/dangerinfo
validate-event是否触发表单验证booleantrue / falsetrue
placement下拉框出现的位置stringtop/top-start/top-end/bottom/bottom-start/bottom-end/left/left-start/left-end/right/right-start/right-endbottom-start
max-collapse-tags需要显示的 Tag 的最大数量 只有当 collapse-tags 设置为 true 时才会生效。number1

z-select 事件

事件名说明回调参数
change选中值发生变化时触发val,目前的选中值
visible-change下拉框出现/隐藏时触发val,出现则为 true,隐藏则为 false
remove-tag多选模式下移除tag时触发val,移除的tag值
clear可清空的单选模式下用户点击清空按钮时触发
blur当 input 失去焦点时触发(event: FocusEvent)
focus当 input 获得焦点时触发(event: FocusEvent)

z-select 插槽

插槽名说明子标签
prefixSelect 组件头部内容
empty无选项时的列表

Option 项可配置属性

属性名说明类型可选值默认值
value选项的值string / number / boolean / object
label选项的标签,若不设置则默认与value相同string / number
disabled是否禁用该选项booleanfalse
options可配置项(分组时可配置)array
render选项自定义string / () => VNode

Released under the MIT License.