fullRender.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React, { useState, useEffect, useMemo } 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: ({ sorter, filter, selection }) => (
  8. <span style={{ display: 'inline-flex', alignItems: 'center', paddingLeft: 20 }}>
  9. {selection}
  10. <span style={{ marginLeft: 8 }}>Name</span>
  11. {sorter}
  12. {filter}
  13. </span>
  14. ),
  15. dataIndex: 'name',
  16. width: 400,
  17. render: (text, record, index) => {
  18. return (
  19. <div>
  20. <Avatar size="small" shape="square" src={figmaIconUrl} style={{ marginRight: 12 }}></Avatar>
  21. {text}
  22. </div>
  23. );
  24. },
  25. filters: [
  26. {
  27. text: 'Semi Design 设计稿',
  28. value: 'Semi Design 设计稿',
  29. },
  30. {
  31. text: 'Semi Pro 设计稿',
  32. value: 'Semi Pro 设计稿',
  33. },
  34. ],
  35. onFilter: (value, record) => record.name.includes(value),
  36. useFullRender: true,
  37. // 此处从render的第四个形参中解构出 展开按钮、选择按钮、文本等内容
  38. render: (text, record, index, { expandIcon, selection, indentText }) => {
  39. return (
  40. <span style={{ display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}>
  41. {indentText}
  42. {expandIcon}
  43. {selection}
  44. <span style={{ marginLeft: 8 }}>
  45. <Avatar size="small" shape="square" src={figmaIconUrl} style={{ marginRight: 12 }}></Avatar>
  46. {text}
  47. </span>
  48. </span>
  49. );
  50. },
  51. },
  52. {
  53. title: '大小',
  54. dataIndex: 'size',
  55. sorter: (a, b) => a.size - b.size > 0 ? 1 : -1,
  56. render: (text) => `${text} KB`
  57. },
  58. {
  59. title: '所有者',
  60. dataIndex: 'owner',
  61. render: (text, record, index) => {
  62. return (
  63. <div>
  64. <Avatar size="small" color={record.avatarBg} style={{ marginRight: 4 }}>{typeof text === 'string' && text.slice(0, 1)}</Avatar>
  65. {text}
  66. </div>
  67. );
  68. }
  69. },
  70. {
  71. title: '更新日期',
  72. dataIndex: 'updateTime',
  73. sorter: (a, b) => a.updateTime - b.updateTime > 0 ? 1 : -1,
  74. render: (value) => {
  75. return dateFns.format(new Date(value), 'yyyy-MM-dd');
  76. }
  77. }
  78. ];
  79. const getData = (total) => {
  80. const data = [];
  81. for (let i = 0; i < total; i++) {
  82. const isSemiDesign = i % 2 === 0;
  83. const randomNumber = (i * 1000) % 199;
  84. data.push({
  85. key: '' + i,
  86. name: isSemiDesign ? `Semi Design 设计稿${i}.fig` : `Semi Pro 设计稿${i}.fig`,
  87. owner: isSemiDesign ? '姜鹏志' : '郝宣',
  88. size: randomNumber,
  89. updateTime: new Date().valueOf() + randomNumber * DAY,
  90. avatarBg: isSemiDesign ? 'grey' : 'red'
  91. });
  92. }
  93. return data;
  94. };
  95. function Demo() {
  96. const [dataSource, setDataSource] = useState([]);
  97. const total = 46;
  98. const pagination = useMemo(() => ({
  99. pageSize: 12,
  100. }), []);
  101. const rowSelection = useMemo(() => {
  102. return {
  103. hidden: true,
  104. fixed: 'left',
  105. };
  106. }, []);
  107. useEffect(() => {
  108. const data = getData(total);
  109. setDataSource(data);
  110. }, [total]);
  111. return (
  112. <Table
  113. pagination={pagination}
  114. rowSelection={rowSelection}
  115. columns={columns}
  116. dataSource={dataSource}
  117. onChange={(...args) => console.log(...args)}
  118. expandedRowRender={record => <article>{record.name}</article>}
  119. />
  120. );
  121. }
  122. render(Demo);