autoComplete.stories.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import React from 'react';
  2. import { storiesOf } from '@storybook/react';
  3. import AutoComplete from '../index';
  4. import { Avatar } from '../../index';
  5. const stories = storiesOf('AutoComplete', module);
  6. stories.add('Paginationdefault', () => (
  7. <>
  8. <AutoComplete data={[1, 2, '2']} onSelect={v => console.log(v)}/>
  9. </>
  10. ));
  11. import { IconSearch } from '@douyinfe/semi-icons';
  12. interface Person {
  13. name: string;
  14. email: string;
  15. abbr: string;
  16. color: string;
  17. }
  18. type Color = 'amber' | 'indigo' | 'cyan';
  19. interface DemoState {
  20. data: Person[];
  21. color: Color[];
  22. list: Person[];
  23. }
  24. class CustomOptionDemo extends React.Component<Record<string, any>, DemoState> {
  25. constructor(props) {
  26. super(props);
  27. this.state = {
  28. data: [],
  29. color: ['amber', 'indigo', 'cyan'],
  30. list: [
  31. { name: '夏可漫', email: '[email protected]', abbr: 'XK', color: 'amber' },
  32. { name: '申悦', email: '[email protected]', abbr: 'SY', color: 'indigo' },
  33. { name: '曲晨一', email: '[email protected]', abbr: 'CY', color: 'blue' },
  34. { name: '文嘉茂', email: '[email protected]', abbr: 'JM', color: 'cyan' },
  35. ],
  36. };
  37. }
  38. search(value) {
  39. let result;
  40. if (value) {
  41. result = this.state.list.map(item => {
  42. return { ...item, value: item.name, label: item.email };
  43. });
  44. } else {
  45. result = [];
  46. }
  47. this.setState({ data: result });
  48. }
  49. renderOption(item) {
  50. let optionStyle = {
  51. display: 'flex',
  52. };
  53. return (
  54. <>
  55. <Avatar color={item.color} size="small">
  56. {item.abbr}
  57. </Avatar>
  58. <div style={{ marginLeft: 4 }}>
  59. <div style={{ fontSize: 14, marginLeft: 4 }}>{item.name}</div>
  60. <div style={{ marginLeft: 4 }}>{item.email}</div>
  61. </div>
  62. </>
  63. );
  64. }
  65. render() {
  66. return (
  67. <AutoComplete<Person>
  68. data={this.state.data}
  69. prefix={<IconSearch />}
  70. style={{ width: '250px' }}
  71. renderSelectedItem={option => option.email}
  72. renderItem={this.renderOption}
  73. onSearch={this.search.bind(this)}
  74. onSelect={v => console.log(v)}
  75. ></AutoComplete>
  76. );
  77. }
  78. }