@onewelcome/react-lib-components 0.1.7-alpha → 0.1.8-alpha

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.
Files changed (69) hide show
  1. package/dist/DataGrid/DataGrid.d.ts +30 -0
  2. package/dist/DataGrid/DataGridActions/DataGridActions.d.ts +14 -0
  3. package/dist/DataGrid/DataGridActions/DataGridColumnsToggle.d.ts +13 -0
  4. package/dist/DataGrid/DataGridBody/DataGridBody.d.ts +16 -0
  5. package/dist/DataGrid/DataGridBody/DataGridCell.d.ts +6 -0
  6. package/dist/DataGrid/DataGridBody/DataGridRow.d.ts +7 -0
  7. package/dist/DataGrid/DataGridHeader/DataGridHeader.d.ts +10 -0
  8. package/dist/DataGrid/DataGridHeader/DataGridHeaderCell.d.ts +10 -0
  9. package/dist/DataGrid/datagrid.interfaces.d.ts +13 -0
  10. package/dist/Form/Select/Option.d.ts +9 -4
  11. package/dist/Form/Select/Select.d.ts +8 -2
  12. package/dist/Form/Wrapper/SelectWrapper/SelectWrapper.d.ts +1 -1
  13. package/dist/Icon/Icon.d.ts +1 -0
  14. package/dist/_BaseStyling_/BaseStyling.d.ts +4 -0
  15. package/dist/hooks/useSpacing.d.ts +1 -1
  16. package/dist/hooks/useWrapper.d.ts +1 -1
  17. package/dist/index.d.ts +5 -0
  18. package/dist/react-lib-components.cjs.development.js +949 -272
  19. package/dist/react-lib-components.cjs.development.js.map +1 -1
  20. package/dist/react-lib-components.cjs.production.min.js +1 -1
  21. package/dist/react-lib-components.cjs.production.min.js.map +1 -1
  22. package/dist/react-lib-components.esm.js +947 -273
  23. package/dist/react-lib-components.esm.js.map +1 -1
  24. package/dist/util/helper.d.ts +1 -1
  25. package/package.json +11 -11
  26. package/src/ContextMenu/ContextMenu.tsx +16 -1
  27. package/src/DataGrid/DataGrid.module.scss +21 -0
  28. package/src/DataGrid/DataGrid.test.tsx +276 -0
  29. package/src/DataGrid/DataGrid.tsx +101 -0
  30. package/src/DataGrid/DataGridActions/DataGridActions.module.scss +35 -0
  31. package/src/DataGrid/DataGridActions/DataGridActions.test.tsx +184 -0
  32. package/src/DataGrid/DataGridActions/DataGridActions.tsx +109 -0
  33. package/src/DataGrid/DataGridActions/DataGridColumnsToggle.module.scss +41 -0
  34. package/src/DataGrid/DataGridActions/DataGridColumnsToggle.test.tsx +83 -0
  35. package/src/DataGrid/DataGridActions/DataGridColumnsToggle.tsx +88 -0
  36. package/src/DataGrid/DataGridBody/DataGridBody.module.scss +10 -0
  37. package/src/DataGrid/DataGridBody/DataGridBody.test.tsx +123 -0
  38. package/src/DataGrid/DataGridBody/DataGridBody.tsx +64 -0
  39. package/src/DataGrid/DataGridBody/DataGridCell.module.scss +33 -0
  40. package/src/DataGrid/DataGridBody/DataGridCell.test.tsx +74 -0
  41. package/src/DataGrid/DataGridBody/DataGridCell.tsx +25 -0
  42. package/src/DataGrid/DataGridBody/DataGridRow.module.scss +7 -0
  43. package/src/DataGrid/DataGridBody/DataGridRow.test.tsx +101 -0
  44. package/src/DataGrid/DataGridBody/DataGridRow.tsx +27 -0
  45. package/src/DataGrid/DataGridBody/__snapshots__/DataGridBody.test.tsx.snap +258 -0
  46. package/src/DataGrid/DataGridHeader/DataGridHeader.module.scss +26 -0
  47. package/src/DataGrid/DataGridHeader/DataGridHeader.test.tsx +255 -0
  48. package/src/DataGrid/DataGridHeader/DataGridHeader.tsx +80 -0
  49. package/src/DataGrid/DataGridHeader/DataGridHeaderCell.module.scss +68 -0
  50. package/src/DataGrid/DataGridHeader/DataGridHeaderCell.test.tsx +128 -0
  51. package/src/DataGrid/DataGridHeader/DataGridHeaderCell.tsx +72 -0
  52. package/src/DataGrid/datagrid.interfaces.ts +14 -0
  53. package/src/Form/Select/Option.tsx +39 -21
  54. package/src/Form/Select/Select.module.scss +1 -1
  55. package/src/Form/Select/Select.test.tsx +235 -56
  56. package/src/Form/Select/Select.tsx +194 -34
  57. package/src/Form/Toggle/Toggle.module.scss +1 -0
  58. package/src/Form/Wrapper/SelectWrapper/SelectWrapper.test.tsx +44 -0
  59. package/src/Form/Wrapper/SelectWrapper/SelectWrapper.tsx +4 -2
  60. package/src/Icon/Icon.module.scss +4 -0
  61. package/src/Icon/Icon.tsx +1 -0
  62. package/src/Notifications/BaseModal/BaseModal.module.scss +1 -1
  63. package/src/Notifications/BaseModal/BaseModal.test.tsx +2 -1
  64. package/src/Notifications/Dialog/Dialog.module.scss +1 -1
  65. package/src/Notifications/Snackbar/SnackbarContainer/SnackbarContainer.module.scss +1 -1
  66. package/src/Notifications/Snackbar/SnackbarItem/SnackbarItem.module.scss +1 -1
  67. package/src/Pagination/Pagination.module.scss +74 -74
  68. package/src/_BaseStyling_/BaseStyling.tsx +14 -6
  69. package/src/index.ts +6 -0
