ant-design/components/auto-complete/demo/options.md

48 lines
1.0 KiB
Markdown
Raw Normal View History

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
也可以直接传 `AutoComplete.Option` 作为 `AutoComplete``children`,而非使用 `options`
2016-07-25 17:46:45 +08:00
## en-US
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
2016-07-25 17:46:45 +08:00
import { AutoComplete } from 'antd';
2022-05-21 22:14:15 +08:00
import React, { useState } from 'react';
2016-08-23 21:00:35 +08:00
const { Option } = AutoComplete;
2016-07-25 17:46:45 +08:00
const App: 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">
{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
export default App;
2019-05-07 14:57:32 +08:00
```