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

49 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`,而非使用 `dataSource`
2016-07-25 17:46:45 +08:00
## en-US
You could pass `AutoComplete.Option` as children of `AutoComplete`, instead of using `dataSource`
2016-07-25 17:46:45 +08:00
2019-05-07 14:57:32 +08:00
```jsx
2016-07-25 17:46:45 +08:00
import { AutoComplete } from 'antd';
2016-08-23 21:00:35 +08:00
const { Option } = AutoComplete;
2016-07-25 17:46:45 +08:00
class Complete extends React.Component {
state = {
result: [],
2019-05-07 14:57:32 +08:00
};
2019-05-07 14:57:32 +08:00
handleSearch = value => {
let result;
2016-07-25 17:46:45 +08:00
if (!value || value.indexOf('@') >= 0) {
result = [];
2016-07-25 17:46:45 +08:00
} else {
result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
2016-07-25 17:46:45 +08:00
}
this.setState({ result });
2019-05-07 14:57:32 +08:00
};
2016-07-25 17:46:45 +08:00
render() {
const { result } = this.state;
2018-11-28 15:00:03 +08:00
const children = result.map(email => <Option key={email}>{email}</Option>);
return (
2019-05-07 14:57:32 +08:00
<AutoComplete style={{ width: 200 }} onSearch={this.handleSearch} placeholder="input here">
{children}
</AutoComplete>
);
}
}
2016-07-25 17:46:45 +08:00
ReactDOM.render(<Complete />, mountNode);
2019-05-07 14:57:32 +08:00
```