infiniteScroll.jsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import React, { useRef } from 'react';
  2. import { Table, Avatar, Button } from '@douyinfe/semi-ui';
  3. const DAY = 24 * 60 * 60 * 1000;
  4. const columns = [
  5. {
  6. title: '标题',
  7. dataIndex: 'name',
  8. width: 200,
  9. fixed: true,
  10. render: (text, record, index) => {
  11. return (
  12. <div>
  13. {text}
  14. </div>
  15. );
  16. },
  17. filters: [
  18. {
  19. text: 'Semi Design 设计稿',
  20. value: 'Semi Design 设计稿',
  21. },
  22. {
  23. text: 'Semi Pro 设计稿',
  24. value: 'Semi Pro 设计稿',
  25. },
  26. ],
  27. onFilter: (value, record) => record.name.includes(value),
  28. },
  29. {
  30. title: '大小',
  31. dataIndex: 'size',
  32. width: 150,
  33. sorter: (a, b) => a.size - b.size > 0 ? 1 : -1,
  34. render: (text) => `${text} KB`
  35. },
  36. {
  37. title: '所有者',
  38. dataIndex: 'owner',
  39. render: (text, record, index) => {
  40. return (
  41. <div>
  42. <Avatar size="small" color={record.avatarBg} style={{ marginRight: 4 }}>{typeof text === 'string' && text.slice(0, 1)}</Avatar>
  43. {text}
  44. </div>
  45. );
  46. }
  47. },
  48. {
  49. title: '更新日期',
  50. dataIndex: 'updateTime',
  51. fixed: 'right',
  52. width: 150,
  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. function InfiniteScrollDemo() {
  60. const [data, setData] = useState([]);
  61. const scroll = { y: 600, x: 1000 };
  62. const style = { width: 750, margin: '0 auto' };
  63. const loadMore = () => {
  64. const pageSize = 20; // load 20 records every time
  65. const newData = [...data];
  66. const currentLength = data.length;
  67. for (let i = currentLength; i < currentLength + pageSize; i++) {
  68. const isSemiDesign = i % 2 === 0;
  69. const randomNumber = (i * 1000) % 199;
  70. newData.push({
  71. key: '' + i,
  72. name: isSemiDesign ? `Semi Design 设计稿${i}.fig` : `Semi Pro 设计稿${i}.fig`,
  73. owner: isSemiDesign ? '姜鹏志' : '郝宣',
  74. size: randomNumber,
  75. updateTime: new Date().valueOf() + randomNumber * DAY,
  76. avatarBg: isSemiDesign ? 'grey' : 'red'
  77. });
  78. }
  79. setData(newData);
  80. };
  81. const itemSize = 56;
  82. const virtualized = {
  83. itemSize,
  84. onScroll: ({ scrollDirection, scrollOffset, scrollUpdateWasRequested }) => {
  85. if (
  86. scrollDirection === 'forward' &&
  87. scrollOffset >= (data.length - Math.ceil(scroll.y / itemSize) * 1.5) * itemSize &&
  88. !scrollUpdateWasRequested
  89. ) {
  90. loadMore();
  91. }
  92. },
  93. };
  94. useEffect(() => {
  95. loadMore();
  96. }, []);
  97. return (
  98. <Table
  99. pagination={false}
  100. columns={columns}
  101. dataSource={data}
  102. scroll={scroll}
  103. style={style}
  104. virtualized={virtualized}
  105. />
  106. );
  107. }
  108. render(InfiniteScrollDemo);