-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelatable.tsx
More file actions
87 lines (69 loc) · 2.55 KB
/
relatable.tsx
File metadata and controls
87 lines (69 loc) · 2.55 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
80
81
82
83
84
85
86
87
import React, { PropsWithChildren } from 'react';
import { Column } from 'react-table';
import {
IWithPaginationOptions,
IWithSortingOptions,
IWithFiltersOptions,
IWithGroupingOptions,
IWithExpandedOptions,
IWithSelectionOptions,
} from '../../add-ons';
import { CellCollSpanGetter, StateChangeHandler } from '../../relatable.types';
import { RelatableActionContext, RelatableStateContext } from '../../states';
import { useRelatableActions, useRelatableState } from './relatable.hooks';
import Table, { ITableProps } from '../table';
import Pagination from '../pagination';
import { StyleWrapper } from './relatable.styled';
export interface IRelatableProps<Data extends object = any> {
// see https://react-table.js.org/api/usetable
columns: Column<Data>[];
data: Data[];
defaultColumn?: Partial<Column<Data>>;
// Relatable state change handler
onStateChange?: StateChangeHandler;
// cell col span getter
getCellColSpan?: CellCollSpanGetter;
// add on options
filterable?: boolean | IWithFiltersOptions;
groupable?: boolean | IWithGroupingOptions;
sortable?: boolean | IWithSortingOptions;
expandable?: boolean | IWithExpandedOptions;
paginated?: boolean | IWithPaginationOptions;
selectable?: boolean | IWithSelectionOptions;
}
// when used without children, Table props are passed along as well
export interface IRelatableBasicProps extends IRelatableProps, ITableProps {
}
export interface IRelatableChildrenProps extends PropsWithChildren<IRelatableProps> {
}
export default function Relatable(props: IRelatableChildrenProps | IRelatableBasicProps): JSX.Element {
// @ts-ignore
const { children } = props;
if (children) {
return <RelatableState {...props}/>;
}
return <RelatableBasic {...props}/>;
}
function RelatableBasic(props: IRelatableBasicProps): JSX.Element {
const { columns, data, defaultColumn, paginated, ...rest } = props;
return <RelatableState {...props}>
<Table {...rest} />
{paginated && <Pagination/>}
</RelatableState>;
}
function RelatableState({ children, ...rest }: IRelatableChildrenProps): JSX.Element {
const tableProps = useRelatableState(rest);
return <RelatableStateContext.Provider value={tableProps}>
<RelatableActions>
<StyleWrapper className="relatable">
{children}
</StyleWrapper>
</RelatableActions>
</RelatableStateContext.Provider>;
}
function RelatableActions({ children }: any) {
const actionsState = useRelatableActions();
return <RelatableActionContext.Provider value={actionsState}>
{children}
</RelatableActionContext.Provider>;
}