Select选择器
下拉选择器。
何时使用#
弹出一个下拉菜单给用户选择操作,用于代替原生的选择器,或者需要一个更优雅的多选器时。
当选项少时(少于 5 项),建议直接将选项平铺,使用 Radio 是更好的选择。
代码演示
Lucy
Lucy
Lucy
Lucy
TypeScript
JavaScript
import { Select } from 'infrad';
import React from 'react';
const { Option } = Select;
const handleChange = (value: string) => {
console.log(`selected ${value}`);
};
const App: React.FC = () => (
<>
<Select defaultValue="lucy" style={{ width: 120 }} onChange={handleChange}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="disabled" disabled>
Disabled
</Option>
<Option value="Yiminghe">yiminghe</Option>
</Select>
<Select defaultValue="lucy" style={{ width: 120 }} disabled>
<Option value="lucy">Lucy</Option>
</Select>
<Select defaultValue="lucy" style={{ width: 120 }} loading>
<Option value="lucy">Lucy</Option>
</Select>
<Select defaultValue="lucy" style={{ width: 120 }} allowClear>
<Option value="lucy">Lucy</Option>
</Select>
</>
);
export default App;
a10
c12
a10
c12
TypeScript
JavaScript
import { Select } from 'infrad';
import React from 'react';
const { Option } = Select;
const children: React.ReactNode[] = [];
for (let i = 10; i < 36; i++) {
children.push(<Option key={i.toString(36) + i}>{i.toString(36) + i}</Option>);
}
const handleChange = (value: string[]) => {
console.log(`selected ${value}`);
};
const App: React.FC = () => (
<>
<Select
mode="multiple"
allowClear
style={{ width: '100%' }}
placeholder="Please select"
defaultValue={['a10', 'c12']}
onChange={handleChange}
>
{children}
</Select>
<br />
<Select
mode="multiple"
disabled
style={{ width: '100%' }}
placeholder="Please select"
defaultValue={['a10', 'c12']}
onChange={handleChange}
>
{children}
</Select>
</>
);
export default App;
China
TypeScript
JavaScript
import { Select } from 'infrad';
import React from 'react';
const { Option } = Select;
const handleChange = (value: string[]) => {
console.log(`selected ${value}`);
};
const App: React.FC = () => (
<Select
mode="multiple"
style={{ width: '100%' }}
placeholder="select one country"
defaultValue={['china']}
onChange={handleChange}
optionLabelProp="label"
>
<Option value="china" label="China">
<div className="demo-option-label-item">
<span role="img" aria-label="China">
🇨🇳
</span>
China (中国)
</div>
</Option>
<Option value="usa" label="USA">
<div className="demo-option-label-item">
<span role="img" aria-label="USA">
🇺🇸
</span>
USA (美国)
</div>
</Option>
<Option value="japan" label="Japan">
<div className="demo-option-label-item">
<span role="img" aria-label="Japan">
🇯🇵
</span>
Japan (日本)
</div>
</Option>
<Option value="korea" label="Korea">
<div className="demo-option-label-item">
<span role="img" aria-label="Korea">
🇰🇷
</span>
Korea (韩国)
</div>
</Option>
</Select>
);
export default App;
.demo-option-label-item > span {
margin-right: 6px;
}
TypeScript
JavaScript
import { Select } from 'infrad';
import React from 'react';
const { Option } = Select;
const children: React.ReactNode[] = [];
for (let i = 10; i < 36; i++) {
children.push(<Option key={i.toString(36) + i}>{i.toString(36) + i}</Option>);
}
const handleChange = (value: string) => {
console.log(`selected ${value}`);
};
const App: React.FC = () => (
<Select mode="tags" style={{ width: '100%' }} placeholder="Tags Mode" onChange={handleChange}>
{children}
</Select>
);
export default App;
Zhejiang
Hangzhou
TypeScript
JavaScript
import { Select } from 'infrad';
import React, { useState } from 'react';
const { Option } = Select;
const provinceData = ['Zhejiang', 'Jiangsu'];
const cityData = {
Zhejiang: ['Hangzhou', 'Ningbo', 'Wenzhou'],
Jiangsu: ['Nanjing', 'Suzhou', 'Zhenjiang'],
};
type CityName = keyof typeof cityData;
const App: React.FC = () => {
const [cities, setCities] = useState(cityData[provinceData[0] as CityName]);
const [secondCity, setSecondCity] = useState(cityData[provinceData[0] as CityName][0]);
const handleProvinceChange = (value: CityName) => {
setCities(cityData[value]);
setSecondCity(cityData[value][0]);
};
const onSecondCityChange = (value: CityName) => {
setSecondCity(value);
};
return (
<>
<Select defaultValue={provinceData[0]} style={{ width: 120 }} onChange={handleProvinceChange}>
{provinceData.map(province => (
<Option key={province}>{province}</Option>
))}
</Select>
<Select style={{ width: 120 }} value={secondCity} onChange={onSecondCityChange}>
{cities.map(city => (
<Option key={city}>{city}</Option>
))}
</Select>
</>
);
};
export default App;
Lucy (101)
TypeScript
JavaScript
import { Select } from 'infrad';
import React from 'react';
const { Option } = Select;
const handleChange = (value: { value: string; label: React.ReactNode }) => {
console.log(value); // { value: "lucy", key: "lucy", label: "Lucy (101)" }
};
const App: React.FC = () => (
<Select
labelInValue
defaultValue={{ value: 'lucy', label: 'Lucy (101)' }}
style={{ width: 120 }}
onChange={handleChange}
>
<Option value="jack">Jack (100)</Option>
<Option value="lucy">Lucy (101)</Option>
</Select>
);
export default App;
TypeScript
JavaScript
import { Select, Spin } from 'infrad';
import type { SelectProps } from 'infrad/es/select';
import debounce from 'lodash/debounce';
import React, { useMemo, useRef, useState } from 'react';
export interface DebounceSelectProps<ValueType = any>
extends Omit<SelectProps<ValueType | ValueType[]>, 'options' | 'children'> {
fetchOptions: (search: string) => Promise<ValueType[]>;
debounceTimeout?: number;
}
function DebounceSelect<
ValueType extends { key?: string; label: React.ReactNode; value: string | number } = any,
>({ fetchOptions, debounceTimeout = 800, ...props }: DebounceSelectProps<ValueType>) {
const [fetching, setFetching] = useState(false);
const [options, setOptions] = useState<ValueType[]>([]);
const fetchRef = useRef(0);
const debounceFetcher = useMemo(() => {
const loadOptions = (value: string) => {
fetchRef.current += 1;
const fetchId = fetchRef.current;
setOptions([]);
setFetching(true);
fetchOptions(value).then(newOptions => {
if (fetchId !== fetchRef.current) {
// for fetch callback order
return;
}
setOptions(newOptions);
setFetching(false);
});
};
return debounce(loadOptions, debounceTimeout);
}, [fetchOptions, debounceTimeout]);
return (
<Select
labelInValue
filterOption={false}
onSearch={debounceFetcher}
notFoundContent={fetching ? <Spin size="small" /> : null}
{...props}
options={options}
/>
);
}
// Usage of DebounceSelect
interface UserValue {
label: string;
value: string;
}
async function fetchUserList(username: string): Promise<UserValue[]> {
console.log('fetching user', username);
return fetch('https://randomuser.me/api/?results=5')
.then(response => response.json())
.then(body =>
body.results.map(
(user: { name: { first: string; last: string }; login: { username: string } }) => ({
label: `${user.name.first} ${user.name.last}`,
value: user.login.username,
}),
),
);
}
const App: React.FC = () => {
const [value, setValue] = useState<UserValue[]>([]);
return (
<DebounceSelect
mode="multiple"
value={value}
placeholder="Select users"
fetchOptions={fetchUserList}
onChange={newValue => {
setValue(newValue as UserValue[]);
}}
style={{ width: '100%' }}
/>
);
};
export default App;
TypeScript
JavaScript
import { Select } from 'infrad';
import React, { useState } from 'react';
const OPTIONS = ['Apples', 'Nails', 'Bananas', 'Helicopters'];
const App: React.FC = () => {
const [selectedItems, setSelectedItems] = useState<string[]>([]);
const filteredOptions = OPTIONS.filter(o => !selectedItems.includes(o));
return (
<Select
mode="multiple"
placeholder="Inserted are removed"
value={selectedItems}
onChange={setSelectedItems}
style={{ width: '100%' }}
>
{filteredOptions.map(item => (
<Select.Option key={item} value={item}>
{item}
</Select.Option>
))}
</Select>
);
};
export default App;
gold
cyan
TypeScript
JavaScript
import { Select, Tag } from 'infrad';
import type { CustomTagProps } from 'rc-select/lib/BaseSelect';
import React from 'react';
const options = [{ value: 'gold' }, { value: 'lime' }, { value: 'green' }, { value: 'cyan' }];
const tagRender = (props: CustomTagProps) => {
const { label, value, closable, onClose } = props;
const onPreventMouseDown = (event: React.MouseEvent<HTMLSpanElement>) => {
event.preventDefault();
event.stopPropagation();
};
return (
<Tag
color={value}
onMouseDown={onPreventMouseDown}
closable={closable}
onClose={onClose}
style={{ marginRight: 3 }}
>
{label}
</Tag>
);
};
const App: React.FC = () => (
<Select
mode="multiple"
showArrow
tagRender={tagRender}
defaultValue={['gold', 'cyan']}
style={{ width: '100%' }}
options={options}
/>
);
export default App;
Infra Design 4.0
100000 Items
a10
c12
Infra Design 3.0
TypeScript
JavaScript
import type { SelectProps } from 'infrad';
import { Divider, Select, Typography } from 'infrad';
import React from 'react';
const { Title } = Typography;
const options: SelectProps['options'] = [];
for (let i = 0; i < 100000; i++) {
const value = `${i.toString(36)}${i}`;
options.push({
label: value,
value,
disabled: i === 10,
});
}
const handleChange = (value: string[]) => {
console.log(`selected ${value}`);
};
const App: React.FC = () => (
<>
<Title level={3}>Infra Design 4.0</Title>
<Title level={4}>{options.length} Items</Title>
<Select
mode="multiple"
style={{ width: '100%' }}
placeholder="Please select"
defaultValue={['a10', 'c12']}
onChange={handleChange}
options={options}
/>
<Divider />
<Title level={3}>Infra Design 3.0</Title>
<iframe
title="Infra Design 3.0 Select demo"
src="https://codesandbox.io/embed/solitary-voice-m3vme?fontsize=14&hidenavigation=1&theme=dark&view=preview"
style={{ width: '100%', height: 300 }}
/>
</>
);
export default App;
HangZhou #310000
TypeScript
JavaScript
import type { RadioChangeEvent } from 'infrad';
import { Radio, Select } from 'infrad';
import type { SelectCommonPlacement } from 'infrad/es/_util/motion';
import React, { useState } from 'react';
const { Option } = Select;
const App: React.FC = () => {
const [placement, SetPlacement] = useState<SelectCommonPlacement>('topLeft');
const placementChange = (e: RadioChangeEvent) => {
SetPlacement(e.target.value);
};
return (
<>
<Radio.Group value={placement} onChange={placementChange}>
<Radio.Button value="topLeft">topLeft</Radio.Button>
<Radio.Button value="topRight">topRight</Radio.Button>
<Radio.Button value="bottomLeft">bottomLeft</Radio.Button>
<Radio.Button value="bottomRight">bottomRight</Radio.Button>
</Radio.Group>
<br />
<br />
<Select
defaultValue="HangZhou"
style={{ width: 120 }}
dropdownMatchSelectWidth={false}
placement={placement}
>
<Option value="HangZhou">HangZhou #310000</Option>
<Option value="NingBo">NingBo #315000</Option>
<Option value="WenZhou">WenZhou #325000</Option>
</Select>
</>
);
};
export default App;
Select a person
TypeScript
JavaScript
import { Select } from 'infrad';
import React from 'react';
const { Option } = Select;
const onChange = (value: string) => {
console.log(`selected ${value}`);
};
const onSearch = (value: string) => {
console.log('search:', value);
};
const App: React.FC = () => (
<Select
showSearch
placeholder="Select a person"
optionFilterProp="children"
onChange={onChange}
onSearch={onSearch}
filterOption={(input, option) =>
(option!.children as unknown as string).toLowerCase().includes(input.toLowerCase())
}
>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="tom">Tom</Option>
</Select>
);
export default App;
a1
a10
c12
a10
c12
TypeScript
JavaScript
import type { RadioChangeEvent } from 'infrad';
import { Radio, Select } from 'infrad';
import type { SizeType } from 'infrad/es/config-provider/SizeContext';
import React, { useState } from 'react';
const { Option } = Select;
const children: React.ReactNode[] = [];
for (let i = 10; i < 36; i++) {
children.push(<Option key={i.toString(36) + i}>{i.toString(36) + i}</Option>);
}
const handleChange = (value: string | string[]) => {
console.log(`Selected: ${value}`);
};
const App: React.FC = () => {
const [size, setSize] = useState<SizeType>('middle');
const handleSizeChange = (e: RadioChangeEvent) => {
setSize(e.target.value);
};
return (
<>
<Radio.Group value={size} onChange={handleSizeChange}>
<Radio.Button value="large">Large</Radio.Button>
<Radio.Button value="default">Default</Radio.Button>
<Radio.Button value="small">Small</Radio.Button>
</Radio.Group>
<br />
<br />
<Select size={size} defaultValue="a1" onChange={handleChange} style={{ width: 200 }}>
{children}
</Select>
<br />
<Select
mode="multiple"
size={size}
placeholder="Please select"
defaultValue={['a10', 'c12']}
onChange={handleChange}
style={{ width: '100%' }}
>
{children}
</Select>
<br />
<Select
mode="tags"
size={size}
placeholder="Please select"
defaultValue={['a10', 'c12']}
onChange={handleChange}
style={{ width: '100%' }}
>
{children}
</Select>
</>
);
};
export default App;
.code-box-demo .ant-select {
margin: 0 8px 10px 0;
}
.ant-row-rtl .code-box-demo .ant-select {
margin: 0 0 10px 8px;
}
#components-select-demo-search-box .code-box-demo .ant-select {
margin: 0;
}
Search to Select
TypeScript
JavaScript
import { Select } from 'infrad';
import React from 'react';
const { Option } = Select;
const App: React.FC = () => (
<Select
showSearch
style={{ width: 200 }}
placeholder="Search to Select"
optionFilterProp="children"
filterOption={(input, option) => (option!.children as unknown as string).includes(input)}
filterSort={(optionA, optionB) =>
(optionA!.children as unknown as string)
.toLowerCase()
.localeCompare((optionB!.children as unknown as string).toLowerCase())
}
>
<Option value="1">Not Identified</Option>
<Option value="2">Closed</Option>
<Option value="3">Communicated</Option>
<Option value="4">Identified</Option>
<Option value="5">Resolved</Option>
<Option value="6">Cancelled</Option>
</Select>
);
export default App;
Lucy
TypeScript
JavaScript
import { Select } from 'infrad';
import React from 'react';
const { Option, OptGroup } = Select;
const handleChange = (value: string) => {
console.log(`selected ${value}`);
};
const App: React.FC = () => (
<Select defaultValue="lucy" style={{ width: 200 }} onChange={handleChange}>
<OptGroup label="Manager">
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
</OptGroup>
<OptGroup label="Engineer">
<Option value="Yiminghe">yiminghe</Option>
</OptGroup>
</Select>
);
export default App;
input search text
TypeScript
JavaScript
import jsonp from 'fetch-jsonp';
import { Select } from 'infrad';
import qs from 'qs';
import React, { useState } from 'react';
const { Option } = Select;
let timeout: ReturnType<typeof setTimeout> | null;
let currentValue: string;
const fetch = (value: string, callback: (data: { value: string; text: string }[]) => void) => {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
currentValue = value;
const fake = () => {
const str = qs.stringify({
code: 'utf-8',
q: value,
});
jsonp(`https://suggest.taobao.com/sug?${str}`)
.then((response: any) => response.json())
.then((d: any) => {
if (currentValue === value) {
const { result } = d;
const data = result.map((item: any) => ({
value: item[0],
text: item[0],
}));
callback(data);
}
});
};
timeout = setTimeout(fake, 300);
};
const SearchInput: React.FC<{ placeholder: string; style: React.CSSProperties }> = props => {
const [data, setData] = useState<any[]>([]);
const [value, setValue] = useState<string>();
const handleSearch = (newValue: string) => {
if (newValue) {
fetch(newValue, setData);
} else {
setData([]);
}
};
const handleChange = (newValue: string) => {
setValue(newValue);
};
const options = data.map(d => <Option key={d.value}>{d.text}</Option>);
return (
<Select
showSearch
value={value}
placeholder={props.placeholder}
style={props.style}
defaultActiveFirstOption={false}
showArrow={false}
filterOption={false}
onSearch={handleSearch}
onChange={handleChange}
notFoundContent={null}
>
{options}
</Select>
);
};
const App: React.FC = () => <SearchInput placeholder="input search text" style={{ width: 200 }} />;
export default App;
TypeScript
JavaScript
import { Select } from 'infrad';
import React from 'react';
const { Option } = Select;
const children: React.ReactNode[] = [];
for (let i = 10; i < 36; i++) {
children.push(<Option key={i.toString(36) + i}>{i.toString(36) + i}</Option>);
}
const handleChange = (value: string) => {
console.log(`selected ${value}`);
};
const App: React.FC = () => (
<Select mode="tags" style={{ width: '100%' }} onChange={handleChange} tokenSeparators={[',']}>
{children}
</Select>
);
export default App;
Lucy
Lucy
TypeScript
JavaScript
import { Select } from 'infrad';
import React from 'react';
const { Option } = Select;
const App: React.FC = () => (
<>
<Select defaultValue="lucy" style={{ width: 120 }} bordered={false}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="Yiminghe">yiminghe</Option>
</Select>
<Select defaultValue="lucy" style={{ width: 120 }} disabled bordered={false}>
<Option value="lucy">Lucy</Option>
</Select>
</>
);
export default App;
TypeScript
JavaScript
import type { SelectProps } from 'infrad';
import { Select, Space } from 'infrad';
import React, { useState } from 'react';
interface ItemProps {
label: string;
value: string;
}
const options: ItemProps[] = [];
for (let i = 10; i < 36; i++) {
const value = i.toString(36) + i;
options.push({
label: `Long Label: ${value}`,
value,
});
}
const App: React.FC = () => {
const [value, setValue] = useState(['a10', 'c12', 'h17', 'j19', 'k20']);
const selectProps: SelectProps = {
mode: 'multiple',
style: { width: '100%' },
value,
options,
onChange: (newValue: string[]) => {
setValue(newValue);
},
placeholder: 'Select Item...',
maxTagCount: 'responsive',
};
return (
<Space direction="vertical" style={{ width: '100%' }}>
<Select {...selectProps} />
<Select {...selectProps} disabled />
</Space>
);
};
export default App;
TypeScript
JavaScript
import { Select, Space } from 'infrad';
import React from 'react';
const App: React.FC = () => (
<Space direction="vertical" style={{ width: '100%' }}>
<Select status="error" style={{ width: '100%' }} />
<Select status="warning" style={{ width: '100%' }} />
</Space>
);
export default App;
#components-select-demo-status .ant-select {
margin: 0;
}
4.19.0
API#
<Select>
<Option value="lucy">lucy</Option>
</Select>
Select props#
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
allowClear | 支持清除 | boolean | false | |
autoClearSearchValue | 是否在选中项后清空搜索框,只在 mode 为 multiple 或 tags 时有效 | boolean | true | |
autoFocus | 默认获取焦点 | boolean | false | |
bordered | 是否有边框 | boolean | true | |
clearIcon | 自定义的多选框清空图标 | ReactNode | - | |
defaultActiveFirstOption | 是否默认高亮第一个选项 | boolean | true | |
defaultOpen | 是否默认展开下拉菜单 | boolean | - | |
defaultValue | 指定默认选中的条目 | string | string[] number | number[] LabeledValue | LabeledValue[] | - | |
disabled | 是否禁用 | boolean | false | |
dropdownClassName | 下拉菜单的 className 属性 | string | - | |
dropdownMatchSelectWidth | 下拉菜单和选择器同宽。默认将设置 min-width ,当值小于选择框宽度时会被忽略。false 时会关闭虚拟滚动 | boolean | number | true | |
dropdownRender | 自定义下拉框内容 | (originNode: ReactNode) => ReactNode | - | |
dropdownStyle | 下拉菜单的 style 属性 | CSSProperties | - | |
fieldNames | 自定义节点 label、value、options 的字段 | object | { label: label , value: value , options: options } | 4.17.0 |
filterOption | 是否根据输入项进行筛选。当其为一个函数时,会接收 inputValue option 两个参数,当 option 符合筛选条件时,应返回 true,反之则返回 false | boolean | function(inputValue, option) | true | |
filterSort | 搜索时对筛选结果项的排序函数, 类似Array.sort里的 compareFunction | (optionA: Option, optionB: Option) => number | - | 4.9.0 |
getPopupContainer | 菜单渲染父节点。默认渲染到 body 上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位。示例 | function(triggerNode) | () => document.body | |
labelInValue | 是否把每个选项的 label 包装到 value 中,会把 Select 的 value 类型从 string 变为 { value: string, label: ReactNode } 的格式 | boolean | false | |
listHeight | 设置弹窗滚动高度 | number | 256 | |
loading | 加载中状态 | boolean | false | |
maxTagCount | 最多显示多少个 tag,响应式模式会对性能产生损耗 | number | responsive | - | responsive: 4.10 |
maxTagPlaceholder | 隐藏 tag 时显示的内容 | ReactNode | function(omittedValues) | - | |
maxTagTextLength | 最大显示的 tag 文本长度 | number | - | |
menuItemSelectedIcon | 自定义多选时当前选中的条目图标 | ReactNode | - | |
mode | 设置 Select 的模式为多选或标签 | multiple | tags | - | |
notFoundContent | 当下拉列表为空时显示的内容 | ReactNode | Not Found | |
open | 是否展开下拉菜单 | boolean | - | |
optionFilterProp | 搜索时过滤对应的 option 属性,如设置为 children 表示对内嵌内容进行搜索。若通过 options 属性配置选项内容,建议设置 optionFilterProp="label" 来对内容进行搜索。 | string | value | |
optionLabelProp | 回填到选择框的 Option 的属性值,默认是 Option 的子元素。比如在子元素需要高亮效果时,此值可以设为 value 。示例 | string | children | |
options | 数据化配置选项内容,相比 jsx 定义会获得更好的渲染性能 | { label, value }[] | - | |
placeholder | 选择框默认文本 | string | - | |
placement | 选择框弹出的位置 | bottomLeft bottomRight topLeft topRight | bottomLeft | |
removeIcon | 自定义的多选框清除图标 | ReactNode | - | |
searchValue | 控制搜索文本 | string | - | |
showArrow | 是否显示下拉小箭头 | boolean | 单选为 true,多选为 false | |
showSearch | 使单选模式可搜索 | boolean | false | |
size | 选择框大小 | large | middle | small | middle | |
status | 设置校验状态 | 'error' | 'warning' | - | 4.19.0 |
suffixIcon | 自定义的选择框后缀图标 | ReactNode | - | |
tagRender | 自定义 tag 内容 render,仅在 mode 为 multiple 或 tags 时生效 | (props) => ReactNode | - | |
tokenSeparators | 自动分词的分隔符,仅在 mode="tags" 时生效 | string[] | - | |
value | 指定当前选中的条目,多选时为一个数组。(value 数组引用未变化时,Select 不会更新) | string | string[] number | number[] LabeledValue | LabeledValue[] | - | |
virtual | 设置 false 时关闭虚拟滚动 | boolean | true | 4.1.0 |
onBlur | 失去焦点时回调 | function | - | |
onChange | 选中 option,或 input 的 value 变化时,调用此函数 | function(value, option:Option | Array<Option>) | - | |
onClear | 清除内容时回调 | function | - | 4.6.0 |
onDeselect | 取消选中时调用,参数为选中项的 value (或 key) 值,仅在 multiple 或 tags 模式下生效 | function(string | number | LabeledValue) | - | |
onDropdownVisibleChange | 展开下拉菜单的回调 | function(open) | - | |
onFocus | 获得焦点时回调 | function | - | |
onInputKeyDown | 按键按下时回调 | function | - | |
onMouseEnter | 鼠标移入时回调 | function | - | |
onMouseLeave | 鼠标移出时回调 | function | - | |
onPopupScroll | 下拉列表滚动时的回调 | function | - | |
onSearch | 文本框值变化时回调 | function(value: string) | - | |
onSelect | 被选中时调用,参数为选中项的 value (或 key) 值 | function(string | number | LabeledValue, option: Option) | - |
注意,如果发现下拉菜单跟随页面滚动,或者需要在其他弹层中触发 Select,请尝试使用
getPopupContainer={triggerNode => triggerNode.parentElement}
将下拉弹层渲染节点固定在触发器的父元素中。
Select Methods#
名称 | 说明 | 版本 |
---|---|---|
blur() | 取消焦点 | |
focus() | 获取焦点 |
Option props#
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
className | Option 器类名 | string | - | |
disabled | 是否禁用 | boolean | false | |
title | 选项上的原生 title 提示 | string | - | |
value | 默认根据此属性值进行筛选 | string | number | - |
OptGroup props#
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
key | Key | string | - | |
label | 组名 | string | React.Element | - |
FAQ#
tag
模式下为何搜索有时会出现两个相同选项?#
这一般是 options
中的 label
和 value
不同导致的,你可以通过 optionFilterProp="label"
将过滤设置为展示值以避免这种情况。
点击 dropdownRender
里的内容浮层关闭怎么办?#
自定义内容点击时会关闭浮层,如果不喜欢关闭,可以添加 onMouseDown={e => e.preventDefault()}
进行阻止(更多详情见 #13448)。
自定义 Option 样式导致滚动异常怎么办?#
这是由于虚拟滚动默认选项高度为 32px
,如果你的选项高度小于该值则需要通过 listItemHeight
属性调整,而 listHeight
用于设置滚动容器高度:
<Select listItemHeight={10} listHeight={250} />
注意:listItemHeight
和 listHeight
为内部属性,如无必要,请勿修改该值。
为何无障碍测试会报缺失 aria-
属性?#
Select 无障碍辅助元素仅在弹窗展开时创建,因而当你在进行无障碍检测时请先打开下拉后再进行测试。对于 aria-label
与 aria-labelledby
属性缺失警告,请自行为 Select 组件添加相应无障碍属性。