AutoComplete自动完成

输入框自动完成功能。

何时使用

需要自动完成时。

代码演示

AutoComplete 自动完成 - 图1

基本使用

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

  1. import { AutoComplete } from 'choerodon-ui';
  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 ? [] : [
  12. value,
  13. value + value,
  14. value + value + value,
  15. ],
  16. });
  17. }
  18. render() {
  19. const { dataSource } = this.state;
  20. return (
  21. <AutoComplete
  22. dataSource={dataSource}
  23. style={{ width: 200 }}
  24. onSelect={onSelect}
  25. onSearch={this.handleSearch}
  26. placeholder="input here"
  27. />
  28. );
  29. }
  30. }
  31. ReactDOM.render(<Complete />, mountNode);

AutoComplete 自动完成 - 图2

自定义输入组件

自定义输入组件。

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

AutoComplete 自动完成 - 图3

查询模式 - 确定类目

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

  1. import { Button, Input, AutoComplete } from 'choerodon-ui';
  2. const Option = AutoComplete.Option;
  3. const OptGroup = AutoComplete.OptGroup;
  4. const dataSource = [{
  5. title: '话题',
  6. children: [{
  7. title: 'AntDesign',
  8. count: 10000,
  9. }, {
  10. title: 'AntDesign UI',
  11. count: 10600,
  12. }],
  13. }, {
  14. title: '问题',
  15. children: [{
  16. title: 'AntDesign UI 有多好',
  17. count: 60100,
  18. }, {
  19. title: 'AntDesign 是啥',
  20. count: 30010,
  21. }],
  22. }, {
  23. title: '文章',
  24. children: [{
  25. title: 'AntDesign 是一个设计语言',
  26. count: 100000,
  27. }],
  28. }];
  29. function renderTitle(title) {
  30. return (
  31. <span>
  32. {title}
  33. <a
  34. style={{ float: 'right' }}
  35. href="https://www.google.com/search?q=choerodon"
  36. target="_blank"
  37. rel="noopener noreferrer"
  38. >更多
  39. </a>
  40. </span>
  41. );
  42. }
  43. const options = dataSource.map(group => (
  44. <OptGroup
  45. key={group.title}
  46. label={renderTitle(group.title)}
  47. >
  48. {group.children.map(opt => (
  49. <Option key={opt.title} value={opt.title}>
  50. {opt.title}
  51. <span className="certain-search-item-count">{opt.count} 关注</span>
  52. </Option>
  53. ))}
  54. </OptGroup>
  55. )).concat([
  56. <Option disabled key="all" className="show-all">
  57. <a
  58. href="https://www.google.com/search?q=choerodon"
  59. target="_blank"
  60. rel="noopener noreferrer"
  61. >
  62. 查看所有结果
  63. </a>
  64. </Option>,
  65. ]);
  66. function Complete() {
  67. return (
  68. <div className="certain-category-search-wrapper" style={{ width: 250 }}>
  69. <AutoComplete
  70. className="certain-category-search"
  71. dropdownClassName="certain-category-search-dropdown"
  72. dropdownMatchSelectWidth={false}
  73. dropdownStyle={{ width: 300 }}
  74. style={{ width: '100%' }}
  75. dataSource={options}
  76. placeholder="input here"
  77. optionLabelProp="value"
  78. >
  79. <Input suffix={<Button shape="circle" size="small" funcType="flat" icon="search" />} />
  80. </AutoComplete>
  81. </div>
  82. );
  83. }
  84. ReactDOM.render(<Complete />, mountNode);
  1. .certain-category-search.c7n-select-auto-complete .c7n-input-affix-wrapper .c7n-input-suffix {
  2. right: 12px;
  3. }
  4. .certain-category-search-dropdown .c7n-select-dropdown-menu-item-group-title {
  5. color: #666;
  6. font-weight: bold;
  7. }
  8. .certain-category-search-dropdown .c7n-select-dropdown-menu-item-group {
  9. border-bottom: 1px solid #F6F6F6;
  10. }
  11. .certain-category-search-dropdown .c7n-select-dropdown-menu-item {
  12. padding-left: 16px;
  13. }
  14. .certain-category-search-dropdown .c7n-select-dropdown-menu-item.show-all {
  15. text-align: center;
  16. cursor: default;
  17. }
  18. .certain-category-search-dropdown .c7n-select-dropdown-menu {
  19. max-height: 300px;
  20. }
  21. .certain-search-item-count {
  22. position: absolute;
  23. color: #999;
  24. right: 16px;
  25. }
  26. .certain-category-search.c7n-select-focused .certain-category-icon {
  27. color: #108ee9;
  28. }
  29. .certain-category-icon {
  30. color: #6E6E6E;
  31. transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  32. font-size: 16px;
  33. }

AutoComplete 自动完成 - 图4

自定义选项

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

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

