span.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import React, { useState, useMemo } from 'react';
  2. import { Table, Avatar } from '@douyinfe/semi-ui';
  3. import * as dateFns from 'date-fns';
  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. const renderObject = {};
  12. const children = (
  13. <div>
  14. <Avatar size="small" shape="square" src={figmaIconUrl} style={{ marginRight: 12 }}></Avatar>
  15. {text}
  16. </div>
  17. );
  18. renderObject.children = children;
  19. if (index === 0) {
  20. renderObject.props = {
  21. colSpan: 4,
  22. };
  23. }
  24. if (index === 1) {
  25. renderObject.props = {
  26. rowSpan: 2,
  27. };
  28. }
  29. if (index === 2) {
  30. renderObject.props = {
  31. rowSpan: 0,
  32. };
  33. }
  34. return renderObject;
  35. },
  36. },
  37. {
  38. title: '大小',
  39. dataIndex: 'size',
  40. render: (text, record, index) => {
  41. if (index === 0) {
  42. return {
  43. children: `${text} KB`,
  44. props: {
  45. colSpan: 0,
  46. }
  47. };
  48. }
  49. if (index === 1) {
  50. return {
  51. children: `${text} KB`,
  52. props: {
  53. rowSpan: 2,
  54. }
  55. };
  56. }
  57. if (index === 2) {
  58. return {
  59. children: `${text} KB`,
  60. props: {
  61. rowSpan: 0,
  62. }
  63. };
  64. }
  65. return `${text} KB`;
  66. }
  67. },
  68. {
  69. title: '所有者',
  70. dataIndex: 'owner',
  71. render: (text, record, index) => {
  72. const children = (
  73. <div>
  74. <Avatar size="small" color={record.avatarBg} style={{ marginRight: 4 }}>{typeof text === 'string' && text.slice(0, 1)}</Avatar>
  75. {text}
  76. </div>
  77. );
  78. if (index === 0) {
  79. return {
  80. children,
  81. props: {
  82. colSpan: 0,
  83. }
  84. };
  85. }
  86. return children;
  87. }
  88. },
  89. {
  90. title: '更新日期',
  91. dataIndex: 'updateTime',
  92. sorter: (a, b) => a.updateTime - b.updateTime > 0 ? 1 : -1,
  93. render: (value, record, index) => {
  94. const children = dateFns.format(new Date(value), 'yyyy-MM-dd');
  95. if (index === 0) {
  96. return {
  97. children,
  98. props: {
  99. colSpan: 0
  100. }
  101. };
  102. }
  103. if (index === 1) {
  104. return {
  105. children,
  106. props: {
  107. rowSpan: 2
  108. }
  109. };
  110. }
  111. if (index === 2) {
  112. return {
  113. children,
  114. props: {
  115. rowSpan: 0
  116. }
  117. };
  118. }
  119. return children;
  120. }
  121. }
  122. ];
  123. const DAY = 24 * 60 * 60 * 1000;
  124. function App() {
  125. const [dataSource, setData] = useState([]);
  126. const getData = (total) => {
  127. const data = [];
  128. for (let i = 0; i < total; i++) {
  129. const isSemiDesign = i % 2 === 0;
  130. const randomNumber = (i * 1000) % 199;
  131. data.push({
  132. key: '' + i,
  133. name: isSemiDesign ? `Semi Design 设计稿${i}.fig` : `Semi Pro 设计稿${i}.fig`,
  134. owner: isSemiDesign ? '姜鹏志' : '郝宣',
  135. size: randomNumber,
  136. updateTime: new Date().valueOf() + randomNumber * DAY,
  137. avatarBg: isSemiDesign ? 'grey' : 'red'
  138. });
  139. }
  140. return data;
  141. };
  142. useEffect(() => {
  143. const data = getData(5);
  144. setData(data);
  145. }, []);
  146. return <Table columns={columns} dataSource={dataSource} pagination={false} />;
  147. }
  148. render(App);