columnRender.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React from 'react';
  2. import { Table, Avatar, Button, Empty, Typography } from '@douyinfe/semi-ui';
  3. import { IconDelete } from '@douyinfe/semi-icons';
  4. import { IllustrationNoResult, IllustrationNoResultDark } from '@douyinfe/semi-illustrations';
  5. const { Text } = Typography;
  6. const raw = [
  7. {
  8. key: '1',
  9. name: 'Semi Design 设计稿标题可能有点长这时候应该显示 Tooltip.fig',
  10. nameIconSrc: 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/figma-icon.png',
  11. size: '2M',
  12. owner: '姜鹏志',
  13. updateTime: '2020-02-02 05:13',
  14. avatarBg: 'grey'
  15. },
  16. {
  17. key: '2',
  18. name: 'Semi Design 分享演示文稿',
  19. nameIconSrc: 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/docs-icon.png',
  20. size: '2M',
  21. owner: '郝宣',
  22. updateTime: '2020-01-17 05:31',
  23. avatarBg: 'red'
  24. },
  25. {
  26. key: '3',
  27. name: '设计文档',
  28. nameIconSrc: 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/docs-icon.png',
  29. size: '34KB',
  30. owner: 'Zoey Edwards',
  31. updateTime: '2020-01-26 11:01',
  32. avatarBg: 'light-blue'
  33. },
  34. {
  35. key: '4',
  36. name: 'Semi Pro 设计文档可能也有点长所以也会显示Tooltip',
  37. nameIconSrc: 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/docs-icon.png',
  38. size: '34KB',
  39. owner: '姜琪',
  40. updateTime: '2020-01-26 11:01',
  41. avatarBg: 'green'
  42. }
  43. ];
  44. function App() {
  45. const [dataSource, setData] = useState(raw);
  46. const removeRecord = (key) => {
  47. let newDataSource = [...dataSource];
  48. if (key != null) {
  49. let idx = newDataSource.findIndex(data => data.key === key);
  50. if (idx > -1) {
  51. newDataSource.splice(idx, 1);
  52. setData(newDataSource);
  53. }
  54. }
  55. };
  56. const resetData = () => {
  57. const newDataSource = [...raw];
  58. setData(newDataSource);
  59. };
  60. const columns = [
  61. {
  62. title: '标题',
  63. dataIndex: 'name',
  64. width: 400,
  65. render: (text, record, index) => {
  66. return (
  67. <span style={{ display: 'flex', alignItems: 'center' }}>
  68. <Avatar size="small" shape="square" src={record.nameIconSrc} style={{ marginRight: 12 }}></Avatar>
  69. {/* 宽度计算方式为单元格设置宽度 - 非文本内容宽度 */}
  70. <Text heading={5} ellipsis={{ showTooltip: true }} style={{ width: 'calc(400px - 76px)' }}>
  71. {text}
  72. </Text>
  73. </span>
  74. );
  75. }
  76. },
  77. {
  78. title: '大小',
  79. dataIndex: 'size',
  80. width: 150,
  81. },
  82. {
  83. title: '所有者',
  84. dataIndex: 'owner',
  85. width: 300,
  86. render: (text, record, index) => {
  87. return (
  88. <div>
  89. <Avatar size="small" color={record.avatarBg} style={{ marginRight: 4 }}>{typeof text === 'string' && text.slice(0, 1)}</Avatar>
  90. {text}
  91. </div>
  92. );
  93. }
  94. },
  95. {
  96. title: '更新日期',
  97. dataIndex: 'updateTime',
  98. width: 200,
  99. },
  100. {
  101. title: '',
  102. dataIndex: 'operate',
  103. render: (text, record) => <Button icon={<IconDelete />} theme='borderless' onClick={() => removeRecord(record.key)} />
  104. },
  105. ];
  106. const empty = (
  107. <Empty
  108. image={<IllustrationNoResult />}
  109. darkModeImage={<IllustrationNoResultDark />}
  110. description={'搜索无结果'}
  111. />
  112. );
  113. return (
  114. <>
  115. <Button onClick={resetData} style={{ marginBottom: 10 }}>重置</Button>
  116. <Table style={{ minHeight: 350 }} columns={columns} dataSource={dataSource} pagination={false} empty={empty} />
  117. </>
  118. );
  119. }
  120. render(App);