Skip to content

Select

Select component encapsulation, works better with z-form component.

Basic Usage

Pass in options to automatically generate options

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

const options = ref([
  { label: 'Option 1', value: 1 },
  { label: 'Option 2', value: 2 },
  { label: 'Option 3', 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>

Disabled

Set disabled to true for a certain item in option

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

const options = ref([
  { label: 'Option 1', value: 1 },
  { label: 'Option 2', value: 2, disabled: true },
  { label: 'Option 3', value: 3 },
])

const selectVal = ref(1)
</script>

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

Disable all, pass disabled as true to the component

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

const options = ref([
  { label: 'Option 1', value: 1 },
  { label: 'Option 2', value: 2 },
  { label: 'Option 3', value: 3 },
])

const selectVal = ref(1)
</script>

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

Key-Value Configuration

Configure alias to customize the key names for label, value and disabled

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

const options = ref([
  { data: { title: 'Option 1' }, key: 1 },
  { data: { title: 'Option 2' }, key: 2 },
  { data: { title: 'Option 3' }, 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>

Multiple Selection

When multiple is true, multiple selection is enabled. The bound model-value is in array format

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

const options = ref([
  { label: 'Option 1', value: 1 },
  { label: 'Option 2', value: 2 },
  { label: 'Option 3', 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>

Select All

TIP

This only takes effect when multiple is true

When all is true, options will be concatenated with { label: 'All', value: 'all' }

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

const options = ref([
  { label: 'Option 1', value: 1 },
  { label: 'Option 2', value: 2 },
  { label: 'Option 3', value: 3 },
])

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

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

Grouping

Grouping display can be easily generated through the options field in option

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

const options = ref([
  {
    label: 'Option 1',
    options: [
      { label: 'Option 1-1', value: 11 },
      { label: 'Option 1-2', value: 12 },
    ],
  },
  {
    label: 'Option 2',
    options: [
      { label: 'Option 2-1', value: 21 },
      { label: 'Option 2-2', value: 22 },
    ],
  },
  {
    label: 'Option 3',
    disabled: true,
    options: [
      { label: 'Option 3-1', value: 31 },
      { label: 'Option 3-2', value: 32 },
    ],
  },
])

const selectVal = ref(11)
</script>

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

Custom Content

Configure render function in option item to customize content, or pass custom string + Slot to the render field to use slot functionality

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

const options = ref([
  {
    label: 'Option 1',
    value: 1,
    render: ({ option }: OptionsItem) => {
      return h('span', {}, `Custom ${option.value}`)
    },
  },
  {
    label: 'Option 2',
    value: 2,
    render: 'optionSlot',
  },
  { label: 'Option 3', value: 3 },
])

const selectVal = ref(1)
</script>

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

Slots

If you want to customize the component header content or the list when there are no options, configure prefix or empty attributes, or use prefix or empty slots

<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', {}, 'Custom Content')
}
</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="Custom Content" class="mt-4" />
    <z-select v-model="selectVal" :options="options" class="mt-4">
      <template #prefix>
        <span>Header</span>
      </template>
      <template #empty>
        <span>Custom Content</span>
      </template>
    </z-select>
  </div>
</template>

Supports tag, loading, header, footer, label and other slots

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

const options = ref([
  { label: 'Option 1', value: 1, render: ({ option }: { option: OptionsItem }) => h('span', `${option.label}${option.value}`) },
  { label: 'Option 2', value: 2 },
  { label: 'Option 3', 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>Header</span>
      </template>
      <template #footer>
        <span>Custom Content</span>
      </template>
      <template #label="{ label, value }">
        <span>{{ label }}: </span>
        <span style="font-weight: bold">{{ value }}</span>
      </template>
    </z-select>
  </div>
</template>

z-select Attributes

AttributeDescriptionTypeAccepted ValuesDefault
model-value / v-modelBinding value of selected itemarray / string / number / boolean / object
optionsConfigurable optionsarray
aliasKey-value pair configurationobject{ label: 'label', value: 'value', disabled: 'disabled' }
prefixSelect component header contentstring /function
emptyList when no optionsstring / function
multipleWhether multiple selectionbooleantrue/falsefalse
allWhether all (only effective when multiple is true)booleantrue/falsefalse
disabledWhether disabledbooleantrue / falsefalse
value-keyUnique identity key name as value, required when binding value is object typestringvalue
sizeInput box sizestringlarge/default/smalldefault
clearableWhether options can be clearedbooleantrue / falsefalse
collapse-tagsWhether to display selected values as text when multiple selectionbooleantrue/falsefalse
collapse-tags-tooltipWhether to show all selected tags when mouse hovers over collapsed tag text. The collapse-tags attribute must be set to true to use this attributebooleantrue / falsefalse
multiple-limitMaximum number of options user can select when multiple attribute is set to true. No limit when set to 0number0
nameNative name attribute of Select inputstring
effectTooltip theme, built-in dark / light themesstringstringlight
autocompleteNative autocomplete attribute of Select inputstringoff
placeholderPlaceholder textstringSelect
filterableWhether Select component is filterablebooleantrue / falsefalse
allow-createWhether creating new items is allowed. Only effective when filterable is set to true.booleantrue/falsefalse
filter-methodCustom filter methodfunction
remoteWhether options are loaded remotely from serverbooleantrue / falsefalse
remote-methodCustom remote search methodfunction
remote-show-suffixShow suffix icon for remote search methodbooleantrue / falsefalse
loadingWhether loading data remotelybooleantrue / falsefalse
loading-textText displayed when loading content from serverstringLoading
no-match-textText displayed when no matching search criteria, can also use empty slotstringNo matching data
no-data-textText displayed when no options, can also use empty slot for custom contentstringNo data
popper-classCustom class name for Select dropdown menustring
reserve-keywordWhen multiple and filter are set to true, whether to retain current search keywords after selecting an optionbooleantrue / falsetrue
default-first-optionWhether to select first matching item when pressing Enter in input box. Use with filterable or remotebooleantrue / falsefalse
popper-append-to-body deprecatedWhether to insert popup into body element. Try setting to false when popup position issues occur.booleantrue / falsetrue
teleportedWhether dropdown menu uses teleport to insert into body elementbooleantrue / falsetrue
persistentWhen dropdown selector is not activated and persistent is set to false, selector will be deleted.booleantrue / falsetrue
automatic-dropdownFor non-filterable Select components, this attribute determines whether to automatically pop up option menu after input box gains focusbooleantrue / falsefalse
clear-iconCustom clear iconstringComponent
fit-input-widthWhether dropdown width is same as input boxbooleantrue / falsefalse
suffix-iconCustom suffix icon componentstringComponent
suffix-transition deprecatedAnimation of suffix icon when dropdown menu shows/hidesbooleantrue / falsetrue
tag-typeTag typestringsuccess/info/warning/dangerinfo
validate-eventWhether to trigger form validationbooleantrue / falsetrue
placementPosition where dropdown appearsstringtop/top-start/top-end/bottom/bottom-start/bottom-end/left/left-start/left-end/right/right-start/right-endbottom-start
max-collapse-tagsMaximum number of Tags to display. Only effective when collapse-tags is set to true.number1

z-select Events

Event NameDescriptionCallback Parameters
changeTriggered when selected value changesval, current selected value
visible-changeTriggered when dropdown appears/hidesval, true when appears, false when hides
remove-tagTriggered when removing tag in multiple modeval, removed tag value
clearTriggered when user clicks clear button in clearable single mode
blurTriggered when input loses focus(event: FocusEvent)
focusTriggered when input gains focus(event: FocusEvent)

z-select Slots

Slot NameDescriptionSubtags
prefixSelect component header content
emptyList when no options

Option Item Configurable Attributes

AttributeDescriptionTypeAccepted ValuesDefault
valueOption valuestring / number / boolean / object
labelOption label, defaults to value if not setstring / number
disabledWhether option is disabledbooleanfalse
optionsConfigurable options (for grouping)array
renderOption customizationstring / () => VNode

Released under the MIT License.