@coveord/plasma-mantine 49.3.4 → 49.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +3 -3
- package/.turbo/turbo-test.log +29 -29
- package/dist/.tsbuildinfo +1 -1
- package/dist/cjs/components/collection/CollectionItem.js +6 -0
- package/dist/cjs/components/collection/CollectionItem.js.map +1 -1
- package/dist/cjs/components/table/Table.js +10 -53
- package/dist/cjs/components/table/Table.js.map +1 -1
- package/dist/cjs/components/table/Table.styles.js +56 -0
- package/dist/cjs/components/table/Table.styles.js.map +1 -0
- package/dist/cjs/components/table/Table.types.js +6 -0
- package/dist/cjs/components/table/Table.types.js.map +1 -0
- package/dist/cjs/components/table/TableActions.js +3 -3
- package/dist/cjs/components/table/TableActions.js.map +1 -1
- package/dist/cjs/components/table/TableContext.js +18 -3
- package/dist/cjs/components/table/TableContext.js.map +1 -1
- package/dist/cjs/components/table/TableDateRangePicker.js +2 -2
- package/dist/cjs/components/table/TableDateRangePicker.js.map +1 -1
- package/dist/cjs/components/table/TableFilter.js +2 -2
- package/dist/cjs/components/table/TableFilter.js.map +1 -1
- package/dist/cjs/components/table/TableHeader.js +2 -2
- package/dist/cjs/components/table/TableHeader.js.map +1 -1
- package/dist/cjs/components/table/TablePagination.js +2 -2
- package/dist/cjs/components/table/TablePagination.js.map +1 -1
- package/dist/cjs/components/table/TablePerPage.js +2 -2
- package/dist/cjs/components/table/TablePerPage.js.map +1 -1
- package/dist/cjs/components/table/TablePredicate.js +2 -2
- package/dist/cjs/components/table/TablePredicate.js.map +1 -1
- package/dist/cjs/components/table/index.js +7 -1
- package/dist/cjs/components/table/index.js.map +1 -1
- package/dist/cjs/components/table/useRowSelection.js +27 -17
- package/dist/cjs/components/table/useRowSelection.js.map +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/definitions/components/table/Table.d.ts +1 -98
- package/dist/definitions/components/table/Table.d.ts.map +1 -1
- package/dist/definitions/components/table/Table.styles.d.ts +11 -0
- package/dist/definitions/components/table/Table.styles.d.ts.map +1 -0
- package/dist/definitions/components/table/Table.types.d.ts +177 -0
- package/dist/definitions/components/table/Table.types.d.ts.map +1 -0
- package/dist/definitions/components/table/TableActions.d.ts.map +1 -1
- package/dist/definitions/components/table/TableContext.d.ts +4 -71
- package/dist/definitions/components/table/TableContext.d.ts.map +1 -1
- package/dist/definitions/components/table/TableFilter.d.ts.map +1 -1
- package/dist/definitions/components/table/TablePagination.d.ts.map +1 -1
- package/dist/definitions/components/table/TablePerPage.d.ts.map +1 -1
- package/dist/definitions/components/table/TablePredicate.d.ts.map +1 -1
- package/dist/definitions/components/table/index.d.ts +2 -2
- package/dist/definitions/components/table/index.d.ts.map +1 -1
- package/dist/definitions/components/table/useRowSelection.d.ts +4 -1
- package/dist/definitions/components/table/useRowSelection.d.ts.map +1 -1
- package/dist/definitions/index.d.ts +1 -1
- package/dist/definitions/index.d.ts.map +1 -1
- package/dist/esm/components/collection/CollectionItem.js +6 -0
- package/dist/esm/components/collection/CollectionItem.js.map +1 -1
- package/dist/esm/components/table/Table.js +11 -54
- package/dist/esm/components/table/Table.js.map +1 -1
- package/dist/esm/components/table/Table.styles.js +46 -0
- package/dist/esm/components/table/Table.styles.js.map +1 -0
- package/dist/esm/components/table/Table.types.js +3 -0
- package/dist/esm/components/table/Table.types.js.map +1 -0
- package/dist/esm/components/table/TableActions.js +2 -2
- package/dist/esm/components/table/TableActions.js.map +1 -1
- package/dist/esm/components/table/TableContext.js +8 -1
- package/dist/esm/components/table/TableContext.js.map +1 -1
- package/dist/esm/components/table/TableDateRangePicker.js +1 -1
- package/dist/esm/components/table/TableDateRangePicker.js.map +1 -1
- package/dist/esm/components/table/TableFilter.js +1 -1
- package/dist/esm/components/table/TableFilter.js.map +1 -1
- package/dist/esm/components/table/TableHeader.js +1 -1
- package/dist/esm/components/table/TableHeader.js.map +1 -1
- package/dist/esm/components/table/TablePagination.js +1 -1
- package/dist/esm/components/table/TablePagination.js.map +1 -1
- package/dist/esm/components/table/TablePerPage.js +1 -1
- package/dist/esm/components/table/TablePerPage.js.map +1 -1
- package/dist/esm/components/table/TablePredicate.js +2 -2
- package/dist/esm/components/table/TablePredicate.js.map +1 -1
- package/dist/esm/components/table/index.js +1 -1
- package/dist/esm/components/table/index.js.map +1 -1
- package/dist/esm/components/table/useRowSelection.js +26 -17
- package/dist/esm/components/table/useRowSelection.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/package.json +6 -5
- package/src/components/collection/CollectionItem.tsx +2 -2
- package/src/components/table/Table.styles.ts +58 -0
- package/src/components/table/Table.tsx +18 -173
- package/src/components/table/Table.types.ts +203 -0
- package/src/components/table/TableActions.tsx +10 -3
- package/src/components/table/TableContext.tsx +9 -70
- package/src/components/table/TableDateRangePicker.tsx +1 -1
- package/src/components/table/TableFilter.tsx +2 -3
- package/src/components/table/TableHeader.tsx +1 -1
- package/src/components/table/TablePagination.tsx +3 -3
- package/src/components/table/TablePerPage.tsx +2 -3
- package/src/components/table/TablePredicate.tsx +3 -2
- package/src/components/table/__tests__/Table.spec.tsx +59 -22
- package/src/components/table/index.ts +2 -2
- package/src/components/table/useRowSelection.ts +36 -21
- package/src/index.ts +2 -0
- package/dist/cjs/components/table/useTable.js +0 -21
- package/dist/cjs/components/table/useTable.js.map +0 -1
- package/dist/definitions/components/table/useTable.d.ts +0 -16
- package/dist/definitions/components/table/useTable.d.ts.map +0 -1
- package/dist/esm/components/table/useTable.js +0 -11
- package/dist/esm/components/table/useTable.js.map +0 -1
- package/src/components/table/useTable.tsx +0 -11
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import {DateRangePickerValue} from '@mantine/dates';
|
|
2
|
+
import {UseFormReturnType} from '@mantine/form';
|
|
3
|
+
import {
|
|
4
|
+
ColumnDef,
|
|
5
|
+
CoreOptions,
|
|
6
|
+
InitialTableState as TanstackInitialTableState,
|
|
7
|
+
TableOptions,
|
|
8
|
+
TableState as TanstackTableState,
|
|
9
|
+
} from '@tanstack/table-core';
|
|
10
|
+
import {Dispatch, ReactElement, ReactNode, RefObject} from 'react';
|
|
11
|
+
|
|
12
|
+
import {TableActions} from './TableActions';
|
|
13
|
+
import {TableAccordionColumn, TableCollapsibleColumn} from './TableCollapsibleColumn';
|
|
14
|
+
import {TableDateRangePicker} from './TableDateRangePicker';
|
|
15
|
+
import {TableFilter} from './TableFilter';
|
|
16
|
+
import {TableFooter} from './TableFooter';
|
|
17
|
+
import {TableHeader} from './TableHeader';
|
|
18
|
+
import {TablePagination} from './TablePagination';
|
|
19
|
+
import {TablePerPage} from './TablePerPage';
|
|
20
|
+
import {TablePredicate} from './TablePredicate';
|
|
21
|
+
|
|
22
|
+
export type RowSelectionWithData<TData> = Record<string, TData>;
|
|
23
|
+
export interface RowSelectionState<TData> {
|
|
24
|
+
rowSelection: RowSelectionWithData<TData>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface TableState<TData> extends Omit<TanstackTableState, 'rowSelection'>, RowSelectionState<TData> {}
|
|
28
|
+
|
|
29
|
+
export interface InitialTableState<TData>
|
|
30
|
+
extends Omit<TanstackInitialTableState, 'rowSelection'>,
|
|
31
|
+
Partial<RowSelectionState<TData>>,
|
|
32
|
+
Partial<TableFormType> {}
|
|
33
|
+
|
|
34
|
+
export type onTableChangeEvent<TData> = (params: TableState<TData> & TableFormType) => void;
|
|
35
|
+
|
|
36
|
+
export type TableFormType = {
|
|
37
|
+
/**
|
|
38
|
+
* Object containing the table predicates and their selected values
|
|
39
|
+
*
|
|
40
|
+
* @example {type: "LONG", origin: "system"}
|
|
41
|
+
*/
|
|
42
|
+
predicates: Record<string, string>;
|
|
43
|
+
/**
|
|
44
|
+
* Selected date range in the table
|
|
45
|
+
*
|
|
46
|
+
* @example [new Date(2022, 0, 1), new Date(2022, 0, 31)]
|
|
47
|
+
*/
|
|
48
|
+
dateRange: DateRangePickerValue;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type TableContextType<TData> = {
|
|
52
|
+
/**
|
|
53
|
+
* Function to call when the table needs an update
|
|
54
|
+
*/
|
|
55
|
+
onChange: () => void;
|
|
56
|
+
/**
|
|
57
|
+
* Internal state of the table
|
|
58
|
+
*
|
|
59
|
+
* @see https://tanstack.com/table/v8/docs/api/core/table#state
|
|
60
|
+
*/
|
|
61
|
+
state: TableState<TData>;
|
|
62
|
+
/**
|
|
63
|
+
* Function to update the table state
|
|
64
|
+
*/
|
|
65
|
+
setState: Dispatch<(prevState: TableState<TData>) => TableState<TData>>;
|
|
66
|
+
/**
|
|
67
|
+
* Whether the table currently as any kind of filter applied.
|
|
68
|
+
* Useful to determine if the noDataChildren is an empty state or just the result of a filter
|
|
69
|
+
*/
|
|
70
|
+
isFiltered: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Function that clears the filter and predicates
|
|
73
|
+
*/
|
|
74
|
+
clearFilters: () => void;
|
|
75
|
+
/**
|
|
76
|
+
* Function that returns the selected row if any.
|
|
77
|
+
*/
|
|
78
|
+
getSelectedRow: () => TData | null;
|
|
79
|
+
/**
|
|
80
|
+
* Function that returns an array of the selected rows. Most useful when multi row selection is enabled.
|
|
81
|
+
*/
|
|
82
|
+
getSelectedRows: () => TData[];
|
|
83
|
+
/**
|
|
84
|
+
* Function that clear the selected row
|
|
85
|
+
*/
|
|
86
|
+
clearSelection: () => void;
|
|
87
|
+
/**
|
|
88
|
+
* Form used to handle predicates and dateRange
|
|
89
|
+
*/
|
|
90
|
+
form: UseFormReturnType<TableFormType>;
|
|
91
|
+
/**
|
|
92
|
+
* Table container ref
|
|
93
|
+
*/
|
|
94
|
+
containerRef: RefObject<HTMLDivElement>;
|
|
95
|
+
/**
|
|
96
|
+
* Whether multi row selection is activated
|
|
97
|
+
*/
|
|
98
|
+
multiRowSelectionEnabled: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Function that returns the number of pages
|
|
101
|
+
*/
|
|
102
|
+
getPageCount: () => number;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export interface TableProps<T> {
|
|
106
|
+
/**
|
|
107
|
+
* Data to display in the table
|
|
108
|
+
*/
|
|
109
|
+
data: T[];
|
|
110
|
+
/**
|
|
111
|
+
* Defines how each row is uniquely identified. It is highly recommended that you specify this prop to an ID that makes sense.
|
|
112
|
+
*/
|
|
113
|
+
getRowId?: CoreOptions<T>['getRowId'];
|
|
114
|
+
/**
|
|
115
|
+
* Columns to display in the table.
|
|
116
|
+
*
|
|
117
|
+
* @see https://tanstack.com/table/v8/docs/guide/column-defs
|
|
118
|
+
*/
|
|
119
|
+
columns: Array<ColumnDef<T>>;
|
|
120
|
+
/**
|
|
121
|
+
* Function called when the table mounts
|
|
122
|
+
*
|
|
123
|
+
* @param state the state of the table
|
|
124
|
+
*/
|
|
125
|
+
onMount?: onTableChangeEvent<T>;
|
|
126
|
+
/**
|
|
127
|
+
* Function called when the table should update
|
|
128
|
+
*
|
|
129
|
+
* @param state the state of the table
|
|
130
|
+
*/
|
|
131
|
+
onChange?: onTableChangeEvent<T>;
|
|
132
|
+
/**
|
|
133
|
+
* Function that generates the expandable content of a row
|
|
134
|
+
* Return null for rows that don't need to be expandable
|
|
135
|
+
*
|
|
136
|
+
* @param datum the row for which the children should be generated.
|
|
137
|
+
*/
|
|
138
|
+
getExpandChildren?: (datum: T) => ReactNode;
|
|
139
|
+
/**
|
|
140
|
+
* React children to show when the table has no rows to show. You can leverage useTable to get the state of the table
|
|
141
|
+
*/
|
|
142
|
+
noDataChildren?: ReactNode;
|
|
143
|
+
/**
|
|
144
|
+
* Whether the table is loading or not
|
|
145
|
+
*
|
|
146
|
+
* @default false
|
|
147
|
+
*/
|
|
148
|
+
loading?: boolean;
|
|
149
|
+
/**
|
|
150
|
+
* Childrens to display in the table. They need to be wrap in either `Table.Header` or `Table.Footer`
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* <Table ...>
|
|
154
|
+
* <Table.Header>
|
|
155
|
+
* <div>Hello</div>
|
|
156
|
+
* </Table.Header>
|
|
157
|
+
* </Table>
|
|
158
|
+
*/
|
|
159
|
+
children?: ReactNode;
|
|
160
|
+
/**
|
|
161
|
+
* Initial state of the table
|
|
162
|
+
*/
|
|
163
|
+
initialState?: InitialTableState<T>;
|
|
164
|
+
/**
|
|
165
|
+
* Action passed when user double clicks on a row
|
|
166
|
+
*/
|
|
167
|
+
doubleClickAction?: (datum: T) => void;
|
|
168
|
+
/**
|
|
169
|
+
* Whether the user can select multiple rows in order to perform actions in bulk
|
|
170
|
+
*
|
|
171
|
+
* @default false
|
|
172
|
+
*/
|
|
173
|
+
multiRowSelectionEnabled?: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* Additional options that can be passed to the table
|
|
176
|
+
*/
|
|
177
|
+
options?: Omit<
|
|
178
|
+
Partial<TableOptions<T>>,
|
|
179
|
+
| 'initialState'
|
|
180
|
+
| 'data'
|
|
181
|
+
| 'columns'
|
|
182
|
+
| 'manualPagination'
|
|
183
|
+
| 'enableMultiRowSelection'
|
|
184
|
+
| 'getRowId'
|
|
185
|
+
| 'getRowCanExpand'
|
|
186
|
+
| 'enableRowSelection'
|
|
187
|
+
| 'onRowSelectionChange'
|
|
188
|
+
>;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export interface TableType {
|
|
192
|
+
<T>(props: TableProps<T>): ReactElement;
|
|
193
|
+
Actions: typeof TableActions;
|
|
194
|
+
Filter: typeof TableFilter;
|
|
195
|
+
Footer: typeof TableFooter;
|
|
196
|
+
Header: typeof TableHeader;
|
|
197
|
+
Pagination: typeof TablePagination;
|
|
198
|
+
PerPage: typeof TablePerPage;
|
|
199
|
+
Predicate: typeof TablePredicate;
|
|
200
|
+
DateRangePicker: typeof TableDateRangePicker;
|
|
201
|
+
CollapsibleColumn: typeof TableCollapsibleColumn;
|
|
202
|
+
AccordionColumn: typeof TableAccordionColumn;
|
|
203
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {Group} from '@mantine/core';
|
|
2
2
|
import {ReactElement, ReactNode} from 'react';
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
import {useTable} from './TableContext';
|
|
4
5
|
|
|
5
6
|
interface TableActionsProps<T> {
|
|
6
7
|
/**
|
|
@@ -26,12 +27,18 @@ interface TableActionsProps<T> {
|
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
export const TableActions = <T,>({children}: TableActionsProps<T>): ReactElement => {
|
|
29
|
-
const {getSelectedRows, multiRowSelectionEnabled} = useTable();
|
|
30
|
+
const {getSelectedRows, multiRowSelectionEnabled} = useTable<T>();
|
|
30
31
|
const selectedRows = getSelectedRows();
|
|
31
32
|
|
|
32
33
|
if (selectedRows.length <= 0) {
|
|
33
34
|
return null;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
return
|
|
37
|
+
return (
|
|
38
|
+
<Group spacing="xs">
|
|
39
|
+
{multiRowSelectionEnabled
|
|
40
|
+
? (children as (data: T[]) => ReactNode)(selectedRows)
|
|
41
|
+
: (children as (datum: T) => ReactNode)(selectedRows[0])}
|
|
42
|
+
</Group>
|
|
43
|
+
);
|
|
37
44
|
};
|
|
@@ -1,75 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {UseFormReturnType} from '@mantine/form';
|
|
3
|
-
import {TableState} from '@tanstack/react-table';
|
|
4
|
-
import {createContext, Dispatch, RefObject} from 'react';
|
|
1
|
+
import {createContext, useContext} from 'react';
|
|
5
2
|
|
|
6
|
-
|
|
3
|
+
import {TableContextType} from './Table.types';
|
|
7
4
|
|
|
8
|
-
export
|
|
9
|
-
/**
|
|
10
|
-
* Object containing the table predicates and their selected values
|
|
11
|
-
*
|
|
12
|
-
* @example {type: "LONG", origin: "system"}
|
|
13
|
-
*/
|
|
14
|
-
predicates: Record<string, string>;
|
|
15
|
-
/**
|
|
16
|
-
* Selected date range in the table
|
|
17
|
-
*
|
|
18
|
-
* @example [new Date(2022, 0, 1), new Date(2022, 0, 31)]
|
|
19
|
-
*/
|
|
20
|
-
dateRange: DateRangePickerValue;
|
|
21
|
-
};
|
|
5
|
+
export const TableContext = createContext<TableContextType<any> | null>(null);
|
|
22
6
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Internal state of the table
|
|
30
|
-
*
|
|
31
|
-
* @see https://tanstack.com/table/v8/docs/api/core/table#state
|
|
32
|
-
*/
|
|
33
|
-
state: TableState;
|
|
34
|
-
/**
|
|
35
|
-
* Function to update the table state
|
|
36
|
-
*/
|
|
37
|
-
setState: Dispatch<(prevState: TableState) => TableState>;
|
|
38
|
-
/**
|
|
39
|
-
* Whether the table currently as any kind of filter applied.
|
|
40
|
-
* Useful to determine if the noDataChildren is an empty state or just the result of a filter
|
|
41
|
-
*/
|
|
42
|
-
isFiltered: boolean;
|
|
43
|
-
/**
|
|
44
|
-
* Function that clears the filter and predicates
|
|
45
|
-
*/
|
|
46
|
-
clearFilters: () => void;
|
|
47
|
-
/**
|
|
48
|
-
* Function that returns the selected row if any.
|
|
49
|
-
*/
|
|
50
|
-
getSelectedRow: () => any | null;
|
|
51
|
-
/**
|
|
52
|
-
* Function that returns an array of the selected rows. Most useful when multi row selection is enabled.
|
|
53
|
-
*/
|
|
54
|
-
getSelectedRows: () => any[];
|
|
55
|
-
/**
|
|
56
|
-
* Function that clear the selected row
|
|
57
|
-
*/
|
|
58
|
-
clearSelection: () => void;
|
|
59
|
-
/**
|
|
60
|
-
* Form used to handle predicates and dateRange
|
|
61
|
-
*/
|
|
62
|
-
form: UseFormReturnType<TableFormType>;
|
|
63
|
-
/**
|
|
64
|
-
* Table container ref
|
|
65
|
-
*/
|
|
66
|
-
containerRef: RefObject<HTMLDivElement>;
|
|
67
|
-
multiRowSelectionEnabled: boolean;
|
|
7
|
+
export const useTable = <T,>(): TableContextType<T> => {
|
|
8
|
+
const ctx = useContext(TableContext);
|
|
9
|
+
if (ctx === null) {
|
|
10
|
+
throw new Error('useTable must be used inside of a TableContext.Provider');
|
|
11
|
+
}
|
|
68
12
|
|
|
69
|
-
|
|
70
|
-
* Function that returns the number of pages
|
|
71
|
-
*/
|
|
72
|
-
getPageCount: () => number;
|
|
13
|
+
return ctx;
|
|
73
14
|
};
|
|
74
|
-
|
|
75
|
-
export const TableContext = createContext<TableContextType | null>(null);
|
|
@@ -7,7 +7,7 @@ import {FunctionComponent, useState} from 'react';
|
|
|
7
7
|
import {Button} from '../button';
|
|
8
8
|
import {DateRangePickerInlineCalendar, DateRangePickerInlineCalendarProps} from '../date-range-picker';
|
|
9
9
|
import {DateRangePickerPreset} from '../date-range-picker/DateRangePickerPresetSelect';
|
|
10
|
-
import {useTable} from './
|
|
10
|
+
import {useTable} from './TableContext';
|
|
11
11
|
|
|
12
12
|
interface TableDateRangePickerProps
|
|
13
13
|
extends Pick<DateRangePickerInlineCalendarProps, 'startProps' | 'endProps' | 'rangeCalendarProps'> {
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import {CrossSize16Px, SearchSize16Px} from '@coveord/plasma-react-icons';
|
|
2
2
|
import {ActionIcon, createStyles, DefaultProps, Selectors, TextInput} from '@mantine/core';
|
|
3
|
-
import {TableState} from '@tanstack/react-table';
|
|
4
3
|
import {ChangeEventHandler, FunctionComponent, MouseEventHandler} from 'react';
|
|
5
4
|
|
|
6
|
-
import {useTable} from './
|
|
5
|
+
import {useTable} from './TableContext';
|
|
7
6
|
|
|
8
7
|
const useStyles = createStyles((theme) => ({
|
|
9
8
|
wrapper: {
|
|
@@ -35,7 +34,7 @@ export const TableFilter: FunctionComponent<TableFilterProps> = ({
|
|
|
35
34
|
const {state, setState} = useTable();
|
|
36
35
|
|
|
37
36
|
const changeFilterValue = (value: string) => {
|
|
38
|
-
setState((prevState
|
|
37
|
+
setState((prevState) => ({
|
|
39
38
|
...prevState,
|
|
40
39
|
pagination: prevState.pagination
|
|
41
40
|
? {pageIndex: 0, pageSize: prevState.pagination.pageSize}
|
|
@@ -3,7 +3,7 @@ import {createStyles, DefaultProps, Group, Selectors, Space, Tooltip} from '@man
|
|
|
3
3
|
import {FunctionComponent, ReactNode} from 'react';
|
|
4
4
|
|
|
5
5
|
import {Button} from '../button';
|
|
6
|
-
import {useTable} from './
|
|
6
|
+
import {useTable} from './TableContext';
|
|
7
7
|
|
|
8
8
|
const useStyles = createStyles((theme) => ({
|
|
9
9
|
root: {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {Pagination} from '@mantine/core';
|
|
2
|
-
import {TableState} from '@tanstack/react-table';
|
|
3
2
|
import {FunctionComponent} from 'react';
|
|
4
|
-
|
|
3
|
+
|
|
4
|
+
import {useTable} from './TableContext';
|
|
5
5
|
|
|
6
6
|
interface TablePaginationProps {
|
|
7
7
|
/**
|
|
@@ -13,7 +13,7 @@ interface TablePaginationProps {
|
|
|
13
13
|
export const TablePagination: FunctionComponent<TablePaginationProps> = ({totalPages}) => {
|
|
14
14
|
const {state, setState, containerRef, getPageCount} = useTable();
|
|
15
15
|
const updatePage = (newPage: number) => {
|
|
16
|
-
setState((prevState
|
|
16
|
+
setState((prevState) => ({
|
|
17
17
|
...prevState,
|
|
18
18
|
pagination: {...prevState.pagination, pageIndex: newPage - 1},
|
|
19
19
|
}));
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import {Group, SegmentedControl, Text} from '@mantine/core';
|
|
2
|
-
import {TableState} from '@tanstack/react-table';
|
|
3
2
|
import {FunctionComponent} from 'react';
|
|
4
3
|
|
|
5
|
-
import {useTable} from './
|
|
4
|
+
import {useTable} from './TableContext';
|
|
6
5
|
|
|
7
6
|
interface TablePerPageProps {
|
|
8
7
|
/**
|
|
@@ -26,7 +25,7 @@ export const TablePerPage: FunctionComponent<TablePerPageProps> & {DEFAULT_SIZE:
|
|
|
26
25
|
const {state, setState} = useTable();
|
|
27
26
|
|
|
28
27
|
const updatePerPage = (newPerPage: string) => {
|
|
29
|
-
setState((prevState
|
|
28
|
+
setState((prevState) => ({
|
|
30
29
|
...prevState,
|
|
31
30
|
pagination: {pageIndex: 0, pageSize: parseInt(newPerPage, 10)},
|
|
32
31
|
}));
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {Select, SelectItem,
|
|
1
|
+
import {Group, Select, SelectItem, Text} from '@mantine/core';
|
|
2
2
|
import {FunctionComponent} from 'react';
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
import {useTable} from './TableContext';
|
|
4
5
|
|
|
5
6
|
interface TablePredicateProps {
|
|
6
7
|
/**
|
|
@@ -3,9 +3,9 @@ import {render, screen, userEvent, waitFor, within} from '@test-utils';
|
|
|
3
3
|
import {FunctionComponent} from 'react';
|
|
4
4
|
|
|
5
5
|
import {Table} from '../Table';
|
|
6
|
-
import {useTable} from '../
|
|
6
|
+
import {useTable} from '../TableContext';
|
|
7
7
|
|
|
8
|
-
type RowData = {firstName: string; lastName?: string};
|
|
8
|
+
type RowData = {id: string; firstName: string; lastName?: string};
|
|
9
9
|
|
|
10
10
|
const columnHelper = createColumnHelper<RowData>();
|
|
11
11
|
const columns: Array<ColumnDef<RowData>> = [
|
|
@@ -15,7 +15,13 @@ const columns: Array<ColumnDef<RowData>> = [
|
|
|
15
15
|
|
|
16
16
|
describe('Table', () => {
|
|
17
17
|
it('renders the data', () => {
|
|
18
|
-
render(
|
|
18
|
+
render(
|
|
19
|
+
<Table
|
|
20
|
+
getRowId={({id}) => id}
|
|
21
|
+
data={[{id: '🆔', firstName: 'first', lastName: 'last'}]}
|
|
22
|
+
columns={columns}
|
|
23
|
+
/>
|
|
24
|
+
);
|
|
19
25
|
|
|
20
26
|
expect(screen.getByRole('columnheader', {name: 'firstName'})).toBeVisible();
|
|
21
27
|
expect(screen.getByRole('columnheader', {name: 'lastName'})).toBeVisible();
|
|
@@ -45,7 +51,7 @@ describe('Table', () => {
|
|
|
45
51
|
enableSorting: false,
|
|
46
52
|
}),
|
|
47
53
|
];
|
|
48
|
-
render(<Table data={[{firstName: 'first', lastName: 'last'}]} columns={customColumns} />);
|
|
54
|
+
render(<Table data={[{id: '🆔', firstName: 'first', lastName: 'last'}]} columns={customColumns} />);
|
|
49
55
|
|
|
50
56
|
expect(screen.getByRole('columnheader', {name: 'First Name'})).toBeVisible();
|
|
51
57
|
expect(screen.getByRole('columnheader', {name: 'Last Name'})).toBeVisible();
|
|
@@ -141,7 +147,8 @@ describe('Table', () => {
|
|
|
141
147
|
];
|
|
142
148
|
render(
|
|
143
149
|
<Table
|
|
144
|
-
|
|
150
|
+
getRowId={({id}) => id}
|
|
151
|
+
data={[{id: '🆔', firstName: 'first', lastName: 'last'}]}
|
|
145
152
|
getExpandChildren={(row: RowData) => <Fixture row={row} />}
|
|
146
153
|
columns={customColumns}
|
|
147
154
|
/>
|
|
@@ -172,10 +179,11 @@ describe('Table', () => {
|
|
|
172
179
|
];
|
|
173
180
|
render(
|
|
174
181
|
<Table
|
|
182
|
+
getRowId={({id}) => id}
|
|
175
183
|
data={[
|
|
176
|
-
{firstName: 'Luke', lastName: 'Skywalker'},
|
|
177
|
-
{firstName: 'Lea', lastName: 'Skywalker'},
|
|
178
|
-
{firstName: 'Han', lastName: 'Solo'},
|
|
184
|
+
{id: '🆔-1', firstName: 'Luke', lastName: 'Skywalker'},
|
|
185
|
+
{id: '🆔-2', firstName: 'Lea', lastName: 'Skywalker'},
|
|
186
|
+
{id: '🆔-3', firstName: 'Han', lastName: 'Solo'},
|
|
179
187
|
]}
|
|
180
188
|
getExpandChildren={(row: RowData) => (row.lastName === 'Skywalker' ? <Fixture row={row} /> : null)}
|
|
181
189
|
columns={customColumns}
|
|
@@ -200,9 +208,10 @@ describe('Table', () => {
|
|
|
200
208
|
];
|
|
201
209
|
render(
|
|
202
210
|
<Table
|
|
211
|
+
getRowId={({id}) => id}
|
|
203
212
|
data={[
|
|
204
|
-
{firstName: 'Jack', lastName: 'Russel'},
|
|
205
|
-
{firstName: 'Golden', lastName: 'Retriever'},
|
|
213
|
+
{id: '🆔-1', firstName: 'Jack', lastName: 'Russel'},
|
|
214
|
+
{id: '🆔-2', firstName: 'Golden', lastName: 'Retriever'},
|
|
206
215
|
]}
|
|
207
216
|
getExpandChildren={(row: RowData) => <Fixture row={row} />}
|
|
208
217
|
columns={customColumns}
|
|
@@ -234,14 +243,18 @@ describe('Table', () => {
|
|
|
234
243
|
const doubleClickSpy = vi.fn();
|
|
235
244
|
render(
|
|
236
245
|
<Table<RowData>
|
|
237
|
-
|
|
246
|
+
getRowId={({id}) => id}
|
|
247
|
+
data={[
|
|
248
|
+
{id: '🆔-1', firstName: 'Mario'},
|
|
249
|
+
{id: '🆔-2', firstName: 'Luigi'},
|
|
250
|
+
]}
|
|
238
251
|
columns={columns}
|
|
239
252
|
doubleClickAction={doubleClickSpy}
|
|
240
253
|
></Table>
|
|
241
254
|
);
|
|
242
255
|
await user.dblClick(screen.getByRole('cell', {name: 'Mario'}));
|
|
243
256
|
expect(doubleClickSpy).toHaveBeenCalledTimes(1);
|
|
244
|
-
expect(doubleClickSpy).toHaveBeenCalledWith({firstName: 'Mario'});
|
|
257
|
+
expect(doubleClickSpy).toHaveBeenCalledWith({id: '🆔-1', firstName: 'Mario'});
|
|
245
258
|
});
|
|
246
259
|
|
|
247
260
|
it('reset row selection when user click outside the table', async () => {
|
|
@@ -250,9 +263,10 @@ describe('Table', () => {
|
|
|
250
263
|
<div>
|
|
251
264
|
<div>I'm a header</div>
|
|
252
265
|
<Table
|
|
266
|
+
getRowId={({id}) => id}
|
|
253
267
|
data={[
|
|
254
|
-
{firstName: 'first', lastName: 'last'},
|
|
255
|
-
{firstName: 'patate', lastName: 'king'},
|
|
268
|
+
{id: '🆔-1', firstName: 'first', lastName: 'last'},
|
|
269
|
+
{id: '🆔-2', firstName: 'patate', lastName: 'king'},
|
|
256
270
|
]}
|
|
257
271
|
columns={columns}
|
|
258
272
|
/>
|
|
@@ -276,9 +290,10 @@ describe('Table', () => {
|
|
|
276
290
|
it('displays a checkbox as the first cell of each row', () => {
|
|
277
291
|
render(
|
|
278
292
|
<Table
|
|
293
|
+
getRowId={({id}) => id}
|
|
279
294
|
data={[
|
|
280
|
-
{firstName: 'John', lastName: 'Smith'},
|
|
281
|
-
{firstName: 'Jane', lastName: 'Doe'},
|
|
295
|
+
{id: '🆔-1', firstName: 'John', lastName: 'Smith'},
|
|
296
|
+
{id: '🆔-2', firstName: 'Jane', lastName: 'Doe'},
|
|
282
297
|
]}
|
|
283
298
|
columns={columns}
|
|
284
299
|
multiRowSelectionEnabled
|
|
@@ -293,13 +308,33 @@ describe('Table', () => {
|
|
|
293
308
|
});
|
|
294
309
|
});
|
|
295
310
|
|
|
311
|
+
it('selects the rows specified in the initial state on mount', () => {
|
|
312
|
+
render(
|
|
313
|
+
<Table
|
|
314
|
+
getRowId={({id}) => id}
|
|
315
|
+
data={[
|
|
316
|
+
{id: '🆔-1', firstName: 'John', lastName: 'Smith'},
|
|
317
|
+
{id: '🆔-2', firstName: 'Jane', lastName: 'Doe'},
|
|
318
|
+
]}
|
|
319
|
+
columns={columns}
|
|
320
|
+
multiRowSelectionEnabled
|
|
321
|
+
initialState={{
|
|
322
|
+
rowSelection: {'🆔-2': {id: '🆔-2', firstName: 'Jane', lastName: 'Doe'}},
|
|
323
|
+
}}
|
|
324
|
+
/>
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
expect(screen.getByRole('row', {name: /jane doe/i, selected: true})).toBeInTheDocument();
|
|
328
|
+
});
|
|
329
|
+
|
|
296
330
|
it('selects all rows of the current page when clicking on the checkbox that is in the column header', async () => {
|
|
297
331
|
const user = userEvent.setup({delay: null});
|
|
298
332
|
render(
|
|
299
333
|
<Table
|
|
334
|
+
getRowId={({id}) => id}
|
|
300
335
|
data={[
|
|
301
|
-
{firstName: 'John', lastName: 'Smith'},
|
|
302
|
-
{firstName: 'Jane', lastName: 'Doe'},
|
|
336
|
+
{id: '🆔-1', firstName: 'John', lastName: 'Smith'},
|
|
337
|
+
{id: '🆔-2', firstName: 'Jane', lastName: 'Doe'},
|
|
303
338
|
]}
|
|
304
339
|
columns={columns}
|
|
305
340
|
multiRowSelectionEnabled
|
|
@@ -321,9 +356,10 @@ describe('Table', () => {
|
|
|
321
356
|
<div>
|
|
322
357
|
<div>I'm a header</div>
|
|
323
358
|
<Table
|
|
359
|
+
getRowId={({id}) => id}
|
|
324
360
|
data={[
|
|
325
|
-
{firstName: 'first', lastName: 'last'},
|
|
326
|
-
{firstName: 'patate', lastName: 'king'},
|
|
361
|
+
{id: '🆔-1', firstName: 'first', lastName: 'last'},
|
|
362
|
+
{id: '🆔-2', firstName: 'patate', lastName: 'king'},
|
|
327
363
|
]}
|
|
328
364
|
columns={columns}
|
|
329
365
|
multiRowSelectionEnabled
|
|
@@ -348,9 +384,10 @@ describe('Table', () => {
|
|
|
348
384
|
const user = userEvent.setup({delay: null});
|
|
349
385
|
render(
|
|
350
386
|
<Table
|
|
387
|
+
getRowId={({id}) => id}
|
|
351
388
|
data={[
|
|
352
|
-
{firstName: 'John', lastName: 'Smith'},
|
|
353
|
-
{firstName: 'Jane', lastName: 'Doe'},
|
|
389
|
+
{id: '🆔-1', firstName: 'John', lastName: 'Smith'},
|
|
390
|
+
{id: '🆔-2', firstName: 'Jane', lastName: 'Doe'},
|
|
354
391
|
]}
|
|
355
392
|
columns={columns}
|
|
356
393
|
multiRowSelectionEnabled
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export * from './Table';
|
|
2
|
-
export {
|
|
3
|
-
export
|
|
2
|
+
export {useTable} from './TableContext';
|
|
3
|
+
export {type onTableChangeEvent, type InitialTableState, type TableState, type TableProps} from './Table.types';
|