index.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import React, { useState, useMemo } from 'react';
  2. import { Table, Avatar } from '@douyinfe/semi-ui';
  3. import * as dateFns from 'date-fns';
  4. const DAY = 24 * 60 * 60 * 1000;
  5. const figmaIconUrl = 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/figma-icon.png';
  6. const getData = () => {
  7. const data = [];
  8. for (let i = 0; i < 46; i++) {
  9. const isSemiDesign = i % 2 === 0;
  10. const randomNumber = (i * 1000) % 199;
  11. data.push({
  12. key: '' + i,
  13. name: isSemiDesign ? `Semi Design 设计稿${i}.fig` : `Semi Pro 设计稿${i}.fig`,
  14. owner: isSemiDesign ? '姜鹏志' : '郝宣',
  15. size: randomNumber,
  16. updateTime: new Date().valueOf() + randomNumber * DAY,
  17. avatarBg: isSemiDesign ? 'grey' : 'red'
  18. });
  19. }
  20. return data;
  21. };
  22. const data = getData();
  23. /**
  24. * fix https://github.com/DouyinFE/semi-design/issues/381
  25. */
  26. App.storyName = 'fixed columns change';
  27. function App() {
  28. const [dataSource, setData] = useState(data);
  29. const [rowKeys, setRowKeys] = useState([]);
  30. const columns = [
  31. {
  32. title: '标题',
  33. dataIndex: 'name',
  34. width: 400,
  35. render: (text, record, index) => {
  36. return (
  37. <div>
  38. <Avatar size="small" shape="square" src={figmaIconUrl} style={{ marginRight: 12 }}></Avatar>
  39. {text}
  40. </div>
  41. );
  42. },
  43. filters: [
  44. {
  45. text: 'Semi Design 设计稿',
  46. value: 'Semi Design 设计稿',
  47. },
  48. {
  49. text: 'Semi Pro 设计稿',
  50. value: 'Semi Pro 设计稿',
  51. },
  52. ],
  53. onFilter: (value, record) => record.name.includes(value),
  54. },
  55. {
  56. title: '大小',
  57. dataIndex: 'size',
  58. sorter: (a, b) => a.size - b.size > 0 ? 1 : -1,
  59. render: (text) => `${text} KB`
  60. },
  61. {
  62. title: '所有者',
  63. dataIndex: 'owner',
  64. render: (text, record, index) => {
  65. return (
  66. <div>
  67. <Avatar size="small" color={record.avatarBg} style={{ marginRight: 4 }}>{typeof text === 'string' && text.slice(0, 1)}</Avatar>
  68. {text}
  69. </div>
  70. );
  71. }
  72. },
  73. {
  74. title: '更新日期',
  75. dataIndex: 'updateTime',
  76. sorter: (a, b) => a.updateTime - b.updateTime > 0 ? 1 : -1,
  77. render: (value) => {
  78. return dateFns.format(new Date(value), 'yyyy-MM-dd');
  79. }
  80. }
  81. ];
  82. return (
  83. <Table
  84. columns={columns}
  85. dataSource={dataSource}
  86. pagination={{
  87. pageSize: 5,
  88. }}
  89. rowSelection={{
  90. onChange: rowKeys => setRowKeys(rowKeys),
  91. selectedRowKeys: rowKeys,
  92. }}
  93. scroll={{ y: 300 }}
  94. />
  95. );
  96. }
  97. export default App;