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
|
|
|
|
|
2016-09-22 14:40:34 +08:00
|
|
|
也可以直接传 `AutoComplete.Option` 作为 `AutoComplete` 的 `children`,而非使用 `dataSource`。
|
2016-07-25 17:46:45 +08:00
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2016-09-22 14:40:34 +08:00
|
|
|
You could pass `AutoComplete.Option` as children of `AutoComplete`, instead of using `dataSource`。
|
2016-07-25 17:46:45 +08:00
|
|
|
|
2017-01-19 15:19:03 +08:00
|
|
|
````__react
|
2016-07-25 17:46:45 +08:00
|
|
|
import { AutoComplete } from 'antd';
|
2016-08-23 21:00:35 +08:00
|
|
|
|
2016-07-25 17:46:45 +08:00
|
|
|
const Option = AutoComplete.Option;
|
|
|
|
|
|
|
|
const Complete = React.createClass({
|
|
|
|
getInitialState() {
|
|
|
|
return {
|
2016-09-21 17:14:58 +08:00
|
|
|
result: [],
|
2016-07-25 17:46:45 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
handleChange(value) {
|
2016-09-21 17:14:58 +08:00
|
|
|
let result;
|
2016-07-25 17:46:45 +08:00
|
|
|
if (!value || value.indexOf('@') >= 0) {
|
2016-09-21 17:14:58 +08:00
|
|
|
result = [];
|
2016-07-25 17:46:45 +08:00
|
|
|
} else {
|
2016-09-21 17:14:58 +08:00
|
|
|
result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
|
2016-07-25 17:46:45 +08:00
|
|
|
}
|
2016-09-21 17:14:58 +08:00
|
|
|
this.setState({ result });
|
2016-07-25 17:46:45 +08:00
|
|
|
},
|
|
|
|
render() {
|
2016-09-21 17:14:58 +08:00
|
|
|
const { result } = this.state;
|
2016-09-22 14:40:34 +08:00
|
|
|
const children = result.map((email) => {
|
2016-09-21 17:14:58 +08:00
|
|
|
return <Option key={email}>{email}</Option>;
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<AutoComplete
|
|
|
|
style={{ width: 200 }}
|
|
|
|
onChange={this.handleChange}
|
2016-12-03 15:28:47 +08:00
|
|
|
placeholder="input here"
|
2016-09-22 14:40:34 +08:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</AutoComplete>
|
2016-09-21 17:14:58 +08:00
|
|
|
);
|
2016-07-25 17:46:45 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
ReactDOM.render(<Complete />, mountNode);
|
|
|
|
````
|