controlledPagination.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 pageSize = 5;
  7. const columns = [
  8. {
  9. title: '标题',
  10. dataIndex: 'name',
  11. width: 400,
  12. render: (text, record, index) => {
  13. return (
  14. <div>
  15. <Avatar size="small" shape="square" src={figmaIconUrl} style={{ marginRight: 12 }}></Avatar>
  16. {text}
  17. </div>
  18. );
  19. },
  20. filters: [
  21. {
  22. text: 'Semi Design 设计稿',
  23. value: 'Semi Design 设计稿',
  24. },
  25. {
  26. text: 'Semi Pro 设计稿',
  27. value: 'Semi Pro 设计稿',
  28. },
  29. ],
  30. onFilter: (value, record) => record.name.includes(value),
  31. },
  32. {
  33. title: '大小',
  34. dataIndex: 'size',
  35. sorter: (a, b) => a.size - b.size > 0 ? 1 : -1,
  36. render: (text) => `${text} KB`
  37. },
  38. {
  39. title: '所有者',
  40. dataIndex: 'owner',
  41. render: (text, record, index) => {
  42. return (
  43. <div>
  44. <Avatar size="small" color={record.avatarBg} style={{ marginRight: 4 }}>{typeof text === 'string' && text.slice(0, 1)}</Avatar>
  45. {text}
  46. </div>
  47. );
  48. }
  49. },
  50. {
  51. title: '更新日期',
  52. dataIndex: 'updateTime',
  53. sorter: (a, b) => a.updateTime - b.updateTime > 0 ? 1 : -1,
  54. render: (value) => {
  55. return dateFns.format(new Date(value), 'yyyy-MM-dd');
  56. }
  57. }
  58. ];
  59. const getData = () => {
  60. const data = [];
  61. for (let i = 0; i < 46; i++) {
  62. const isSemiDesign = i % 2 === 0;
  63. const randomNumber = (i * 1000) % 199;
  64. data.push({
  65. key: '' + i,
  66. name: isSemiDesign ? `Semi Design 设计稿${i}.fig` : `Semi Pro 设计稿${i}.fig`,
  67. owner: isSemiDesign ? '姜鹏志' : '郝宣',
  68. size: randomNumber,
  69. updateTime: new Date().valueOf() + randomNumber * DAY,
  70. avatarBg: isSemiDesign ? 'grey' : 'red'
  71. });
  72. }
  73. return data;
  74. };
  75. const data = getData();
  76. function App() {
  77. const [dataSource, setData] = useState([]);
  78. const [loading, setLoading] = useState(false);
  79. const [currentPage, setPage] = useState(1);
  80. const fetchData = (currentPage = 1) => {
  81. setLoading(true);
  82. setPage(currentPage);
  83. return new Promise((res, rej) => {
  84. setTimeout(() => {
  85. const data = getData();
  86. let dataSource = data.slice((currentPage - 1) * pageSize, currentPage * pageSize);
  87. res(dataSource);
  88. }, 300);
  89. }).then(dataSource => {
  90. setLoading(false);
  91. setData(dataSource);
  92. });
  93. };
  94. const handlePageChange = page => {
  95. fetchData(page);
  96. };
  97. useEffect(() => {
  98. fetchData();
  99. }, []);
  100. return (
  101. <Table
  102. columns={columns}
  103. dataSource={dataSource}
  104. pagination={{
  105. currentPage,
  106. pageSize: 5,
  107. total: data.length,
  108. onPageChange: handlePageChange
  109. }}
  110. loading={loading}
  111. />
  112. );
  113. }
  114. render(App);