Skip to content

Select

Select component to select value from options.

When To Use

  • A dropdown menu for displaying choices - an elegant alternative to the native <select> element.
  • Utilizing Radio is recommended when there are fewer total options (less than 5).

Examples

Basic Usage

vue
<template>
  <a-select
    ref="select"
    v-model:value="value"
    style="width: 120px"
    @focus="focus"
    @blur="blur"
    @change="handleChange"
  >
    <a-select-option value="jack">Jack</a-select-option>
    <a-select-option value="lucy">Lucy</a-select-option>
    <a-select-option value="disabled" disabled>Disabled</a-select-option>
    <a-select-option value="Yiminghe">yiminghe</a-select-option>
  </a-select>
</template>

<script setup>
import { ref } from 'vue'

const value = ref('lucy')

const focus = () => {
  console.log('focus')
}

const blur = () => {
  console.log('blur')
}

const handleChange = (value) => {
  console.log(`selected ${value}`)
}
</script>

Select with search field

vue
<template>
  <a-select
    show-search
    placeholder="Select a person"
    option-filter-prop="children"
    style="width: 200px"
    :filter-option="filterOption"
    @focus="handleFocus"
    @blur="handleBlur"
    @change="handleChange"
  >
    <a-select-option value="jack">Jack</a-select-option>
    <a-select-option value="lucy">Lucy</a-select-option>
    <a-select-option value="tom">Tom</a-select-option>
  </a-select>
</template>

<script setup>
const filterOption = (input, option) => {
  return option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
}

const handleChange = (value) => {
  console.log(`selected ${value}`)
}

const handleBlur = () => {
  console.log('blur')
}

const handleFocus = () => {
  console.log('focus')
}
</script>

Multiple selection

vue
<template>
  <div>
    <a-select
      v-model:value="value1"
      mode="multiple"
      style="width: 100%"
      placeholder="select one country"
      :default-value="['china']"
      @change="handleChange"
    >
      <a-select-option v-for="i in 25" :key="(i + 9).toString(36) + i">
        {{ (i + 9).toString(36) + i }}
      </a-select-option>
    </a-select>
    <br />
    <a-select
      v-model:value="value2"
      mode="multiple"
      size="small"
      style="width: 100%"
      placeholder="select one country"
      :default-value="['china']"
      @change="handleChange"
    >
      <a-select-option v-for="i in 25" :key="(i + 9).toString(36) + i">
        {{ (i + 9).toString(36) + i }}
      </a-select-option>
    </a-select>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const value1 = ref(['china'])
const value2 = ref(['china'])

const handleChange = (value) => {
  console.log(`selected ${value}`)
}
</script>

Sizes

vue
<template>
  <div>
    <a-radio-group v-model:value="size">
      <a-radio-button value="large">Large</a-radio-button>
      <a-radio-button value="default">Default</a-radio-button>
      <a-radio-button value="small">Small</a-radio-button>
    </a-radio-group>
    <br />
    <br />
    <a-select
      v-model:value="value1"
      :size="size"
      default-value="a1"
      style="width: 200px"
      @change="handleChange"
    >
      <a-select-option value="a1">Long, long, long piece of text</a-select-option>
      <a-select-option value="a2">hi!</a-select-option>
    </a-select>
    <br />
    <a-select
      v-model:value="value2"
      mode="multiple"
      :size="size"
      placeholder="Please select"
      :default-value="['a10', 'c12']"
      style="width: 100%"
      @change="handleChange"
    >
      <a-select-option v-for="i in 25" :key="(i + 9).toString(36) + i">
        {{ (i + 9).toString(36) + i }}
      </a-select-option>
    </a-select>
    <br />
    <a-select
      v-model:value="value3"
      mode="tags"
      :size="size"
      placeholder="Please select"
      style="width: 100%"
      @change="handleChange"
    >
      <a-select-option v-for="i in 25" :key="(i + 9).toString(36) + i">
        {{ (i + 9).toString(36) + i }}
      </a-select-option>
    </a-select>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const size = ref('default')
const value1 = ref('a1')
const value2 = ref(['a10', 'c12'])
const value3 = ref([])

const handleChange = (value) => {
  console.log(`selected ${value}`)
}
</script>

Tags

vue
<template>
  <div>
    <a-select
      v-model:value="value"
      mode="tags"
      style="width: 100%"
      placeholder="Tags Mode"
      @change="handleChange"
    >
      <a-select-option v-for="i in 25" :key="(i + 9).toString(36) + i">
        {{ (i + 9).toString(36) + i }}
      </a-select-option>
    </a-select>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const value = ref([])

const handleChange = (value) => {
  console.log(`selected ${value}`)
}
</script>

API

Select props

