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

    AutoComplete 自动完成 - 图4

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

    1. import { AutoComplete } from 'antd';
    2. const Option = AutoComplete.Option;
    3. class Complete extends React.Component {
    4. state = {
    5. result: [],
    6. };
    7. handleSearch = value => {
    8. if (!value || value.indexOf('@') >= 0) {
    9. result = [];
    10. } else {
    11. result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
    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 {
    14. right: 0;
    15. }
    16. .global-search.ant-select-auto-complete .ant-input-affix-wrapper .ant-input-suffix button {
    17. border-top-left-radius: 0;
    18. border-bottom-left-radius: 0;
    19. }
    20. .global-search-item {
    21. display: flex;
    22. }
    23. .global-search-item-desc {
    24. flex: auto;
    25. text-overflow: ellipsis;
    26. overflow: hidden;
    27. }
    28. .global-search-item-count {
    29. flex: none;
    30. }
    1. const dataSource = ['12345', '23456', '34567'];
    2. <AutoComplete dataSource={dataSource} />;
    名称描述
    blur()移除焦点
    focus()获取焦点