@@ -1,6 +1,6 @@
1
1
  declare type KeyValuePair = {
2
2
  [key: string]: unknown;
3
3
  };
4
- export declare const generateID: (length?: number, stringToWeaveIn?: string) => string;
4
+ export declare const generateID: (length?: number, stringToWeaveIn?: string | undefined) => string;
5
5
  export declare const filterProps: (props: any, regexPattern: RegExp, returnFiltered?: boolean) => KeyValuePair;
6
6
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onewelcome/react-lib-components",
3
- "version": "0.1.7-alpha",
3
+ "version": "0.1.8-alpha",
4
4
  "license": "Apache-2.0",
5
5
  "author": "OneWelcome B.V.",
6
6
  "main": "dist/index.js",
@@ -34,7 +34,7 @@
34
34
  "react": ">=16"
35
35
  },
36
36
  "engines": {
37
- "node": ">=12"
37
+ "node": ">=16"
38
38
  },
39
39
  "size-limit": [
40
40
  {
@@ -49,16 +49,16 @@
49
49
  "devDependencies": {
50
50
  "@babel/core": "^7.18.0",
51
51
  "@size-limit/preset-small-lib": "^7.0.8",
52
- "@storybook/addon-a11y": "^6.5.3",
53
- "@storybook/addon-docs": "^6.5.3",
54
- "@storybook/addon-essentials": "^6.5.3",
55
- "@storybook/addon-links": "^6.5.3",
56
- "@storybook/addons": "^6.5.3",
57
- "@storybook/builder-webpack5": "^6.5.3",
58
- "@storybook/manager-webpack5": "^6.5.3",
52
+ "@storybook/addon-a11y": "^6.5.8",
53
+ "@storybook/addon-docs": "^6.5.8",
54
+ "@storybook/addon-essentials": "^6.5.8",
55
+ "@storybook/addon-links": "^6.5.8",
56
+ "@storybook/addons": "^6.5.8",
57
+ "@storybook/builder-webpack5": "^6.5.8",
58
+ "@storybook/manager-webpack5": "^6.5.8",
59
59
  "@storybook/preset-scss": "^1.0.3",
60
- "@storybook/react": "^6.5.3",
61
- "@storybook/theming": "^6.5.6",
60
+ "@storybook/react": "^6.5.8",
61
+ "@storybook/theming": "^6.5.8",
62
62
  "@testing-library/dom": "^8.13.0",
63
63
  "@testing-library/jest-dom": "^5.16.4",
64
64
  "@testing-library/react": "^12.1.5",
@@ -63,7 +63,22 @@ export const ContextMenu = React.forwardRef<HTMLDivElement, Props>(
63
63
  setFocusedContextMenuItem(selectedContextMenuItem);
64
64
  }
65
65
 
66
- event.preventDefault();
66
+ const codesToPrevenDefault = [
67
+ 'ArrowDown',
68
+ 'ArrowUp',
69
+ 'ArrowLeft',
70
+ 'ArrowRight',
71
+ 'Enter',
72
+ 'Space',
73
+ 'Escape',
74
+ 'End',
75
+ 'Home',
76
+ ];
77
+
78
+ if (codesToPrevenDefault.includes(event.code)) {
79
+ event.preventDefault();
80
+ }
81
+
67
82
  switch (event.code) {
68
83
  case 'ArrowDown':
69
84
  if (!showContextMenu) {
@@ -0,0 +1,21 @@
1
+ .table-wrapper {
2
+ overflow-x: auto;
3
+ }
4
+
5
+ .table {
6
+ border-collapse: collapse;
7
+ padding-top: 0.5rem;
8
+ width: 100%;
9
+ position: relative;
10
+ }
11
+
12
+ .pagination {
13
+ margin-top: 1rem;
14
+ padding: 0 1rem;
15
+ }
16
+
17
+ @media only screen and (min-width: 50em) {
18
+ .pagination {
19
+ padding: 0 1.25rem;
20
+ }
21
+ }
@@ -0,0 +1,276 @@
1
+ import React, { useEffect, useRef } from 'react';
2
+ import { DataGrid, Props } from './DataGrid';
3
+ import { getAllByRole, render, queryAllByRole, getByRole } from '@testing-library/react';
4
+ import { DataGridRow } from './DataGridBody/DataGridRow';
5
+ import { DataGridCell } from './DataGridBody/DataGridCell';
6
+ import { ContextMenu } from '../ContextMenu/ContextMenu';
7
+ import { IconButton } from '../Button/IconButton';
8
+ import { Icon, Icons } from '../Icon/Icon';
9
+ import { ContextMenuItem } from '../ContextMenu/ContextMenuItem';
10
+ import userEvent from '@testing-library/user-event';
11
+
12
+ type DataType = { firstName: string; lastName: string; date: string };
13
+
14
+ const defaultParams: Props<DataType> = {
15
+ children: ({ item }) => (
16
+ <DataGridRow key={item.firstName}>
17
+ <DataGridCell>{item.firstName}</DataGridCell>
18
+ <DataGridCell>{item.lastName}</DataGridCell>
19
+ <DataGridCell>{item.date}</DataGridCell>
20
+ <DataGridCell>
21
+ <ContextMenu
22
+ id={`consent_menu_${item.firstName}`}
23
+ trigger={
24
+ <IconButton title={`Actions for ${item.firstName}`} color="default">
25
+ <Icon icon={Icons.EllipsisAlt} />
26
+ </IconButton>
27
+ }
28
+ >
29
+ <ContextMenuItem>Item 1</ContextMenuItem>
30
+ </ContextMenu>
31
+ </DataGridCell>
32
+ </DataGridRow>
33
+ ),
34
+ headers: [
35
+ { name: 'firstName', headline: 'First name' },
36
+ { name: 'lastName', headline: 'Last name' },
37
+ { name: 'date', headline: 'Date' },
38
+ ],
39
+ enableMultiSorting: true,
40
+ data: [
41
+ { firstName: 'Paweł', lastName: 'Napieracz', date: '12.12.1990' },
42
+ { firstName: 'Michał', lastName: 'Górski', date: '12.12.1994' },
43
+ { firstName: 'Daniel', lastName: 'Velden', date: '12.12.199x' },
44
+ { firstName: 'Jasha', lastName: 'Joachimsthal', date: '12.12.198x' },
45
+ ],
46
+ };
47
+
48
+ const createDataGrid = (params?: (defaultParams: Props<DataType>) => Props<DataType>) => {
49
+ let parameters = defaultParams;
50
+ if (params) {
51
+ parameters = params(defaultParams);
52
+ }
53
+ const queries = render(<DataGrid {...parameters} data-testid="dataGrid" />);
54
+ const dataGrid = queries.getByTestId('dataGrid');
55
+
56
+ return {
57
+ ...queries,
58
+ dataGrid,
59
+ };
60
+ };
61
+
62
+ describe('DataGrid should render', () => {
63
+ it('renders without crashing', () => {
64
+ const rowsCount = defaultParams.data?.length!;
65
+ const colsCount = defaultParams.headers?.length!;
66
+ const contextMenuCellCount = 1;
67
+ const { dataGrid, ...queries } = createDataGrid();
68
+
69
+ expect(dataGrid).toBeDefined();
70
+
71
+ expect(dataGrid.querySelector('.actions')).toBeNull();
72
+
73
+ const [thead, tbody] = queries.getAllByRole('rowgroup');
74
+
75
+ expect(queryAllByRole(thead, 'columnheader')).toHaveLength(3);
76
+ expect(queryAllByRole(thead, 'cell')).toHaveLength(contextMenuCellCount);
77
+ expect(queryAllByRole(thead, 'button')).toHaveLength(0); //sorting should be disabled by default
78
+
79
+ expect(queryAllByRole(tbody, 'cell')).toHaveLength(
80
+ rowsCount * (colsCount + contextMenuCellCount)
81
+ );
82
+ });
83
+
84
+ it('renders actions buttons', () => {
85
+ const { dataGrid } = createDataGrid((params) => ({
86
+ ...params,
87
+ actions: {
88
+ enableAddBtn: true,
89
+ enableColumnsBtn: true,
90
+ enableSearchBtn: true,
91
+ },
92
+ }));
93
+ const [addBtn, desktopColumnsBtn, mobileColumnsBtn, desktopSearchBtn, mobileSearchBtn] =
94
+ getAllByRole(dataGrid.querySelector('.actions')!, 'button');
95
+ expect(addBtn).toHaveTextContent('Add item');
96
+ expect(desktopColumnsBtn).toHaveTextContent('Columns');
97
+ expect(mobileColumnsBtn).toHaveTextContent('Show/hide columns');
98
+ expect(desktopSearchBtn).toHaveTextContent('Search');
99
+ expect(mobileSearchBtn).toHaveTextContent('Search');
100
+ });
101
+
102
+ it('renders table with initialized sorting indicators', () => {
103
+ const { getAllByRole } = createDataGrid((params) => ({
104
+ ...params,
105
+ onSort: jest.fn(),
106
+ initialSort: [
107
+ { name: defaultParams.headers[0].name, direction: 'DESC' },
108
+ { name: defaultParams.headers[2].name, direction: 'ASC' },
109
+ ],
110
+ }));
111
+
112
+ const [firstNameHeader, lastNameHeader, dateHeader] = getAllByRole('columnheader');
113
+ expect(firstNameHeader).toHaveAttribute('aria-sort', 'descending');
114
+ expect(lastNameHeader).not.toHaveAttribute('aria-sort');
115
+ expect(dateHeader).toHaveAttribute('aria-sort', 'ascending');
116
+ });
117
+
118
+ it('renders table without context menu', () => {
119
+ const rowsCount = defaultParams.data?.length!;
120
+ const colsCount = defaultParams.headers?.length!;
121
+ const { getAllByRole } = createDataGrid((params) => ({
122
+ ...params,
123
+ disableContextMenuColumn: true,
124
+ children: ({ item }) => (
125
+ <DataGridRow key={item.firstName}>
126
+ <DataGridCell>{item.firstName}</DataGridCell>
127
+ <DataGridCell>{item.lastName}</DataGridCell>
128
+ <DataGridCell>{item.date}</DataGridCell>
129
+ </DataGridRow>
130
+ ),
131
+ }));
132
+
133
+ expect(getAllByRole('columnheader')).toHaveLength(3);
134
+ expect(getAllByRole('cell')).toHaveLength(rowsCount * colsCount);
135
+ });
136
+
137
+ it('renders loading state with skeleton loading animation on data cells and empty row on content menu cells', () => {
138
+ const { ...queries } = createDataGrid((params) => ({ ...params, isLoading: true }));
139
+
140
+ const [_thead, tbody] = queries.getAllByRole('rowgroup');
141
+ expect(
142
+ getAllByRole(tbody, 'cell').find((cell, index) => {
143
+ const contextMenuCell = index % 4 === 3;
144
+ return (
145
+ !contextMenuCell && (cell.firstChild as HTMLElement).getAttribute('class') !== 'loading'
146
+ );
147
+ })
148
+ ).toBeUndefined();
149
+ });
150
+
151
+ it('renders empty state', () => {
152
+ const emptyLabel = 'emptyLabel';
153
+ const { ...queries } = createDataGrid((params) => ({ ...params, emptyLabel, data: [] }));
154
+
155
+ const [_thead, tbody] = queries.getAllByRole('rowgroup');
156
+ const rows = getAllByRole(tbody, 'row');
157
+ const firstRowCells = getAllByRole(rows[0], 'cell');
158
+ expect(firstRowCells).toHaveLength(1);
159
+ expect(firstRowCells[0]).toHaveAttribute('colspan', `${defaultParams.headers.length + 1}`);
160
+ expect(firstRowCells[0]).toHaveTextContent(emptyLabel);
161
+ });
162
+
163
+ it('renders pagination', () => {
164
+ const { getByRole } = createDataGrid((params) => ({
165
+ ...params,
166
+ paginationProps: {
167
+ totalElements: 105,
168
+ pageSize: 10,
169
+ currentPage: 2,
170
+ onPageChange: jest.fn(),
171
+ onPageSizeChange: jest.fn(),
172
+ },
173
+ }));
174
+
175
+ expect(getByRole('button', { name: 'previous' })).toBeDefined();
176
+ expect(getByRole('button', { name: 'next' })).toBeDefined();
177
+ expect(getByRole('button', { name: 'last' })).toBeDefined();
178
+ });
179
+ });
180
+
181
+ describe('DataGrid should have interactive table header', () => {
182
+ it('clicking table headers should call sort callback', () => {
183
+ const onSort = jest.fn();
184
+ const { getAllByRole } = createDataGrid((params) => ({
185
+ ...params,
186
+ onSort,
187
+ }));
188
+
189
+ const [firstNameHeader, lastNameHeader, dateHeader] = getAllByRole('columnheader');
190
+
191
+ userEvent.click(getByRole(firstNameHeader, 'button'));
192
+ expect(onSort).toBeCalledWith([{ name: defaultParams.headers[0].name, direction: 'ASC' }]);
193
+
194
+ userEvent.click(getByRole(lastNameHeader, 'button'));
195
+ userEvent.click(getByRole(dateHeader, 'button'));
196
+ expect(onSort).toBeCalledWith([
197
+ { name: defaultParams.headers[2].name, direction: 'ASC' },
198
+ { name: defaultParams.headers[1].name, direction: 'ASC' },
199
+ { name: defaultParams.headers[0].name, direction: 'ASC' },
200
+ ]);
201
+
202
+ userEvent.click(getByRole(firstNameHeader, 'button'));
203
+ expect(onSort).toBeCalledWith([
204
+ { name: defaultParams.headers[0].name, direction: 'DESC' },
205
+ { name: defaultParams.headers[2].name, direction: 'ASC' },
206
+ { name: defaultParams.headers[1].name, direction: 'ASC' },
207
+ ]);
208
+
209
+ userEvent.click(getByRole(dateHeader, 'button'));
210
+ expect(onSort).toBeCalledWith([
211
+ { name: defaultParams.headers[2].name, direction: 'DESC' },
212
+ { name: defaultParams.headers[0].name, direction: 'DESC' },
213
+ { name: defaultParams.headers[1].name, direction: 'ASC' },
214
+ ]);
215
+
216
+ userEvent.click(getByRole(dateHeader, 'button'));
217
+ userEvent.click(getByRole(firstNameHeader, 'button'));
218
+ userEvent.click(getByRole(lastNameHeader, 'button'));
219
+ expect(onSort).toBeCalledWith([{ name: defaultParams.headers[1].name, direction: 'DESC' }]);
220
+ expect(onSort).toBeCalledTimes(8);
221
+ });
222
+
223
+ it('clicking on show/hide columns popover should show/hide columns', async () => {
224
+ const { getByRole, findByLabelText, ...queries } = createDataGrid((params) => ({
225
+ ...params,
226
+ actions: { enableColumnsBtn: true },
227
+ }));
228
+ expect(queries.getAllByRole('columnheader')).toHaveLength(3);
229
+
230
+ userEvent.click(getByRole('button', { name: 'Columns' }));
231
+
232
+ const nameToggle = await findByLabelText(defaultParams.headers[1].headline);
233
+ expect(nameToggle).toBeChecked();
234
+ userEvent.click(nameToggle);
235
+ expect(nameToggle).not.toBeChecked();
236
+ expect(getByRole('button', { name: 'Close show columns dialog' })).toBeDefined();
237
+
238
+ const columns = queries.getAllByRole('columnheader');
239
+ expect(columns).toHaveLength(2);
240
+ expect(columns[0]).toHaveTextContent(defaultParams.headers[0].headline);
241
+ expect(columns[1]).toHaveTextContent(defaultParams.headers[2].headline);
242
+
243
+ const rowsCount = defaultParams.data?.length!;
244
+ const colsCount = defaultParams.headers.length;
245
+ const hiddenColsCount = defaultParams.headers.length;
246
+ const contextMenuColsCount = defaultParams.headers.length;
247
+ const [_thead, tbody] = queries.getAllByRole('rowgroup');
248
+ expect(getAllByRole(tbody, 'cell')).toHaveLength(
249
+ rowsCount * (colsCount - hiddenColsCount + contextMenuColsCount)
250
+ );
251
+ });
252
+ });
253
+
254
+ describe('ref should work', () => {
255
+ it('should give back the proper data prop, this also checks if the component propagates ...rest properly', () => {
256
+ const ExampleComponent = ({
257
+ propagateRef,
258
+ }: {
259
+ propagateRef: (ref: React.RefObject<HTMLElement>) => void;
260
+ }) => {
261
+ const ref = useRef(null);
262
+
263
+ useEffect(() => {
264
+ propagateRef(ref);
265
+ }, [ref]);
266
+
267
+ return <DataGrid {...defaultParams} data-ref="testing" ref={ref} />;
268
+ };
269
+
270
+ const refCheck = (ref: React.RefObject<HTMLElement>) => {
271
+ expect(ref.current).toHaveAttribute('data-ref', 'testing');
272
+ };
273
+
274
+ render(<ExampleComponent propagateRef={refCheck} />);
275
+ });
276
+ });
@@ -0,0 +1,101 @@
1
+ import React, { ComponentPropsWithRef, ReactElement, Ref, useEffect, useState } from 'react';
2
+ import { Props as ButtonProps } from '../Button/Button';
3
+ import classes from './DataGrid.module.scss';
4
+ import { DataGridHeader } from './DataGridHeader/DataGridHeader';
5
+ import { DataGridActions } from './DataGridActions/DataGridActions';
6
+ import { DataGridBody } from './DataGridBody/DataGridBody';
7
+ import { ColumnName, HeaderCell, OnSortFunction, Sort } from './datagrid.interfaces';
8
+ import { Pagination, Props as PaginationProps } from '../Pagination/Pagination';
9
+
10
+ export interface Props<T> extends ComponentPropsWithRef<'div'> {
11
+ children: ({ item, index }: { item: T; index: number }) => ReactElement;
12
+ data?: T[];
13
+ initialSort?: Sort;
14
+ onSort?: OnSortFunction;
15
+ headers: HeaderCell[];
16
+ actions?: {
17
+ enableAddBtn?: boolean;
18
+ enableColumnsBtn?: boolean;
19
+ enableSearchBtn?: boolean;
20
+ addBtnProps?: ButtonProps;
21
+ columnsBtnProps?: ButtonProps;
22
+ searchBtnProps?: ButtonProps;
23
+ };
24
+ emptyLabel?: string;
25
+ paginationProps?: PaginationProps;
26
+ disableContextMenuColumn?: boolean;
27
+ isLoading?: boolean;
28
+ enableMultiSorting?: boolean;
29
+ }
30
+
31
+ const DataGridInner = <T extends {}>(
32
+ {
33
+ children,
34
+ data,
35
+ initialSort,
36
+ onSort,
37
+ headers,
38
+ actions = {},
39
+ paginationProps,
40
+ disableContextMenuColumn,
41
+ isLoading,
42
+ enableMultiSorting,
43
+ emptyLabel,
44
+ ...rest
45
+ }: Props<T>,
46
+ ref: Ref<HTMLDivElement>
47
+ ) => {
48
+ if (!headers) {
49
+ throw new Error('Headers definition has to be provided');
50
+ }
51
+ if (!children) {
52
+ throw new Error('DataGridBody should be provieded with a template how to render rows');
53
+ }
54
+
55
+ const [internalHeaders, setInternalHeaders] = useState(headers);
56
+
57
+ useEffect(() => setInternalHeaders(headers), [headers]);
58
+
59
+ const onColumnToggled = (colName: ColumnName) => {
60
+ setInternalHeaders(
61
+ internalHeaders.map((item) =>
62
+ item.name !== colName ? item : { ...item, hidden: !item.hidden }
63
+ )
64
+ );
65
+ };
66
+
67
+ return (
68
+ <div {...rest} ref={ref}>
69
+ <DataGridActions {...actions} headers={internalHeaders} onColumnToggled={onColumnToggled} />
70
+ <div className={classes['table-wrapper']}>
71
+ <table className={classes['table']}>
72
+ <DataGridHeader
73
+ headers={internalHeaders}
74
+ initialSort={initialSort}
75
+ onSort={onSort}
76
+ disableContextMenuColumn={disableContextMenuColumn}
77
+ enableMultiSorting={enableMultiSorting}
78
+ />
79
+ <DataGridBody
80
+ children={children}
81
+ data={data}
82
+ headers={internalHeaders}
83
+ isLoading={isLoading}
84
+ disableContextMenuColumn={disableContextMenuColumn}
85
+ emptyLabel={emptyLabel}
86
+ />
87
+ </table>
88
+ </div>
89
+ {paginationProps && !isLoading && (
90
+ <Pagination
91
+ {...paginationProps}
92
+ className={`${classes['pagination']} ${paginationProps.className ?? ''}`}
93
+ />
94
+ )}
95
+ </div>
96
+ );
97
+ };
98
+
99
+ export const DataGrid = React.forwardRef(DataGridInner) as <T extends {}>(
100
+ p: Props<T> & { ref?: Ref<HTMLDivElement> }
101
+ ) => ReactElement;
@@ -0,0 +1,35 @@
1
+ .actions {
2
+ margin-bottom: 1rem;
3
+ padding: 0 1rem;
4
+ display: flex;
5
+ flex-wrap: wrap;
6
+ }
7
+
8
+ .left-actions {
9
+ flex: 1;
10
+ }
11
+
12
+ .right-actions {
13
+ display: flex;
14
+ gap: 1rem;
15
+ }
16
+
17
+ .desktop {
18
+ display: none;
19
+ }
20
+
21
+ @media only screen and (min-width: 50em) {
22
+ .actions {
23
+ padding: 0 1.25rem;
24
+ }
25
+ }
26
+
27
+ @media only screen and (min-width: 30em) {
28
+ .desktop {
29
+ display: block;
30
+ }
31
+
32
+ .mobile {
33
+ display: none;
34
+ }
35
+ }
@@ -0,0 +1,184 @@
1
+ import React, { useEffect, useRef } from 'react';
2
+ import { DataGridActions, Props } from './DataGridActions';
3
+ import { render } from '@testing-library/react';
4
+ import userEvent from '@testing-library/user-event';
5
+
6
+ const defaultParams: Props = {
7
+ headers: [],
8
+ onColumnToggled: jest.fn(),
9
+ };
10
+
11
+ const createDataGridActions = (params?: (defaultParams: Props) => Props) => {
12
+ let parameters: Props = defaultParams;
13
+ if (params) {
14
+ parameters = params(defaultParams);
15
+ }
16
+ const queries = render(<DataGridActions {...parameters} data-testid="dataGridActions" />);
17
+ const dataGridActions = queries.getByTestId('dataGridActions');
18
+
19
+ return {
20
+ ...queries,
21
+ dataGridActions,
22
+ };
23
+ };
24
+
25
+ describe('DataGridActions should render', () => {
26
+ it('renders without crashing', () => {
27
+ const { container } = render(<DataGridActions {...defaultParams} />);
28
+
29
+ expect(container).toBeEmptyDOMElement();
30
+ });
31
+
32
+ it('renders all buttons', () => {
33
+ const { dataGridActions, getAllByRole } = createDataGridActions((params) => ({
34
+ ...params,
35
+ enableAddBtn: true,
36
+ enableColumnsBtn: true,
37
+ enableSearchBtn: true,
38
+ }));
39
+
40
+ expect(dataGridActions).toBeDefined();
41
+ const buttons = getAllByRole('button');
42
+ expect(buttons).toHaveLength(5);
43
+ expect(buttons[0]).toHaveTextContent('Add item');
44
+ expect(buttons[1]).toHaveTextContent('Columns');
45
+ expect(buttons[2]).toHaveTextContent('Show/hide columns');
46
+ expect(buttons[3]).toHaveTextContent('Search');
47
+ expect(buttons[4]).toHaveTextContent('Search');
48
+ });
49
+
50
+ it('renders only add button', () => {
51
+ const { dataGridActions, getAllByRole } = createDataGridActions((params) => ({
52
+ ...params,
53
+ enableAddBtn: true,
54
+ }));
55
+
56
+ expect(dataGridActions).toBeDefined();
57
+ const buttons = getAllByRole('button');
58
+ expect(buttons).toHaveLength(1);
59
+ expect(buttons[0]).toHaveTextContent('Add item');
60
+ });
61
+
62
+ it('renders only columns button', () => {
63
+ const { dataGridActions, getAllByRole } = createDataGridActions((params) => ({
64
+ ...params,
65
+ enableColumnsBtn: true,
66
+ }));
67
+
68
+ expect(dataGridActions).toBeDefined();
69
+ const buttons = getAllByRole('button');
70
+ expect(buttons).toHaveLength(2);
71
+ expect(buttons[0]).toHaveTextContent('Columns');
72
+ expect(buttons[1]).toHaveTextContent('Show/hide columns');
73
+ });
74
+
75
+ it('renders only search button', () => {
76
+ const { dataGridActions, getAllByRole } = createDataGridActions((params) => ({
77
+ ...params,
78
+ enableSearchBtn: true,
79
+ }));
80
+
81
+ expect(dataGridActions).toBeDefined();
82
+ const buttons = getAllByRole('button');
83
+ expect(buttons).toHaveLength(2);
84
+ expect(buttons[0]).toHaveTextContent('Search');
85
+ expect(buttons[1]).toHaveTextContent('Search');
86
+ });
87
+
88
+ it('renders buttons with overwritten props', () => {
89
+ const addBtnProps = { children: 'button1', title: 'title1' };
90
+ const columnsBtnProps = { children: 'button2', title: 'title2' };
91
+ const searchBtnProps = { children: 'button3', title: 'title3' };
92
+ const { dataGridActions, getAllByRole } = createDataGridActions((params) => ({
93
+ ...params,
94
+ enableAddBtn: true,
95
+ enableColumnsBtn: true,
96
+ enableSearchBtn: true,
97
+ addBtnProps,
98
+ columnsBtnProps,
99
+ searchBtnProps,
100
+ }));
101
+
102
+ expect(dataGridActions).toBeDefined();
103
+ const [addBtn, desktopColumnsBtn, mobileColumnsBtn, desktopSearchBtn, mobileSearchBtn] =
104
+ getAllByRole('button');
105
+ expect(addBtn).toHaveTextContent(addBtnProps.children);
106
+ expect(addBtn).toHaveAttribute('title', addBtnProps.title);
107
+ expect(desktopColumnsBtn).toHaveTextContent(columnsBtnProps.children);
108
+ expect(desktopColumnsBtn).toHaveAttribute('title', columnsBtnProps.title);
109
+ expect(mobileColumnsBtn).toHaveTextContent(columnsBtnProps.title);
110
+ expect(desktopSearchBtn).toHaveTextContent(searchBtnProps.children);
111
+ expect(desktopSearchBtn).toHaveAttribute('title', searchBtnProps.title);
112
+ expect(mobileSearchBtn).toHaveTextContent(searchBtnProps.title);
113
+ });
114
+ });
115
+
116
+ describe('DataGridActions should be interactive', () => {
117
+ it('clicking on columns button opens show/hide columns popover', async () => {
118
+ const toggleHeader = { name: 'test', headline: 'Label' };
119
+ const { getAllByRole, findByLabelText } = createDataGridActions((params) => ({
120
+ ...params,
121
+ enableColumnsBtn: true,
122
+ headers: [toggleHeader],
123
+ }));
124
+
125
+ userEvent.click(getAllByRole('button')[0]);
126
+
127
+ const toggle = await findByLabelText(toggleHeader.headline);
128
+ expect(toggle).toBeDefined();
129
+ expect(toggle).toBeChecked();
130
+
131
+ userEvent.click(toggle);
132
+ expect(defaultParams.onColumnToggled).toBeCalledWith(toggleHeader.name);
133
+ });
134
+
135
+ it('clicking on add button runs callback', () => {
136
+ const onClick = jest.fn();
137
+ const { getAllByRole } = createDataGridActions((params) => ({
138
+ ...params,
139
+ enableAddBtn: true,
140
+ addBtnProps: { onClick },
141
+ }));
142
+
143
+ userEvent.click(getAllByRole('button')[0]);
144
+
145
+ expect(onClick).toBeCalledTimes(1);
146
+ });
147
+
148
+ it('clicking on search button runs callback', () => {
149
+ const onClick = jest.fn();
150
+ const { getAllByRole } = createDataGridActions((params) => ({
151
+ ...params,
152
+ enableSearchBtn: true,
153
+ searchBtnProps: { onClick },
154
+ }));
155
+
156
+ userEvent.click(getAllByRole('button')[0]);
157
+
158
+ expect(onClick).toBeCalledTimes(1);
159
+ });
160
+ });
161
+
162
+ describe('ref should work', () => {
163
+ it('should give back the proper data prop, this also checks if the component propagates ...rest properly', () => {
164
+ const ExampleComponent = ({
165
+ propagateRef,
166
+ }: {
167
+ propagateRef: (ref: React.RefObject<HTMLElement>) => void;
168
+ }) => {
169
+ const ref = useRef(null);
170
+
171
+ useEffect(() => {
172
+ propagateRef(ref);
173
+ }, [ref]);
174
+
175
+ return <DataGridActions {...defaultParams} data-ref="testing" enableAddBtn ref={ref} />;
176
+ };
177
+
178
+ const refCheck = (ref: React.RefObject<HTMLElement>) => {
179
+ expect(ref.current).toHaveAttribute('data-ref', 'testing');
180
+ };
181
+
182
+ render(<ExampleComponent propagateRef={refCheck} />);
183
+ });
184
+ });