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
|
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
也可以直接传 `AutoComplete.Option` 作为 `AutoComplete` 的 `children`,而非使用 `options`。
|
2016-07-25 17:46:45 +08:00
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
You could pass `AutoComplete.Option` as children of `AutoComplete`, instead of using `options`。
|
2016-07-25 17:46:45 +08:00
|
|
|
|
2020-01-21 16:21:37 +08:00
|
|
|
```tsx
|
2020-01-22 12:11:49 +08:00
|
|
|
import React, { useState } from 'react';
|
2016-07-25 17:46:45 +08:00
|
|
|
import { AutoComplete } from 'antd';
|
2016-08-23 21:00:35 +08:00
|
|
|
|
2019-05-27 21:32:45 +08:00
|
|
|
const { Option } = AutoComplete;
|
2016-07-25 17:46:45 +08:00
|
|
|
|
2020-01-21 17:14:58 +08:00
|
|
|
const Complete: React.FC = () => {
|
2020-01-22 12:11:49 +08:00
|
|
|
const [result, setResult] = useState<string[]>([]);
|
2020-01-21 16:21:37 +08:00
|
|
|
const handleSearch = (value: string) => {
|
|
|
|
let res: 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 {
|
2020-01-21 16:21:37 +08:00
|
|
|
res = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
|
2016-07-25 17:46:45 +08:00
|
|
|
}
|
2020-01-21 16:21:37 +08:00
|
|
|
setResult(res);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2020-01-21 16:21:37 +08:00
|
|
|
return (
|
|
|
|
<AutoComplete style={{ width: 200 }} onSearch={handleSearch} placeholder="input here">
|
2020-05-09 19:32:02 +08:00
|
|
|
{result.map((email: string) => (
|
|
|
|
<Option key={email} value={email}>
|
|
|
|
{email}
|
|
|
|
</Option>
|
|
|
|
))}
|
2020-01-21 16:21:37 +08:00
|
|
|
</AutoComplete>
|
|
|
|
);
|
|
|
|
};
|
2016-07-25 17:46:45 +08:00
|
|
|
|
|
|
|
ReactDOM.render(<Complete />, mountNode);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|