group.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React from 'react';
  2. import { Table, Avatar } from '@douyinfe/semi-ui';
  3. const DAY = 24 * 60 * 60 * 1000;
  4. const figmaIconUrl = 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/figma-icon.png';
  5. const columns = [
  6. {
  7. title: '标题',
  8. dataIndex: 'name',
  9. width: 400,
  10. render: (text, record, index) => {
  11. return (
  12. <div>
  13. <Avatar size="small" shape="square" src={figmaIconUrl} style={{ marginRight: 12 }}></Avatar>
  14. {text}
  15. </div>
  16. );
  17. },
  18. filters: [
  19. {
  20. text: 'Semi Design 设计稿',
  21. value: 'Semi Design 设计稿',
  22. },
  23. {
  24. text: 'Semi Pro 设计稿',
  25. value: 'Semi Pro 设计稿',
  26. },
  27. ],
  28. onFilter: (value, record) => record.name.includes(value),
  29. },
  30. {
  31. title: '大小',
  32. dataIndex: 'size',
  33. sorter: (a, b) => a.size - b.size > 0 ? 1 : -1,
  34. render: (text) => `${text} KB`
  35. },
  36. {
  37. title: '所有者',
  38. dataIndex: 'owner',
  39. render: (text, record, index) => {
  40. return (
  41. <div>
  42. <Avatar size="small" color={record.avatarBg} style={{ marginRight: 4 }}>{typeof text === 'string' && text.slice(0, 1)}</Avatar>
  43. {text}
  44. </div>
  45. );
  46. }
  47. },
  48. {
  49. title: '更新日期',
  50. dataIndex: 'updateTime',
  51. sorter: (a, b) => a.updateTime - b.updateTime > 0 ? 1 : -1,
  52. render: (value) => {
  53. return dateFns.format(new Date(value), 'yyyy-MM-dd');
  54. }
  55. }
  56. ];
  57. const getData = () => {
  58. const data = [];
  59. for (let i = 0; i < 46; i++) {
  60. const isSemiDesign = i % 2 === 0;
  61. const randomNumber = i * 1000 % 19 + 100;
  62. data.push({
  63. key: '' + i,
  64. name: isSemiDesign ? `Semi Design 设计稿${i}.fig` : `Semi Pro 设计稿${i}.fig`,
  65. owner: isSemiDesign ? '姜鹏志' : '郝宣',
  66. size: randomNumber,
  67. updateTime: new Date().valueOf() + randomNumber * DAY,
  68. avatarBg: isSemiDesign ? 'grey' : 'red',
  69. });
  70. }
  71. return data;
  72. };
  73. const data = getData();
  74. function Demo() {
  75. const rowKey = record => `${record.owner && record.owner.toLowerCase()}-${record.name && record.name.toLowerCase()}`;
  76. return (
  77. <div style={{ padding: '20px 0px' }}>
  78. <Table
  79. dataSource={data}
  80. rowKey={rowKey}
  81. groupBy={'size'}
  82. columns={columns}
  83. renderGroupSection={groupKey => <strong>根据文件大小分组 {groupKey} KB</strong>}
  84. onGroupedRow={(group, index) => {
  85. return {
  86. // onMouseEnter: () => {
  87. // console.log(`Grouped row mouse enter: `, group, index);
  88. // },
  89. // onMouseLeave: () => {
  90. // console.log(`Grouped row mouse leave: `, group, index);
  91. // },
  92. onClick: e => { console.log(`Grouped row clicked: `, group, index); }
  93. };
  94. }}
  95. clickGroupedRowToExpand // if you want to click the entire row to expand
  96. scroll={{ y: 480 }}
  97. />
  98. </div>
  99. );
  100. }
  101. render(Demo);