AutoComplete自动完成

    需要自动完成时。

    基本使用

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

    AutoComplete自动完成 - 图2

    自定义输入组件。

    1. const { TextArea } = Input;
    2. function onSelect(value) {
    3. console.log('onSelect', value);
    4. }
    5. class Complete extends React.Component {
    6. state = {
    7. dataSource: [],
    8. };
    9. handleSearch = value => {
    10. this.setState({
    11. dataSource: !value ? [] : [value, value + value, value + value + value],
    12. });
    13. };
    14. handleKeyPress = ev => {
    15. console.log('handleKeyPress', ev);
    16. };
    17. render() {
    18. const { dataSource } = this.state;
    19. return (
    20. <AutoComplete
    21. dataSource={dataSource}
    22. style={{ width: 200 }}
    23. onSelect={onSelect}
    24. onSearch={this.handleSearch}
    25. >
    26. <TextArea
    27. placeholder="input here"
    28. className="custom"
    29. style={{ height: 50 }}
    30. onKeyPress={this.handleKeyPress}
    31. />
    32. </AutoComplete>
    33. );
    34. }
    35. }
    36. ReactDOM.render(<Complete />, mountNode);

    查询模式: 确定类目 示例。

    1. import { Icon, Input, AutoComplete } from 'antd';
    2. const { Option, OptGroup } = AutoComplete;
    3. const dataSource = [
    4. {
    5. title: 'Libraries',
    6. children: [
    7. {
    8. title: 'AntDesign',
    9. count: 10000,
    10. },
    11. {
    12. title: 'AntDesign UI',
    13. count: 10600,
    14. },
    15. ],
    16. },
    17. {
    18. title: 'Solutions',
    19. children: [
    20. {
    21. title: 'AntDesign UI',
    22. count: 60100,
    23. },
    24. {
    25. title: 'AntDesign',
    26. count: 30010,
    27. ],
    28. },
    29. {
    30. title: 'Articles',
    31. children: [
    32. {
    33. count: 100000,
    34. },
    35. ],
    36. },
    37. ];
    38. function renderTitle(title) {
    39. return (
    40. <span>
    41. {title}
    42. <a
    43. style={{ float: 'right' }}
    44. href="https://www.google.com/search?q=antd"
    45. target="_blank"
    46. rel="noopener noreferrer"
    47. >
    48. more
    49. </a>
    50. </span>
    51. );
    52. }
    53. const options = dataSource
    54. .map(group => (
    55. <OptGroup key={group.title} label={renderTitle(group.title)}>
    56. {group.children.map(opt => (
    57. <Option key={opt.title} value={opt.title}>
    58. {opt.title}
    59. <span className="certain-search-item-count">{opt.count} people</span>
    60. </Option>
    61. ))}
    62. </OptGroup>
    63. ))
    64. .concat([
    65. <Option disabled key="all" className="show-all">
    66. <a href="https://www.google.com/search?q=antd" target="_blank" rel="noopener noreferrer">
    67. View all results
    68. </a>
    69. </Option>,
    70. ]);
    71. function Complete() {
    72. return (
    73. <div className="certain-category-search-wrapper" style={{ width: 250 }}>
    74. <AutoComplete
    75. className="certain-category-search"
    76. dropdownClassName="certain-category-search-dropdown"
    77. dropdownMatchSelectWidth={false}
    78. dropdownStyle={{ width: 300 }}
    79. size="large"
    80. style={{ width: '100%' }}
    81. dataSource={options}
    82. placeholder="input here"
    83. optionLabelProp="value"
    84. >
    85. <Input suffix={<Icon type="search" className="certain-category-icon" />} />
    86. </AutoComplete>
    87. </div>
    88. );
    89. }
    90. ReactDOM.render(<Complete />, mountNode);

    AutoComplete自动完成 - 图4

    也可以直接传 AutoComplete.Option 作为 AutoCompletechildren,而非使用 dataSource

    1. import { AutoComplete } from 'antd';
    2. const { Option } = AutoComplete;
    3. class Complete extends React.Component {
    4. state = {
    5. result: [],
    6. handleSearch = value => {
    7. let result;
    8. if (!value || value.indexOf('@') >= 0) {
    9. } else {
    10. result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
    11. }
    12. this.setState({ result });
    13. };
    14. render() {
    15. const { result } = this.state;
    16. const children = result.map(email => <Option key={email}>{email}</Option>);
    17. return (
    18. <AutoComplete style={{ width: 200 }} onSearch={this.handleSearch} placeholder="input here">
    19. {children}
    20. </AutoComplete>
    21. );
    22. }
    23. }
    24. ReactDOM.render(<Complete />, mountNode);

    不区分大小写的 AutoComplete

    1. import { AutoComplete } from 'antd';
    2. const dataSource = ['Burns Bay Road', 'Downing Street', 'Wall Street'];
    3. function Complete() {
    4. return (
    5. <AutoComplete
    6. style={{ width: 200 }}
    7. dataSource={dataSource}
    8. placeholder="try to type `b`"
    9. filterOption={(inputValue, option) =>
    10. option.props.children.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1
    11. }
    12. />
    13. );
    14. }
    15. ReactDOM.render(<Complete />, mountNode);

    AutoComplete自动完成 - 图6

    查询模式: 不确定类目 示例。

    1. .global-search-wrapper {
    2. padding-right: 50px;
    3. }
    4. .global-search {
    5. width: 100%;
    6. }
    7. .global-search.ant-select-auto-complete .ant-select-selection--single {
    8. margin-right: -46px;
    9. }
    10. .global-search.ant-select-auto-complete .ant-input-affix-wrapper .ant-input:not(:last-child) {
    11. padding-right: 62px;
    12. }
    13. .global-search.ant-select-auto-complete .ant-input-affix-wrapper .ant-input-suffix button {
    14. border-top-left-radius: 0;
    15. border-bottom-left-radius: 0;
    16. }
    17. .global-search-item {
    18. display: flex;
    19. }
    20. .global-search-item-desc {
    21. flex: auto;
    22. text-overflow: ellipsis;
    23. overflow: hidden;
    24. }
    25. .global-search-item-count {
    26. flex: none;
    27. }
    1. const dataSource = ['12345', '23456', '34567'];
    名称描述
    blur()移除焦点
    focus()获取焦点