index.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import React, { useEffect, useState } from 'react';
  2. import { Table, Typography, Tag, Popover, Button, Switch } from '@douyinfe/semi-ui';
  3. import { cloneDeep } from 'lodash';
  4. import { ColumnProps } from 'table/interface';
  5. export default function App() {
  6. const [count, setCount] = useState(100);
  7. const [data, setData] = useState([]);
  8. const handleSwitchChange = (options: { checked; record; index }) => {
  9. const { checked, index } = options;
  10. const newData = cloneDeep(data);
  11. newData[index].completeStatus = checked;
  12. setData(newData);
  13. };
  14. const src = 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bag.jpeg';
  15. const shouldCellUpdate: ColumnProps['shouldCellUpdate'] = (props, prevProps) => {
  16. return props.record !== prevProps.record;
  17. };
  18. const columns: ColumnProps[] = [
  19. {
  20. title: '需求标题',
  21. dataIndex: 'featureTitle',
  22. render: (text, record, index) => (
  23. <a rel="noreferrer" href="https://semi.design/zh-CN/show/table" target="_blank">
  24. {text}
  25. </a>
  26. ),
  27. filterIcon: <div>test</div>,
  28. shouldCellUpdate
  29. },
  30. {
  31. title: '文档',
  32. dataIndex: 'doc',
  33. width: 150,
  34. render: (text, record, index) => (
  35. <Typography.Text link ellipsis={{ showTooltip: true }} style={{ width: 150, breakWord: 'break-all' }}>
  36. {text}
  37. </Typography.Text>
  38. ),
  39. shouldCellUpdate
  40. },
  41. {
  42. title: '需求状态',
  43. dataIndex: 'featureStatus',
  44. width: 100,
  45. render: (text, record, index) => (
  46. <Tag style={{ width: 50 }}>
  47. <Typography.Text ellipsis={{ showTooltip: true }} style={{ width: 50 }}>
  48. {text}
  49. </Typography.Text>
  50. </Tag>
  51. ),
  52. shouldCellUpdate
  53. },
  54. {
  55. title: '优先级',
  56. dataIndex: 'priority',
  57. render: (text, record, index) => <Tag>{text}</Tag>,
  58. shouldCellUpdate
  59. },
  60. {
  61. title: 'PM',
  62. dataIndex: 'pm',
  63. render: (text, record, index) => (
  64. <Popover
  65. showArrow
  66. content={
  67. <article>
  68. Hi ByteDancer, this is a popover.
  69. <br /> We have 2 lines.
  70. </article>
  71. }
  72. key={index}
  73. >
  74. <Tag avatarSrc={src} avatarShape="circle">
  75. {text}
  76. </Tag>
  77. </Popover>
  78. ),
  79. shouldCellUpdate
  80. },
  81. {
  82. title: '产品线',
  83. dataIndex: 'productLine',
  84. render: (text, record, index) => (
  85. <Tag>
  86. <Typography.Text ellipsis={{ showTooltip: true }} style={{ width: 50 }}>
  87. {text}
  88. </Typography.Text>
  89. </Tag>
  90. ),
  91. shouldCellUpdate
  92. },
  93. {
  94. title: '前端',
  95. dataIndex: 'fe',
  96. render: (text, record, index) => (
  97. <Popover
  98. showArrow
  99. content={
  100. <article>
  101. Hi ByteDancer, this is a popover.
  102. <br /> We have 2 lines.
  103. </article>
  104. }
  105. key={index}
  106. >
  107. <Tag color="blue">{text}</Tag>
  108. </Popover>
  109. ),
  110. shouldCellUpdate
  111. },
  112. {
  113. title: '服务端',
  114. dataIndex: 'server',
  115. render: (text, record, index) => (
  116. <Popover
  117. showArrow
  118. content={
  119. <article>
  120. Hi ByteDancer, this is a popover.
  121. <br /> We have 2 lines.
  122. </article>
  123. }
  124. key={index}
  125. >
  126. <Tag avatarSrc={src}>{text}</Tag>
  127. </Popover>
  128. ),
  129. shouldCellUpdate
  130. },
  131. {
  132. title: '创建时间',
  133. dataIndex: 'createTime',
  134. render: (text, record, index) => (
  135. <Typography.Text
  136. ellipsis={{ showTooltip: true }}
  137. style={{ width: 50 }}
  138. onClick={() => {
  139. console.log('click createTime', record);
  140. }}
  141. >
  142. {text}
  143. </Typography.Text>
  144. ),
  145. shouldCellUpdate
  146. },
  147. {
  148. title: '完成时间',
  149. dataIndex: 'completeTime',
  150. render: (text, record, index) => (
  151. <Typography.Text
  152. ellipsis={{ showTooltip: true }}
  153. style={{ width: 50 }}
  154. onClick={() => {
  155. console.log('click completeTime', record);
  156. }}
  157. >
  158. {text}
  159. </Typography.Text>
  160. ),
  161. shouldCellUpdate
  162. },
  163. {
  164. title: '完成状态',
  165. dataIndex: 'completeStatus',
  166. render: (text, record, index) => (
  167. <Switch
  168. checked={record.completeStatus}
  169. onChange={checked => handleSwitchChange({ checked, record, index })}
  170. ></Switch>
  171. ),
  172. shouldCellUpdate
  173. },
  174. ];
  175. useEffect(() => {
  176. const getData = () => {
  177. const data = Array.from(
  178. {
  179. length: count,
  180. },
  181. (_, key) => {
  182. const rowRandom = Math.round(Math.random() * 1000);
  183. const prioritySet = ['P0', 'P1', 'P2'];
  184. const priority = prioritySet[Math.round(Math.random() * 2)];
  185. const featureStatusSet = ['待埋点', '开始', '待需详评', '测试', '已完成'];
  186. const featureStatus = featureStatusSet[Math.round(Math.random() * 4)];
  187. const doc = 'https://semi.design';
  188. const createTime = new Date().valueOf();
  189. return {
  190. key,
  191. featureTitle: `需求-${rowRandom}`,
  192. doc,
  193. featureStatus,
  194. priority,
  195. pm: 'Li',
  196. productLine: 'Hotsoon',
  197. fe: '姜鹏志',
  198. server: 'ZhuYi',
  199. createTime,
  200. completeTime: createTime + rowRandom,
  201. completeStatus: false,
  202. };
  203. }
  204. );
  205. return data;
  206. };
  207. const newData = getData();
  208. setData(newData);
  209. }, [count]);
  210. const scroll = { y: 500 };
  211. return (
  212. <>
  213. <div>
  214. <Button onClick={() => setCount(count * 2)}>count * 2</Button>
  215. </div>
  216. <Table
  217. title={`数据条数:${data.length}`}
  218. rowSelection
  219. columns={columns}
  220. dataSource={data}
  221. pagination={false}
  222. scroll={scroll}
  223. />
  224. </>
  225. );
  226. }