2016-07-25 17:46:45 +08:00
|
|
|
---
|
|
|
|
order: 2
|
2016-08-23 21:00:35 +08:00
|
|
|
title:
|
2016-07-25 17:46:45 +08:00
|
|
|
zh-CN: 自定义选项
|
|
|
|
en-US: Customized
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
2022-09-19 11:28:36 +08:00
|
|
|
可以返回自定义的 `Option` label
|
2016-07-25 17:46:45 +08:00
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2022-09-19 11:28:36 +08:00
|
|
|
You could set custom `Option` label
|
2016-07-25 17:46:45 +08:00
|
|
|
|
2020-01-21 16:21:37 +08:00
|
|
|
```tsx
|
2016-07-25 17:46:45 +08:00
|
|
|
import { AutoComplete } from 'antd';
|
2022-05-23 14:37:16 +08:00
|
|
|
import React, { useState } from 'react';
|
2016-08-23 21:00:35 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
2022-09-19 11:28:36 +08:00
|
|
|
const [options, setOptions] = useState<{ value: string; label: string }[]>([]);
|
2022-05-19 09:46:26 +08:00
|
|
|
|
2020-01-21 16:21:37 +08:00
|
|
|
const handleSearch = (value: string) => {
|
2022-09-19 11:28:36 +08:00
|
|
|
let res: { value: string; label: string }[] = [];
|
2016-07-25 17:46:45 +08:00
|
|
|
if (!value || value.indexOf('@') >= 0) {
|
2020-01-21 16:21:37 +08:00
|
|
|
res = [];
|
2016-07-25 17:46:45 +08:00
|
|
|
} else {
|
2022-09-19 11:28:36 +08:00
|
|
|
res = ['gmail.com', '163.com', 'qq.com'].map(domain => ({
|
|
|
|
value,
|
|
|
|
label: `${value}@${domain}`,
|
|
|
|
}));
|
2016-07-25 17:46:45 +08:00
|
|
|
}
|
2022-09-19 11:28:36 +08:00
|
|
|
setOptions(res);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2022-05-19 09:46:26 +08:00
|
|
|
|
2020-01-21 16:21:37 +08:00
|
|
|
return (
|
2022-09-19 11:28:36 +08:00
|
|
|
<AutoComplete
|
|
|
|
style={{ width: 200 }}
|
|
|
|
onSearch={handleSearch}
|
|
|
|
placeholder="input here"
|
|
|
|
options={options}
|
|
|
|
/>
|
2020-01-21 16:21:37 +08:00
|
|
|
);
|
|
|
|
};
|
2016-07-25 17:46:45 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|