ant-design/components/select/index.en-US.md

119 lines
7.1 KiB
Markdown
Raw Normal View History

2016-07-10 08:55:43 +08:00
---
category: Components
type: Data Entry
2016-07-10 08:55:43 +08:00
title: Select
cover: https://gw.alipayobjects.com/zos/alicdn/_0XzgOis7/Select.svg
2016-07-10 08:55:43 +08:00
---
2017-08-23 11:45:21 +08:00
Select component to select value from options.
2016-07-10 08:55:43 +08:00
2016-09-10 13:43:30 +08:00
## When To Use
2016-07-10 08:55:43 +08:00
- A dropdown menu for displaying choices - an elegant alternative to the native `<select>` element.
- Utilizing [Radio](/components/radio/) is recommended when there are fewer total options (less than 5).
2016-10-14 11:29:45 +08:00
## API
2016-07-10 08:55:43 +08:00
2019-12-26 14:37:39 +08:00
```jsx
<Select>
<Option value="lucy">lucy</Option>
</Select>
2016-07-10 08:55:43 +08:00
```
### Select props
| Property | Description | Type | Default | Version |
| --- | --- | --- | --- | --- |
| allowClear | Show clear button. | boolean | false | |
| autoClearSearchValue | Whether the current search will be cleared on selecting an item. Only applies when `mode` is set to `multiple` or `tags`. | boolean | true | |
| autoFocus | Get focus by default | boolean | false | |
| defaultActiveFirstOption | Whether active first option by default | boolean | true | |
| defaultValue | Initial selected option. | string\|string\[]<br />number\|number\[]<br />LabeledValue\|LabeledValue[] | - | |
| disabled | Whether disabled select | boolean | false | |
| dropdownClassName | className of dropdown menu | string | - | |
| dropdownMatchSelectWidth | Determine whether the dropdown menu and the select input are the same width. Default set `min-width` same as input. `false` will disable virtual scroll | boolean \| number | true | |
| dropdownRender | Customize dropdown content | (originNode: ReactNode) => ReactNode | - | |
2020-04-28 14:09:54 +08:00
| dropdownStyle | style of dropdown menu | CSSProperties | - | |
| filterOption | If 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 excluded. | boolean or function(inputValue, option) | true | |
| getPopupContainer | Parent 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. [Example](https://codesandbox.io/s/4j168r7jw0) | function(triggerNode) | () => document.body | |
| labelInValue | whether to embed label in value, turn the format of value from `string` to `{key: string, label: ReactNode}` | boolean | false | |
2020-04-15 10:04:01 +08:00
| listHeight | Config popup height | number | 256 | |
| maxTagCount | Max tag count to show | number | - | |
2019-11-25 15:16:59 +08:00
| maxTagTextLength | Max tag text length to show | number | - | |
| maxTagPlaceholder | Placeholder for not showing tags | ReactNode/function(omittedValues) | - | |
| tagRender | Customize tag render | (props) => ReactNode | - | |
| mode | Set mode of Select | `multiple` \| `tags` | - | |
| notFoundContent | Specify content to show when no result matches.. | ReactNode | 'Not Found' | |
2020-05-19 10:42:34 +08:00
| options | Select options. Will get better perf than jsx definition | { label, value }[] | - | |
| optionFilterProp | Which prop value of option will be used for filter if filterOption is true | string | `value` | |
| optionLabelProp | Which prop value of option will render as content of select. [Example](https://codesandbox.io/s/antd-reproduction-template-tk678) | string | `children` | |
| placeholder | Placeholder of select | string\|ReactNode | - | |
| showArrow | Whether to show the drop-down arrow | boolean | true | |
| showSearch | Whether show search input in single mode. | boolean | false | |
2020-03-01 18:24:59 +08:00
| size | Size of Select input. | `large` \| `middle` \| `small` | | |
| suffixIcon | The custom suffix icon | ReactNode | - | |
| removeIcon | The custom remove icon | ReactNode | - | |
| clearIcon | The custom clear icon | ReactNode | - | |
| menuItemSelectedIcon | The custom menuItemSelected icon with multiple options | ReactNode | - | |
| tokenSeparators | Separator used to tokenize on tag/multiple mode | string\[] | | |
2020-03-29 18:23:21 +08:00
| value | Current selected option. | string\|string\[]<br />number\|number\[]<br />LabeledValue\|LabeledValue[] | - | |
2020-04-08 10:52:55 +08:00
| virtual | Disable virtual scroll when set to `false` | boolean | true | 4.1.0 |
| onBlur | Called when blur | function | - | |
| onChange | Called when select an option or input value change | function(value, option:Option/Array&lt;Option>) | - | |
| onDeselect | Called when a option is deselected, param is the selected option's value. Only called for multiple or tags, effective in multiple or tags mode only. | function(string\|number\|LabeledValue) | - | |
| onFocus | Called when focus | function | - | |
| onInputKeyDown | Called when key pressed | function | - | |
| onMouseEnter | Called when mouse enter | function | - | |
| onMouseLeave | Called when mouse leave | function | - | |
| onPopupScroll | Called when dropdown scrolls | function | - | |
| onSearch | Callback function that is fired when input changed. | function(value: string) | | |
| onSelect | Called when a option is selected, the params are option's value (or key) and option instance. | function(string\|number\|LabeledValue, option:Option) | - | |
| defaultOpen | Initial open state of dropdown | boolean | - | |
| open | Controlled open state of dropdown | boolean | - | |
| onDropdownVisibleChange | Call when dropdown open | function(open) | - | |
| loading | indicate loading state | Boolean | false | |
| bordered | whether has border style | Boolean | true | |
2016-07-10 08:55:43 +08:00
### Select Methods
| Name | Description | Version |
| ------- | ------------ | ------- |
| blur() | Remove focus | |
| focus() | Get focus | |
2016-07-10 08:55:43 +08:00
### Option props
| Property | Description | Type | Default | Version |
| --------- | ------------------------------------------ | -------------- | ------- | ------- |
| disabled | Disable this option | boolean | false | |
| title | `title` of Select after select this Option | string | - | |
| value | default to filter with this property | string\|number | - | |
| className | additional class to option | string | - | |
2016-07-10 08:55:43 +08:00
### OptGroup props
| Property | Description | Type | Default | Version |
| -------- | ----------- | --------------------- | ------- | ------- |
| key | | string | - | |
| label | Group label | string\|React.Element | - | |
## FAQ
### The dropdown is closed when click `dropdownRender` area?
2020-02-26 16:47:44 +08:00
See the instruction in [dropdownRender example](#components-select-demo-custom-dropdown-menu).
### Why sometime customize Option cause scroll break?
Virtual scroll internal set item height as `32px`. You need to adjust `listItemHeight` when your option height is less and `listHeight` config list container height:
```tsx
<Select listItemHeight={10} listHeight={250} />
```
Note: `listItemHeight` and `listHeight` are internal props. Please only modify when necessary.
2020-05-23 14:51:54 +08:00
### Why a11y test report missing `aria-` props?
Select only create a11y auxiliary node when operating on. Please open Select and retry. For `aria-label` & `aria-labelledby` miss warning, please add related prop to Select with your own requirement.