PropertyDescriptionTypeDefaultVersion
allowClearShow clear buttonbooleanfalse
autoClearSearchValueWhether the current search will be cleared on selecting an item. Only applies when mode is set to multiple or tagsbooleantrue
autoFocusGet focus by defaultbooleanfalse
borderedWhether has border stylebooleantrue
clearIconThe custom clear iconVNode | slot-
defaultActiveFirstOptionWhether active first option by defaultbooleantrue
defaultOpenInitial open state of dropdownboolean-
defaultValueInitial selected optionstring | string[] | number | number[] | LabeledValue | LabeledValue[]-
disabledWhether disabled selectbooleanfalse
dropdownClassNameThe className of dropdown menustring-
dropdownMatchSelectWidthDetermine whether the dropdown menu and the select input are the same width. Default set min-width same as input. Will ignore when value less than select width. false will disable virtual scrollboolean | numbertrue
dropdownRenderCustomize dropdown content({menuNode: VNode, props}) => VNode | v-slot-
dropdownStyleThe style of dropdown menuCSSProperties-
fieldNamesCustomize node label, value, options field nameobject{ label: label, value: value, options: options }
filterOptionIf true, filter options by input, if function, filter options against it. The function will receive two arguments, inputValue and option, if the function returns true, the option will be included in the filtered set; Otherwise, it will be excludedboolean | function(inputValue, option)true
filterSortSort function for search options sorting, see Array.sort's compareFunction(optionA: Option, optionB: Option) => number-
firstActiveValueValue of action option by defaultstring | string[]-
getPopupContainerParent Node which the selector should be rendered to. Default to body. When position issues happen, try to modify it into scrollable content and position it relative.function(triggerNode)() => document.body
labelInValueWhether to embed label in value, turn the format of value from string to {key: string, label: vNodes}booleanfalse
listHeightConfig popup heightnumber256
loadingIndicate loading statebooleanfalse
maxTagCountMax tag count to show. responsive will cost render performancenumber | responsive-
maxTagPlaceholderPlaceholder for not showing tagsslot | function(omittedValues)-
maxTagTextLengthMax tag text length to shownumber-
menuItemSelectedIconThe custom menuItemSelected iconVNode | slot-
modeSet mode of Selectmultiple | tags-
notFoundContentSpecify content to show when no result matchesstring | slotNot Found
openControlled open state of dropdownboolean-
optioncustom render option by slotv-slot:option="{value, label, [disabled, key, title]}"-
optionFilterPropWhich prop value of option will be used for filter if filterOption is truestringvalue
optionLabelPropWhich prop value of option will render as content of select.stringchildren
optionsData of the selectOption, manual construction work is no longer needed if this property has been setarray<{value, label, [disabled, key, title]}>[]
placeholderPlaceholder of selectstring | slot-
placementThe position where the selection box pops upbottomLeft bottomRight topLeft topRightbottomLeft
removeIconThe custom remove iconVNode | slot-
searchValueThe current input "search" textstring-
showArrowWhether to show the drop-down arrowbooleantrue
showSearchWhether select is searchablebooleansingle: false, multiple: true
sizeSize of Select inputlarge | middle | smallmiddle
suffixIconThe custom suffix iconVNode | slot-
tagRenderCustomize tag render, only applies when mode is set to multiple or tagsslot | function(props)-
tokenSeparatorsSeparator used to tokenize, only applies when mode="tags"string[][]
value(v-model)Current selected option (considered as a immutable array)string | string[] | number | number[] | LabeledValue | LabeledValue[]-
virtualDisable virtual scroll when set to falsebooleantrue

Select Events

Events NameDescriptionArgumentsVersion
blurCalled when blurfunction-
changeCalled when select an option or input value changefunction(value, option:Option | Array<Option>)-
deselectCalled when an option is deselected, param is the selected option's value. Only called for multiple or tags, effective in multiple or tags mode onlyfunction(string | number | LabeledValue)-
dropdownVisibleChangeCalled when dropdown openfunction(open)-
focusCalled when focusfunction-
inputKeyDownCalled when key pressedfunction-
mouseenterCalled when mouse enterfunction-
mouseleaveCalled when mouse leavefunction-
popupScrollCalled when dropdown scrollsfunction-
searchCallback function that is fired when input changedfunction(value: string)-
selectCalled when an option is selected, the params are option's value (or key) and option instancefunction(string | number | LabeledValue, option: Option)-

Option props

PropertyDescriptionTypeDefaultVersion
classThe additional class to optionstring-
disabledDisable this optionbooleanfalse
keySame usage as value. If Vue request you to set this property, you can set it to value of option, and then omit value propertystring-
titletitle of Select after select this Optionstring-
valueDefault to filter with this propertystring | number-

OptGroup props

PropertyDescriptionTypeDefaultVersion
keyKeystring-
labelGroup labelstring | slot-

Released under the MIT License.