-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.js
69 lines (62 loc) · 1.5 KB
/
Table.js
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
import React, { useEffect } from 'react';
import Head from './Head';
import Body from './Body';
import TitleBar from './TitleBar';
import Pagination from './Pagination';
import { renderSubComponent } from './utils/render';
import { useTable } from './hooks/useTable';
import './table.css';
const Table = ({
dataSource,
className,
style,
onChangeOrder,
onFilterChange,
onDelete,
enableCheckbox = true,
children,
}) => {
const {
setData,
setCallbacks,
setEnableCheckbox,
state: { data, callbacks, selectedRows },
} = useTable();
useEffect(() => {
setEnableCheckbox(enableCheckbox);
}, [enableCheckbox]);
useEffect(() => {
setCallbacks({
onChangeOrder: onChangeOrder ?? callbacks?.onChangeOrder,
});
}, [onChangeOrder]);
useEffect(() => {
if (!dataSource) return;
setData(
dataSource.map((row, index) => {
row.uuid = Date.now();
return row;
})
);
}, [dataSource]);
/**
* To do:
*
* Find way to automatically set header sizes
*/
return (
<div>
<div className={`table ${className}`} style={{ ...style }}>
{/* TitleBar */}
{renderSubComponent(TitleBar, children, { onFilterChange, onDelete })}
{/* Pagination */}
{renderSubComponent(Pagination, children)}
{/* Head */}
{renderSubComponent(Head, children)}
{/* Body/Rows */}
{renderSubComponent(Body, children, { data, selectedRows })}
</div>
</div>
);
};
export default Table;