1 | import React, { useEffect, useState, useRef } from "react";
|
2 | /**
|
3 | * ----------------------------------------
|
4 | * 滚动表格容器
|
5 | * 等自身有了宽度以后, 再渲染子组件
|
6 | * ----------------------------------------
|
7 | */
|
8 | export default function ScrolledTableBox({ children }) {
|
9 | const [width, setWidth] = useState("100%");
|
10 | const ref = useRef(null);
|
11 |
|
12 | useEffect(() => {
|
13 | const rect = ref.current.getBoundingClientRect();
|
14 | setWidth(rect.width + "px");
|
15 | }, []);
|
16 |
|
17 | return (
|
18 | <div
|
19 | className="ScrolledTableBox"
|
20 | ref={ref}
|
21 | style={{ width: width, minHeight: "10px" }}
|
22 | >
|
23 | {width === "100%" ? null : children}
|
24 | </div>
|
25 | );
|
26 | }
|