index.jsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import React from 'react';
  2. import { debounce } from 'lodash';
  3. import { AutoComplete, Icon, Button } from '@douyinfe/semi-ui';
  4. import { IconSearch } from '@douyinfe/semi-icons';
  5. export default class ObjectDemo extends React.Component {
  6. constructor() {
  7. super();
  8. this.state = {
  9. list: [
  10. {
  11. value: 'douyin',
  12. label: 'douyin',
  13. email: '[email protected]',
  14. type: 2,
  15. },
  16. {
  17. value: 'hotsoon',
  18. label: 'huoshan',
  19. email: '[email protected]',
  20. type: 3,
  21. },
  22. {
  23. value: 'pipixia',
  24. label: 'pip',
  25. email: '[email protected]',
  26. },
  27. ],
  28. loading: false,
  29. };
  30. this.onSearch = this.onSearch.bind(this);
  31. this.handleSelect = this.handleSelect.bind(this);
  32. this.renderItem = this.renderItem.bind(this);
  33. this.renderSelectedItem = this.renderSelectedItem.bind(this);
  34. this.search = debounce(this.search.bind(this), 200);
  35. }
  36. onSearch(inputValue) {
  37. this.setState({
  38. loading: true,
  39. });
  40. this.search(inputValue);
  41. }
  42. search(inputValue) {
  43. let { list } = this.state;
  44. const newList = list.map(item => {
  45. let num = Math.random()
  46. .toString()
  47. .slice(2, 5);
  48. let option = `${inputValue }-${ num}`;
  49. return { ...item, label: `名称:${ option}`, value: option };
  50. });
  51. this.setState({
  52. list: newList,
  53. loading: false,
  54. });
  55. }
  56. handleSelect(value) {
  57. console.log(value);
  58. }
  59. renderItem(item) {
  60. return (
  61. <div>
  62. <div>{item.label}</div>
  63. <div>email: {item.email}</div>
  64. <div
  65. style={{
  66. color: 'pink',
  67. }}
  68. >
  69. value: {item.value}
  70. </div>
  71. </div>
  72. );
  73. }
  74. renderSelectedItem(item) {
  75. // 注意:与Select不同,此处只能返回String类型的值,不能返回ReactNode
  76. return item.value;
  77. }
  78. render() {
  79. const { loading } = this.state;
  80. return (
  81. <div>
  82. <AutoComplete
  83. data={this.state.list}
  84. style={{
  85. width: 250,
  86. }}
  87. prefix={<IconSearch />}
  88. onSearch={this.onSearch}
  89. loading={loading}
  90. onChangeWithObject
  91. renderItem={this.renderItem}
  92. renderSelectedItem={this.renderSelectedItem}
  93. onSelect={this.handleSelect}
  94. triggerRender={({ value, inputValue, onFocus }) => <Button onFocus={onFocus}>{inputValue}</Button>}
  95. />
  96. </div>
  97. );
  98. }
  99. }