ant-design/components/auto-complete/demo/basic.md
二货机器人 330a952a98
docs: Update AutoComplete first demo with control mode & add faq (#18432)
* add FAQ

* update demo

* add miss prop
2019-08-23 12:18:27 +08:00

1.3 KiB

order title
0
zh-CN en-US
基本使用 Basic Usage

zh-CN

基本使用。通过 dataSource 设置自动完成的数据源

en-US

Basic Usage, set data source of autocomplete with dataSource property.

import { AutoComplete } from 'antd';

function onSelect(value) {
  console.log('onSelect', value);
}

class Complete extends React.Component {
  state = {
    value: '',
    dataSource: [],
  };

  onSearch = searchText => {
    this.setState({
      dataSource: !searchText ? [] : [searchText, searchText.repeat(2), searchText.repeat(3)],
    });
  };

  onChange = value => {
    this.setState({ value });
  };

  render() {
    const { dataSource, value } = this.state;
    return (
      <div>
        <AutoComplete
          dataSource={dataSource}
          style={{ width: 200 }}
          onSelect={onSelect}
          onSearch={this.onSearch}
          placeholder="input here"
        />
        <br />
        <br />
        <AutoComplete
          value={value}
          dataSource={dataSource}
          style={{ width: 200 }}
          onSelect={onSelect}
          onSearch={this.onSearch}
          onChange={this.onChange}
          placeholder="control mode"
        />
      </div>
    );
  }
}

ReactDOM.render(<Complete />, mountNode);