Skip to content

Commit 8914020

Browse files
authored
feat/full-type-safe (#107)
* wip * wip * wip * feat(table): fully typesafe
1 parent ea35e8f commit 8914020

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+551
-423
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
with:
1616
node-version: '16.x'
1717
- run: yarn install
18-
- run: yarn run check
18+
- run: yarn run valid
1919
- run: yarn run library:build
2020
- run: yarn run semantic-release
2121
env:

.storybook/stories/Types/compact-table.story.mdx

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,52 @@ import { Meta } from '@storybook/addon-docs';
55
# Compact Table
66

77
```javascript
8+
import * as React from 'react';
9+
810
import { TableNode, TableProps, RowProps } from '@table-library/react-table-library/types/table';
911
import { RowHeight } from '@table-library/react-table-library/types/virtualized';
1012
import { ColumnSortProps } from '@table-library/react-table-library/types/sort';
1113
import { ColumnSelectProps } from '@table-library/react-table-library/types/select';
1214
import { ColumnTreeProps } from '@table-library/react-table-library/types/tree';
1315
import { ColumnResizeProps } from '@table-library/react-table-library/types/resize';
1416

15-
export type Column = {
16-
label: string,
17-
renderCell: (node: TableNode) => React.ReactNode,
18-
resize?: ColumnResizeProps,
19-
sort?: ColumnSortProps,
20-
select?: ColumnSelectProps,
21-
tree?: ColumnTreeProps,
22-
pinLeft?: boolean,
23-
pinRight?: boolean,
24-
hide?: boolean,
25-
cellProps?: Record<string, any>,
17+
export type Column<T extends TableNode> = {
18+
label: string;
19+
renderCell: (node: T) => React.ReactNode;
20+
footer?: string;
21+
resize?: ColumnResizeProps;
22+
sort?: ColumnSortProps;
23+
select?: ColumnSelectProps;
24+
tree?: ColumnTreeProps<T>;
25+
pinLeft?: boolean;
26+
pinRight?: boolean;
27+
hide?: boolean;
28+
cellProps?: Record<string, any>;
2629
};
2730

2831
export type VirtualizedOptions = {
29-
rowHeight: RowHeight,
30-
itemCount?: number,
32+
rowHeight: RowHeight;
33+
itemCount?: number;
3134
};
3235

3336
export type TableOptions = {
34-
renderBeforeTable?: () => React.ReactNode,
35-
renderAfterTable?: () => React.ReactNode,
37+
renderBeforeTable?: () => React.ReactNode;
38+
renderAfterTable?: () => React.ReactNode;
3639
};
3740

38-
export type RowOptions = {
39-
renderBeforeRow?: (node: TableNode) => React.ReactNode,
40-
renderAfterRow?: (node: TableNode) => React.ReactNode,
41+
export type RowOptions<T extends TableNode> = {
42+
renderBeforeRow?: (node: T, index: number) => React.ReactNode;
43+
renderAfterRow?: (node: T, index: number) => React.ReactNode;
4144
};
4245

43-
export type RowPropsAsObject = Omit<RowProps, 'item' | 'children'>;
46+
export type RowPropsAsObject<T extends TableNode> = Omit<RowProps<T>, 'item' | 'children'>;
4447

45-
export type CompactTableProps = TableProps & {
46-
columns: Column[],
47-
tableOptions?: TableOptions,
48-
rowProps?: RowPropsAsObject,
49-
rowOptions?: RowOptions,
50-
virtualizedOptions?: VirtualizedOptions,
48+
export type CompactTableProps<T extends TableNode> = TableProps<T> & {
49+
columns: Column<T>[];
50+
tableOptions?: TableOptions;
51+
rowProps?: RowPropsAsObject<T>;
52+
rowOptions?: RowOptions<T>;
53+
virtualizedOptions?: VirtualizedOptions;
5154
};
5255
```
5356

.storybook/stories/Types/composed-table.story.mdx

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,68 @@ import { Meta } from '@storybook/addon-docs';
55
# Composed Table
66

77
```javascript
8-
import * as React from 'react';
9-
10-
import { Nullish } from '@table-library/react-table-library/types/common';
11-
import { Theme } from '@table-library/react-table-library/types/theme';
12-
import { Layout } from '@table-library/react-table-library/types/layout';
13-
import { ColumnResizeProps } from '@table-library/react-table-library/types/resize';
14-
import { Select } from '@table-library/react-table-library/types/select';
15-
import { Tree } from '@table-library/react-table-library/types/tree';
16-
import { Sort } from '@table-library/react-table-library/types/sort';
17-
import { Pagination } from '@table-library/react-table-library/types/pagination';
18-
198
export type RestProps = Record<string, any>;
209

21-
export type OnClick = (node: TableNode, event: React.SyntheticEvent | React.KeyboardEvent) => void;
10+
export type Event = React.SyntheticEvent | React.KeyboardEvent;
11+
12+
export type OnClick<T extends TableNode> = (node: T, event: Event) => void;
2213

2314
export type CellProps = {
24-
className?: string,
25-
pinLeft?: boolean,
26-
pinRight?: boolean,
27-
stiff?: boolean,
28-
gridColumnStart?: number,
29-
gridColumnEnd?: number,
30-
onClick?: (event: React.SyntheticEvent) => void,
31-
children?: React.ReactNode,
15+
className?: string;
16+
pinLeft?: boolean;
17+
pinRight?: boolean;
18+
stiff?: boolean;
19+
gridColumnStart?: number;
20+
gridColumnEnd?: number;
21+
onClick?: (event: React.SyntheticEvent) => void;
22+
children?: React.ReactNode;
3223
} & RestProps;
3324

3425
export type HeaderCellProps = {
35-
index?: number,
36-
className?: string,
37-
hide?: boolean,
38-
pinLeft?: boolean,
39-
pinRight?: boolean,
40-
stiff?: boolean,
41-
isFooter?: boolean,
42-
gridColumnStart?: number,
43-
gridColumnEnd?: number,
44-
resize?: ColumnResizeProps,
45-
children?: React.ReactNode,
26+
index?: number;
27+
className?: string;
28+
hide?: boolean;
29+
pinLeft?: boolean;
30+
pinRight?: boolean;
31+
stiff?: boolean;
32+
isFooter?: boolean;
33+
gridColumnStart?: number;
34+
gridColumnEnd?: number;
35+
resize?: ColumnResizeProps;
36+
children?: React.ReactNode;
4637
} & RestProps;
4738

48-
export type RowProps = {
49-
item: TableNode,
50-
className?: string,
51-
disabled?: boolean,
52-
onClick?: OnClick,
53-
onDoubleClick?: OnClick,
54-
children: React.ReactNode,
39+
export type RowProps<T extends TableNode> = {
40+
item: T;
41+
className?: string;
42+
disabled?: boolean;
43+
onClick?: OnClick<T>;
44+
onDoubleClick?: OnClick<T>;
45+
children: React.ReactNode;
5546
} & RestProps;
5647

5748
export type BodyProps = {
58-
children: React.ReactNode,
49+
children: React.ReactNode;
5950
} & RestProps;
6051

6152
export type HeaderRowProps = {
62-
className?: string,
63-
children: React.ReactNode,
53+
isFooter?: boolean;
54+
className?: string;
55+
children: React.ReactNode;
6456
} & RestProps;
6557

6658
export type HeaderProps = {
67-
children: React.ReactNode,
59+
children: React.ReactNode;
6860
} & RestProps;
6961

70-
export type TableProps = {
71-
data: Data;
62+
export type TableProps<T extends TableNode> = {
63+
data: Data<T>;
7264
theme?: Theme;
7365
layout?: Layout;
74-
sort?: Sort;
75-
pagination?: Pagination;
76-
select?: Select;
77-
tree?: Tree;
66+
sort?: Sort<T>;
67+
pagination?: Pagination<T>;
68+
select?: Select<T>;
69+
tree?: Tree<T>;
7870
onInit?: OnInitFunction;
7971
children?: (nodes: ExtendedNode<TableNode>[]) => React.ReactNode;
8072
} & RestProps;

.storybook/stories/Types/data.story.mdx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import { Meta } from '@storybook/addon-docs';
77
Either table component needs to receive the following `Data` type as `data` prop:
88

99
```javascript
10+
export type Identifier = string | number;
11+
1012
export type TableNode = {
11-
id: string,
12-
nodes?: TableNode[] | Nullish,
13-
[prop: string]: any,
13+
id: Identifier;
14+
nodes?: TableNode[] | Nullish;
15+
[prop: string]: any;
1416
};
1517

1618
export type ExtendedNode<T extends TableNode> = T & {
@@ -20,9 +22,9 @@ export type ExtendedNode<T extends TableNode> = T & {
2022
ancestors?: ExtendedNode<T>[];
2123
};
2224

23-
export type Data = {
24-
pageInfo?: any,
25-
nodes: TableNode[],
25+
export type Data<T extends TableNode> = {
26+
pageInfo?: any;
27+
nodes: T[];
2628
};
2729
```
2830

.storybook/stories/Types/layout.story.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import { Meta } from '@storybook/addon-docs';
66

77
```javascript
88
export type Layout = {
9-
custom?: boolean,
10-
horizontalScroll?: boolean,
11-
fixedHeader?: boolean,
12-
isDiv?: boolean,
13-
resizedLayout?: string,
14-
onLayoutChange?: (grid: string) => void,
9+
custom?: boolean;
10+
horizontalScroll?: boolean;
11+
fixedHeader?: boolean;
12+
isDiv?: boolean;
13+
resizedLayout?: string;
14+
onLayoutChange?: (grid: string) => void;
1515
};
1616
```
1717

.storybook/stories/Types/pagination.story.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ import { State, Modifier } from '@table-library/react-table-library/types/common
99
import { TableNode } from '@table-library/react-table-library/types/table';
1010

1111
export type PaginationOptions = {
12-
isServer?: boolean,
12+
isServer?: boolean;
1313
};
1414

1515
export type PaginationOptionsSound = {
16-
isServer: boolean,
16+
isServer: boolean;
1717
};
1818

1919
export type PaginationFunctions = {
20-
onSetPage: (page: number) => void,
21-
onSetSize: (size: number) => void,
20+
onSetPage: (page: number) => void;
21+
onSetSize: (size: number) => void;
2222
};
2323

24-
export type Pagination = {
25-
state: State,
26-
fns: PaginationFunctions,
27-
options: PaginationOptionsSound,
28-
modifier: Modifier,
24+
export type Pagination<T extends TableNode> = {
25+
state: State;
26+
fns: PaginationFunctions;
27+
options: PaginationOptionsSound;
28+
modifier: Modifier<T>;
2929
};
3030

3131
export type Pages = Record<string, TableNode[]>;

.storybook/stories/Types/resize.story.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { Meta } from '@storybook/addon-docs';
66

77
```javascript
88
type ColumnResizePropsObject = {
9-
minWidth?: number,
10-
resizerWidth?: number,
11-
resizerHighlight?: string,
9+
minWidth?: number;
10+
resizerWidth?: number;
11+
resizerHighlight?: string;
1212
};
1313

1414
export type ColumnResizeProps = ColumnResizePropsObject | boolean;

.storybook/stories/Types/select.story.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export type SelectOptionsSound = {
3636
isPartialToAll: boolean;
3737
};
3838

39-
export type CellSelectProps = {
40-
item: TableNode;
39+
export type CellSelectProps<T extends TableNode> = {
40+
item: T;
4141
};
4242

4343
type ColumnSelectPropsObject = {
@@ -47,14 +47,14 @@ type ColumnSelectPropsObject = {
4747

4848
export type ColumnSelectProps = ColumnSelectPropsObject | boolean;
4949

50-
export type Select = {
50+
export type Select<T extends TableNode> = {
5151
state: State;
52-
fns: IdReducerFunctions;
52+
fns: IdReducerFunctions<T>;
5353
options: SelectOptionsSound;
54-
_getRowProps: GetRowProps;
54+
_getRowProps: GetRowProps<T>;
5555
components: {
5656
HeaderCellSelect: React.FunctionComponent<Record<string, any>>;
57-
CellSelect: React.FunctionComponent<CellSelectProps>;
57+
CellSelect: React.FunctionComponent<CellSelectProps<T>>;
5858
};
5959
};
6060
```

.storybook/stories/Types/sort.story.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ export type HeaderCellSortProps = {
7676
children?: React.ReactNode;
7777
} & Record<string, any>;
7878

79-
export type Sort = {
79+
export type Sort<T extends TableNode> = {
8080
state: State;
8181
fns: SortFunctions;
8282
options: SortOptionsSound;
83-
modifier: Modifier;
83+
modifier: Modifier<T>;
8484
components: {
8585
HeaderCellSort: React.FunctionComponent<HeaderCellSortProps>;
8686
};

.storybook/stories/Types/theme.story.mdx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import { Meta } from '@storybook/addon-docs';
66

77
```javascript
88
export type Theme = {
9-
Table?: string,
10-
Header?: string,
11-
Footer?: string,
12-
Body?: string,
13-
BaseRow?: string,
14-
HeaderRow?: string,
15-
FooterRow?: string,
16-
Row?: string,
17-
BaseCell?: string,
18-
HeaderCell?: string,
19-
Cell?: string,
9+
Table?: string;
10+
Header?: string;
11+
Footer?: string;
12+
Body?: string;
13+
BaseRow?: string;
14+
HeaderRow?: string;
15+
FooterRow?: string;
16+
Row?: string;
17+
BaseCell?: string;
18+
HeaderCell?: string;
19+
FooterCell?: string;
20+
Cell?: string;
2021
};
2122
```
2223

0 commit comments

Comments
 (0)