-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathPaginatedTableWithLayout.tsx
More file actions
79 lines (67 loc) · 2.34 KB
/
PaginatedTableWithLayout.tsx
File metadata and controls
79 lines (67 loc) · 2.34 KB
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
import React from 'react';
import {TableWithControlsLayout} from '../TableWithControlsLayout/TableWithControlsLayout';
import type {TableWrapperProps} from '../TableWithControlsLayout/TableWithControlsLayout';
import {PaginatedTableProvider, usePaginatedTableState} from './PaginatedTableContext';
import type {PaginatedTableState} from './types';
export interface PaginatedTableWithLayoutProps {
controls?: React.ReactNode;
table: React.ReactNode;
tableWrapperProps?: Omit<TableWrapperProps, 'children'>;
error?: React.ReactNode;
initialState?: Partial<PaginatedTableState>;
fullHeight?: boolean;
noBatching?: boolean;
}
const TableWrapper = ({
table,
tableWrapperProps,
}: {
table: React.ReactNode;
tableWrapperProps?: Omit<TableWrapperProps, 'children'>;
}) => {
const {tableState} = usePaginatedTableState();
const {sortParams} = tableState;
const enhancedTableWrapperProps = React.useMemo(() => {
const existingScrollDependencies = tableWrapperProps?.scrollDependencies || [];
return {
...tableWrapperProps,
scrollDependencies: [...existingScrollDependencies, sortParams],
};
}, [tableWrapperProps, sortParams]);
return (
<TableWithControlsLayout.Table {...enhancedTableWrapperProps}>
{table}
</TableWithControlsLayout.Table>
);
};
const ControlsSection = ({controls}: {controls?: React.ReactNode}) => {
if (!controls) {
return null;
}
return <TableWithControlsLayout.Controls>{controls}</TableWithControlsLayout.Controls>;
};
const ErrorSection = ({error}: {error?: React.ReactNode}) => {
if (!error) {
return null;
}
return <React.Fragment>{error}</React.Fragment>;
};
export const PaginatedTableWithLayout = ({
controls,
table,
tableWrapperProps,
error,
initialState,
noBatching,
fullHeight = true,
}: PaginatedTableWithLayoutProps) => {
return (
<PaginatedTableProvider initialState={initialState} noBatching={noBatching}>
<TableWithControlsLayout fullHeight={fullHeight}>
<ControlsSection controls={controls} />
<ErrorSection error={error} />
<TableWrapper table={table} tableWrapperProps={tableWrapperProps} />
</TableWithControlsLayout>
</PaginatedTableProvider>
);
};