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
|
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
基本使用。通过 options 设置自动完成的数据源
|
2016-07-25 17:46:45 +08:00
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
Basic Usage, set data source of autocomplete with `options` property.
|
2016-07-25 17:46:45 +08:00
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
```tsx
|
2020-01-21 16:21:37 +08:00
|
|
|
import React, { useState, FC } from 'react';
|
2016-07-25 17:46:45 +08:00
|
|
|
import { AutoComplete } from 'antd';
|
|
|
|
|
2020-01-21 16:21:37 +08:00
|
|
|
const mockVal = (str: string, repeat: number = 1) => {
|
2019-09-12 20:15:17 +08:00
|
|
|
return {
|
|
|
|
value: str.repeat(repeat),
|
|
|
|
};
|
2020-01-21 16:21:37 +08:00
|
|
|
};
|
|
|
|
const Complete: FC = () => {
|
|
|
|
const [value, setValue] = useState('');
|
|
|
|
const [options, setOptions] = useState<{ value: string }[]>([]);
|
|
|
|
const onSearch = (searchText: string) => {
|
|
|
|
setOptions(
|
|
|
|
!searchText ? [] : [mockVal(searchText), mockVal(searchText, 2), mockVal(searchText, 3)],
|
|
|
|
);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2020-01-21 16:21:37 +08:00
|
|
|
const onSelect = (data: string) => {
|
|
|
|
console.log('onSelect', data);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2020-01-21 16:21:37 +08:00
|
|
|
const onChange = (data: string) => {
|
|
|
|
setValue(data);
|
2019-08-23 12:18:27 +08:00
|
|
|
};
|
2020-01-21 16:21:37 +08:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<AutoComplete
|
|
|
|
options={options}
|
|
|
|
style={{ width: 200 }}
|
|
|
|
onSelect={onSelect}
|
|
|
|
onSearch={onSearch}
|
|
|
|
placeholder="input here"
|
|
|
|
/>
|
|
|
|
<br />
|
|
|
|
<br />
|
|
|
|
<AutoComplete
|
|
|
|
value={value}
|
|
|
|
options={options}
|
|
|
|
style={{ width: 200 }}
|
|
|
|
onSelect={onSelect}
|
|
|
|
onSearch={onSearch}
|
|
|
|
onChange={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
|
|
|
```
|