2016-07-04 10:31:27 +08:00
|
|
|
---
|
|
|
|
order: 1
|
2017-02-20 21:39:07 +08:00
|
|
|
title:
|
2016-09-23 09:39:35 +08:00
|
|
|
zh-CN: 异步加载
|
|
|
|
en-US: Asynchronous loading
|
2016-07-04 10:31:27 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
匹配内容列表为异步返回时。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2016-09-01 18:12:12 +08:00
|
|
|
async
|
2016-07-04 10:31:27 +08:00
|
|
|
|
2017-02-13 10:55:53 +08:00
|
|
|
````jsx
|
2016-07-04 10:31:27 +08:00
|
|
|
import { Mention } from 'antd';
|
|
|
|
|
|
|
|
const users = ['afc163', 'benjycui', 'yiminghe', 'jljsj33', 'dqaria', 'RaoHai'];
|
2017-02-20 21:39:07 +08:00
|
|
|
|
|
|
|
class AsyncMention extends React.Component {
|
2017-02-20 21:47:57 +08:00
|
|
|
state = {
|
|
|
|
suggestions: [],
|
|
|
|
loading: false,
|
2017-02-20 21:39:07 +08:00
|
|
|
}
|
2017-02-20 22:21:14 +08:00
|
|
|
fetchSuggestions = (value, callback) => {
|
2016-07-04 10:31:27 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
callback(users.filter(item => item.indexOf(value) !== -1));
|
|
|
|
}, 500);
|
2017-02-20 21:39:07 +08:00
|
|
|
}
|
|
|
|
onSearchChange = (value) => {
|
2016-07-04 10:31:27 +08:00
|
|
|
this.fetchSuggestions(value, (suggestions) => {
|
|
|
|
this.setState({
|
|
|
|
suggestions,
|
|
|
|
loading: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.setState({
|
|
|
|
loading: true,
|
|
|
|
});
|
2017-02-20 21:39:07 +08:00
|
|
|
}
|
2016-07-04 10:31:27 +08:00
|
|
|
render() {
|
|
|
|
const { suggestions, loading } = this.state;
|
2016-07-21 15:40:13 +08:00
|
|
|
return (
|
|
|
|
<Mention
|
2016-10-31 15:39:03 +08:00
|
|
|
style={{ width: '100%', height: 100 }}
|
2016-07-21 15:40:13 +08:00
|
|
|
loading={loading}
|
|
|
|
suggestions={suggestions}
|
|
|
|
onSearchChange={this.onSearchChange}
|
|
|
|
/>
|
|
|
|
);
|
2017-02-20 21:39:07 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-04 10:31:27 +08:00
|
|
|
|
2017-02-20 21:39:07 +08:00
|
|
|
ReactDOM.render(<AsyncMention />, mountNode);
|
2016-07-04 10:31:27 +08:00
|
|
|
````
|