广州市综治平台前端
xusd
3 days ago b4725b231cfe2a710288e8bd0b1b9d990989f90c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * @Company: hugeInfo
 * @Author: lwh
 * @Date: 2024-10-17 09:10:18
 * @LastEditTime: 2024-10-18 15:50:30
 * @LastEditors: lwh
 * @Version: 1.0.0
 * @Description: 公共组件:表格滚动
 */
 
import React, { useEffect, useRef } from 'react';
import { Table } from 'antd';
 
/**
 * 自动滚动表格组件
 * @param {Object} props 
 * @param {Array} props.dataSource 表格数据源
 * @param {Number} props.rollTime 表格每次滚动间隔时间 单位ms,默认100ms
 * @param {Number} props.rollNum 表格超过指定条数开始滚动,默认10条
 * @param {Number} props.rollTop 表格每次滚动的高度 单位px,默认2.5px
 * @param {Boolean} props.flag 是否开启滚动,默认true
 * @returns
 */
const ScrollTable = (props) => {
  const {
    dataSource,
    columns,
    rollTime = 100,
    rollNum = 10,
    rollTop = 2.5,
    flag = true,
    ...restProps
  } = props;
 
 
  const tableContainer = useRef(null);
  let timer = null;
 
  const startScrolling = () => {
    const container = tableContainer.current?.querySelector('.ant-table-body');
    if (container && dataSource.length > rollNum && flag) {
      timer = setInterval(() => {
        container.scrollTop += rollTop;
        if (container.scrollTop >= container.scrollHeight - container.clientHeight) {
          container.scrollTop = 0;
        }
      }, rollTime);
    }
  };
 
  useEffect(() => {
    startScrolling();
  }, [timer]);
 
  const handleMouseOver = () => {
    clearInterval(timer);
  };
 
  const handleMouseOut = () => {
    startScrolling();
  };
 
 
  return (
    <div
      onMouseOver={handleMouseOver}
      onMouseOut={handleMouseOut}
      style={{ position: 'relative', width: '100%', height: '100%' }} // 确保
    >
      <Table
        dataSource={dataSource}
        columns={columns}
        rowKey="id"
        ref={tableContainer}
        pagination={false}
        scroll={{ y: 250, x: '100%', scrollToFirstRowOnChange: true }}
        {...restProps}
      />
    </div>
  );
};
 
export default ScrollTable