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

77 lines
1.4 KiB
Markdown
Raw Normal View History

2016-07-25 17:46:45 +08:00
---
order: 0
2016-08-23 21:00:35 +08:00
title:
2016-07-25 17:46:45 +08:00
zh-CN: 基本使用
en-US: Basic Usage
---
## zh-CN
基本使用。通过 options 设置自动完成的数据源
2016-07-25 17:46:45 +08:00
## en-US
Basic Usage, set data source of autocomplete with `options` property.
2016-07-25 17:46:45 +08:00
```tsx
2016-07-25 17:46:45 +08:00
import { AutoComplete } from 'antd';
function onSelect(value) {
console.log('onSelect', value);
}
function mockVal(str: string, repeat: number = 1) {
return {
value: str.repeat(repeat),
};
}
class Complete extends React.Component {
state = {
value: '',
options: [],
2019-05-07 14:57:32 +08:00
};
onSearch = searchText => {
2016-07-25 17:46:45 +08:00
this.setState({
options: !searchText
? []
: [mockVal(searchText), mockVal(searchText, 2), mockVal(searchText, 3)],
2016-07-25 17:46:45 +08:00
});
2019-05-07 14:57:32 +08:00
};
onChange = value => {
this.setState({ value });
};
2016-07-25 17:46:45 +08:00
render() {
const { options, value } = this.state;
return (
<div>
<AutoComplete
options={[]}
style={{ width: 200 }}
onSelect={onSelect}
onSearch={this.onSearch}
placeholder="input here"
/>
<br />
<br />
<AutoComplete
value={value}
options={options}
style={{ width: 200 }}
onSelect={onSelect}
onSearch={this.onSearch}
onChange={this.onChange}
placeholder="control mode"
/>
</div>
);
}
}
2016-07-25 17:46:45 +08:00
ReactDOM.render(<Complete />, mountNode);
2019-05-07 14:57:32 +08:00
```