AutoComplete 自动完成 - 图5

不区分大小写

不区分大小写的 AutoComplete

  1. import { AutoComplete } from 'choerodon-ui';
  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) => option.props.children.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1}
  10. />
  11. );
  12. }
  13. ReactDOM.render(<Complete />, mountNode);

AutoComplete 自动完成 - 图6

查询模式 - 不确定类目

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

  1. import { Button, Input, AutoComplete } from 'choerodon-ui';
  2. const Option = AutoComplete.Option;
  3. function onSelect(value) {
  4. console.log('onSelect', value);
  5. }
  6. function getRandomInt(max, min = 0) {
  7. return Math.floor(Math.random() * (max - min + 1)) + min; // eslint-disable-line no-mixed-operators
  8. }
  9. function searchResult(query) {
  10. return (new Array(getRandomInt(5))).join('.').split('.')
  11. .map((item, idx) => ({
  12. query,
  13. category: `${query}${idx}`,
  14. count: getRandomInt(200, 100),
  15. }));
  16. }
  17. function renderOption(item) {
  18. return (
  19. <Option key={item.category} text={item.category}>
  20. {item.query}
  21. <a
  22. href={`https://s.taobao.com/search?q=${item.query}`}
  23. target="_blank"
  24. rel="noopener noreferrer"
  25. >
  26. {item.category}
  27. </a>
  28. 区块中
  29. <span className="global-search-item-count">约 {item.count} 个结果</span>
  30. </Option>
  31. );
  32. }
  33. class Complete extends React.Component {
  34. state = {
  35. dataSource: [],
  36. }
  37. handleSearch = (value) => {
  38. this.setState({
  39. dataSource: value ? searchResult(value) : [],
  40. });
  41. }
  42. render() {
  43. const { dataSource } = this.state;
  44. return (
  45. <div className="global-search-wrapper" style={{ width: 300 }}>
  46. <AutoComplete
  47. className="global-search"
  48. style={{ width: '100%' }}
  49. dataSource={dataSource.map(renderOption)}
  50. onSelect={onSelect}
  51. onSearch={this.handleSearch}
  52. placeholder="input here"
  53. optionLabelProp="text"
  54. >
  55. <Input
  56. suffix={(
  57. <Button size="small" shape="circle" type="primary" icon="search" />
  58. )}
  59. />
  60. </AutoComplete>
  61. </div>
  62. );
  63. }
  64. }
  65. ReactDOM.render(<Complete />, mountNode);
  1. .global-search-wrapper {
  2. padding-right: 50px;
  3. }
  4. .global-search {
  5. width: 100%;
  6. }
  7. .global-search.c7n-select-auto-complete .c7n-select-selection--single {
  8. margin-right: -46px;
  9. }
  10. .global-search.c7n-select-auto-complete .c7n-input-affix-wrapper .c7n-input:not(:last-child) {
  11. padding-right: 62px;
  12. }
  13. .global-search.c7n-select-auto-complete .c7n-input-affix-wrapper .c7n-input-suffix {
  14. right: 0;
  15. }
  16. .global-search-item-count {
  17. position: absolute;
  18. right: 16px;
  19. }

API

  1. const dataSource = ['12345', '23456', '34567'];
  2. <AutoComplete dataSource={dataSource} />
参数说明类型默认值
allowClear支持清除, 单选模式有效booleanfalse
autoFocus自动获取焦点booleanfalse
backfill使用键盘选择选项的时候把选中项回填到输入框中booleanfalse
children (自动完成的数据源)自动完成的数据源React.ReactElement / Array<React.ReactElement>-
children (自定义输入框)自定义输入框HTMLInputElement / HTMLTextAreaElement / React.ReactElement<Input />
dataSource自动完成的数据源DataSourceItemType[]
defaultActiveFirstOption是否默认高亮第一个选项。booleantrue
defaultValue指定默认选中的条目string|string[]|{ key: string, label: string|ReactNode }|Array<{ key: string, label: string|ReactNode}>
disabled是否禁用booleanfalse
filterOption是否根据输入项进行筛选。当其为一个函数时,会接收 inputValue option 两个参数,当 option 符合筛选条件时,应返回 true,反之则返回 falseboolean or function(inputValue, option)true
optionLabelProp回填到选择框的 Option 的属性值,默认是 Option 的子元素。比如在子元素需要高亮效果时,此值可以设为 valuestringchildren
placeholder输入框提示string-
value指定当前选中的条目string|string[]|{ key: string, label: string|ReactNode }|Array<{ key: string, label: string|ReactNode }>
onChange选中 option,或 input 的 value 变化时,调用此函数function(value)
onSearch搜索补全项的时候调用function(value)
onSelect被选中时调用,参数为选中项的 value 值function(value, option)

方法

名称描述
blur()移除焦点
focus()获取焦点