@onewelcome/react-lib-components 0.1.5-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 (105) hide show
  1. package/dist/Button/IconButton.d.ts +2 -1
  2. package/dist/ContextMenu/ContextMenu.d.ts +2 -3
  3. package/dist/ContextMenu/ContextMenuItem.d.ts +10 -3
  4. package/dist/DataGrid/DataGrid.d.ts +30 -0
  5. package/dist/DataGrid/DataGridActions/DataGridActions.d.ts +14 -0
  6. package/dist/DataGrid/DataGridActions/DataGridColumnsToggle.d.ts +13 -0
  7. package/dist/DataGrid/DataGridBody/DataGridBody.d.ts +16 -0
  8. package/dist/DataGrid/DataGridBody/DataGridCell.d.ts +6 -0
  9. package/dist/DataGrid/DataGridBody/DataGridRow.d.ts +7 -0
  10. package/dist/DataGrid/DataGridHeader/DataGridHeader.d.ts +10 -0
  11. package/dist/DataGrid/DataGridHeader/DataGridHeaderCell.d.ts +10 -0
  12. package/dist/DataGrid/datagrid.interfaces.d.ts +13 -0
  13. package/dist/Form/Checkbox/Checkbox.d.ts +2 -2
  14. package/dist/Form/Select/Option.d.ts +9 -4
  15. package/dist/Form/Select/Select.d.ts +8 -2
  16. package/dist/Form/Toggle/Toggle.d.ts +1 -1
  17. package/dist/Form/Wrapper/SelectWrapper/SelectWrapper.d.ts +1 -1
  18. package/dist/Icon/Icon.d.ts +1 -0
  19. package/dist/Link/Link.d.ts +4 -3
  20. package/dist/Notifications/BaseModal/BaseModal.d.ts +4 -2
  21. package/dist/Notifications/SlideInModal/SlideInModal.d.ts +4 -0
  22. package/dist/StatusIndicator/StatusIndicator.d.ts +9 -0
  23. package/dist/_BaseStyling_/BaseStyling.d.ts +4 -0
  24. package/dist/hooks/useSpacing.d.ts +1 -1
  25. package/dist/hooks/useWrapper.d.ts +1 -1
  26. package/dist/index.d.ts +48 -43
  27. package/dist/react-lib-components.cjs.development.js +2900 -2049
  28. package/dist/react-lib-components.cjs.development.js.map +1 -1
  29. package/dist/react-lib-components.cjs.production.min.js +1 -1
  30. package/dist/react-lib-components.cjs.production.min.js.map +1 -1
  31. package/dist/react-lib-components.esm.js +2897 -2050
  32. package/dist/react-lib-components.esm.js.map +1 -1
  33. package/dist/util/helper.d.ts +1 -1
  34. package/package.json +11 -11
  35. package/src/Button/BaseButton.module.scss +3 -18
  36. package/src/Button/Button.module.scss +4 -311
  37. package/src/Button/IconButton.module.scss +21 -128
  38. package/src/Button/IconButton.test.tsx +24 -0
  39. package/src/Button/IconButton.tsx +6 -1
  40. package/src/ContextMenu/ContextMenu.test.tsx +121 -6
  41. package/src/ContextMenu/ContextMenu.tsx +99 -6
  42. package/src/ContextMenu/ContextMenuItem.tsx +57 -9
  43. package/src/DataGrid/DataGrid.module.scss +21 -0
  44. package/src/DataGrid/DataGrid.test.tsx +276 -0
  45. package/src/DataGrid/DataGrid.tsx +101 -0
  46. package/src/DataGrid/DataGridActions/DataGridActions.module.scss +35 -0
  47. package/src/DataGrid/DataGridActions/DataGridActions.test.tsx +184 -0
  48. package/src/DataGrid/DataGridActions/DataGridActions.tsx +109 -0
  49. package/src/DataGrid/DataGridActions/DataGridColumnsToggle.module.scss +41 -0
  50. package/src/DataGrid/DataGridActions/DataGridColumnsToggle.test.tsx +83 -0
  51. package/src/DataGrid/DataGridActions/DataGridColumnsToggle.tsx +88 -0
  52. package/src/DataGrid/DataGridBody/DataGridBody.module.scss +10 -0
  53. package/src/DataGrid/DataGridBody/DataGridBody.test.tsx +123 -0
  54. package/src/DataGrid/DataGridBody/DataGridBody.tsx +64 -0
  55. package/src/DataGrid/DataGridBody/DataGridCell.module.scss +33 -0
  56. package/src/DataGrid/DataGridBody/DataGridCell.test.tsx +74 -0
  57. package/src/DataGrid/DataGridBody/DataGridCell.tsx +25 -0
  58. package/src/DataGrid/DataGridBody/DataGridRow.module.scss +7 -0
  59. package/src/DataGrid/DataGridBody/DataGridRow.test.tsx +101 -0
  60. package/src/DataGrid/DataGridBody/DataGridRow.tsx +27 -0
  61. package/src/DataGrid/DataGridBody/__snapshots__/DataGridBody.test.tsx.snap +258 -0
  62. package/src/DataGrid/DataGridHeader/DataGridHeader.module.scss +26 -0
  63. package/src/DataGrid/DataGridHeader/DataGridHeader.test.tsx +255 -0
  64. package/src/DataGrid/DataGridHeader/DataGridHeader.tsx +80 -0
  65. package/src/DataGrid/DataGridHeader/DataGridHeaderCell.module.scss +68 -0
  66. package/src/DataGrid/DataGridHeader/DataGridHeaderCell.test.tsx +128 -0
  67. package/src/DataGrid/DataGridHeader/DataGridHeaderCell.tsx +72 -0
  68. package/src/DataGrid/datagrid.interfaces.ts +14 -0
  69. package/src/Form/Checkbox/Checkbox.test.tsx +144 -8
  70. package/src/Form/Checkbox/Checkbox.tsx +8 -8
  71. package/src/Form/Select/Option.tsx +39 -21
  72. package/src/Form/Select/Select.module.scss +1 -1
  73. package/src/Form/Select/Select.test.tsx +235 -56
  74. package/src/Form/Select/Select.tsx +194 -34
  75. package/src/Form/Toggle/Toggle.module.scss +1 -0
  76. package/src/Form/Toggle/Toggle.test.tsx +45 -19
  77. package/src/Form/Toggle/Toggle.tsx +3 -3
  78. package/src/Form/Wrapper/CheckboxWrapper/CheckboxWrapper.test.tsx +1 -1
  79. package/src/Form/Wrapper/SelectWrapper/SelectWrapper.test.tsx +44 -0
  80. package/src/Form/Wrapper/SelectWrapper/SelectWrapper.tsx +4 -2
  81. package/src/Icon/Icon.module.scss +4 -0
  82. package/src/Icon/Icon.tsx +1 -0
  83. package/src/Link/Link.module.scss +20 -0
  84. package/src/Link/Link.test.tsx +33 -0
  85. package/src/Link/Link.tsx +8 -2
  86. package/src/Notifications/BaseModal/BaseModal.module.scss +1 -1
  87. package/src/Notifications/BaseModal/BaseModal.test.tsx +77 -12
  88. package/src/Notifications/BaseModal/BaseModal.tsx +27 -6
  89. package/src/Notifications/Dialog/Dialog.module.scss +1 -1
  90. package/src/Notifications/Dialog/Dialog.tsx +1 -1
  91. package/src/Notifications/SlideInModal/SlideInModal.module.scss +36 -0
  92. package/src/Notifications/SlideInModal/SlideInModal.test.tsx +69 -0
  93. package/src/Notifications/SlideInModal/SlideInModal.tsx +31 -0
  94. package/src/Notifications/Snackbar/SnackbarContainer/SnackbarContainer.module.scss +1 -1
  95. package/src/Notifications/Snackbar/SnackbarItem/SnackbarItem.module.scss +1 -1
  96. package/src/Pagination/Pagination.module.scss +74 -74
  97. package/src/StatusIndicator/StatusIndicator.module.scss +27 -0
  98. package/src/StatusIndicator/StatusIndicator.test.tsx +127 -0
  99. package/src/StatusIndicator/StatusIndicator.tsx +25 -0
  100. package/src/Tiles/Tile.module.scss +1 -1
  101. package/src/Tiles/Tile.test.tsx +4 -4
  102. package/src/_BaseStyling_/BaseStyling.tsx +14 -6
  103. package/src/index.ts +85 -48
  104. package/src/mixins.module.scss +171 -0
  105. package/src/readyclasses.module.scss +0 -30
@@ -0,0 +1,258 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`DataGridBody should render renders without crashing 1`] = `
4
+ Array [
5
+ <tr
6
+ class="row"
7
+ >
8
+ <td
9
+ class="cell "
10
+ >
11
+ <span
12
+ class="typography_style_body "
13
+ >
14
+ Paweł
15
+ </span>
16
+ </td>
17
+ <td
18
+ class="cell "
19
+ >
20
+ <span
21
+ class="typography_style_body "
22
+ >
23
+ Napieracz
24
+ </span>
25
+ </td>
26
+ <td
27
+ class="cell "
28
+ >
29
+ <span
30
+ class="typography_style_body "
31
+ >
32
+ 12.12.1990
33
+ </span>
34
+ </td>
35
+ <td
36
+ class="cell "
37
+ >
38
+ <span
39
+ class="typography_style_body "
40
+ >
41
+ <div
42
+ class="context-menu"
43
+ >
44
+ <button
45
+ aria-controls="consent_menu_Paweł-menu"
46
+ aria-expanded="false"
47
+ aria-haspopup="true"
48
+ class="button icon-button text default button-m"
49
+ id="consent_menu_Paweł"
50
+ tabindex="0"
51
+ type="button"
52
+ >
53
+ <span
54
+ aria-hidden="true"
55
+ class="icon icon-ellipsis-alt "
56
+ data-icon="true"
57
+ />
58
+ <span
59
+ class="sr-only"
60
+ >
61
+ Actions for Paweł
62
+ </span>
63
+ </button>
64
+ </div>
65
+ </span>
66
+ </td>
67
+ </tr>,
68
+ <tr
69
+ class="row"
70
+ >
71
+ <td
72
+ class="cell "
73
+ >
74
+ <span
75
+ class="typography_style_body "
76
+ >
77
+ Michał
78
+ </span>
79
+ </td>
80
+ <td
81
+ class="cell "
82
+ >
83
+ <span
84
+ class="typography_style_body "
85
+ >
86
+ Górski
87
+ </span>
88
+ </td>
89
+ <td
90
+ class="cell "
91
+ >
92
+ <span
93
+ class="typography_style_body "
94
+ >
95
+ 12.12.1994
96
+ </span>
97
+ </td>
98
+ <td
99
+ class="cell "
100
+ >
101
+ <span
102
+ class="typography_style_body "
103
+ >
104
+ <div
105
+ class="context-menu"
106
+ >
107
+ <button
108
+ aria-controls="consent_menu_Michał-menu"
109
+ aria-expanded="false"
110
+ aria-haspopup="true"
111
+ class="button icon-button text default button-m"
112
+ id="consent_menu_Michał"
113
+ tabindex="0"
114
+ type="button"
115
+ >
116
+ <span
117
+ aria-hidden="true"
118
+ class="icon icon-ellipsis-alt "
119
+ data-icon="true"
120
+ />
121
+ <span
122
+ class="sr-only"
123
+ >
124
+ Actions for Michał
125
+ </span>
126
+ </button>
127
+ </div>
128
+ </span>
129
+ </td>
130
+ </tr>,
131
+ <tr
132
+ class="row"
133
+ >
134
+ <td
135
+ class="cell "
136
+ >
137
+ <span
138
+ class="typography_style_body "
139
+ >
140
+ Daniel
141
+ </span>
142
+ </td>
143
+ <td
144
+ class="cell "
145
+ >
146
+ <span
147
+ class="typography_style_body "
148
+ >
149
+ Velden
150
+ </span>
151
+ </td>
152
+ <td
153
+ class="cell "
154
+ >
155
+ <span
156
+ class="typography_style_body "
157
+ >
158
+ 12.12.199x
159
+ </span>
160
+ </td>
161
+ <td
162
+ class="cell "
163
+ >
164
+ <span
165
+ class="typography_style_body "
166
+ >
167
+ <div
168
+ class="context-menu"
169
+ >
170
+ <button
171
+ aria-controls="consent_menu_Daniel-menu"
172
+ aria-expanded="false"
173
+ aria-haspopup="true"
174
+ class="button icon-button text default button-m"
175
+ id="consent_menu_Daniel"
176
+ tabindex="0"
177
+ type="button"
178
+ >
179
+ <span
180
+ aria-hidden="true"
181
+ class="icon icon-ellipsis-alt "
182
+ data-icon="true"
183
+ />
184
+ <span
185
+ class="sr-only"
186
+ >
187
+ Actions for Daniel
188
+ </span>
189
+ </button>
190
+ </div>
191
+ </span>
192
+ </td>
193
+ </tr>,
194
+ <tr
195
+ class="row"
196
+ >
197
+ <td
198
+ class="cell "
199
+ >
200
+ <span
201
+ class="typography_style_body "
202
+ >
203
+ Jasha
204
+ </span>
205
+ </td>
206
+ <td
207
+ class="cell "
208
+ >
209
+ <span
210
+ class="typography_style_body "
211
+ >
212
+ Joachimsthal
213
+ </span>
214
+ </td>
215
+ <td
216
+ class="cell "
217
+ >
218
+ <span
219
+ class="typography_style_body "
220
+ >
221
+ 12.12.198x
222
+ </span>
223
+ </td>
224
+ <td
225
+ class="cell "
226
+ >
227
+ <span
228
+ class="typography_style_body "
229
+ >
230
+ <div
231
+ class="context-menu"
232
+ >
233
+ <button
234
+ aria-controls="consent_menu_Jasha-menu"
235
+ aria-expanded="false"
236
+ aria-haspopup="true"
237
+ class="button icon-button text default button-m"
238
+ id="consent_menu_Jasha"
239
+ tabindex="0"
240
+ type="button"
241
+ >
242
+ <span
243
+ aria-hidden="true"
244
+ class="icon icon-ellipsis-alt "
245
+ data-icon="true"
246
+ />
247
+ <span
248
+ class="sr-only"
249
+ >
250
+ Actions for Jasha
251
+ </span>
252
+ </button>
253
+ </div>
254
+ </span>
255
+ </td>
256
+ </tr>,
257
+ ]
258
+ `;
@@ -0,0 +1,26 @@
1
+ .thead {
2
+ &::after {
3
+ content: '';
4
+ position: absolute;
5
+ top: 2.5rem;
6
+ width: calc(100% - 2rem);
7
+ left: 1rem;
8
+ border-bottom: 1px solid #c3c3c7;
9
+ }
10
+ }
11
+
12
+ .row {
13
+ height: 2.5rem;
14
+ }
15
+
16
+ .context-menu {
17
+ width: 4.125rem;
18
+ box-sizing: border-box;
19
+ }
20
+
21
+ @media only screen and (min-width: 50em) {
22
+ .thead::after {
23
+ width: calc(100% - 2.5rem);
24
+ left: 1.25rem;
25
+ }
26
+ }
@@ -0,0 +1,255 @@
1
+ import React, { useEffect, useRef } from 'react';
2
+ import { DataGridHeader, Props } from './DataGridHeader';
3
+ import { getByRole, render } from '@testing-library/react';
4
+ import userEvent from '@testing-library/user-event';
5
+
6
+ const defaultParams: Props = {
7
+ headers: [
8
+ { name: 'firstName', headline: 'First name' },
9
+ { name: 'lastName', headline: 'Last name' },
10
+ ],
11
+ };
12
+
13
+ const createDataGridHeader = (params?: (defaultParams: Props) => Props) => {
14
+ let parameters: Props = defaultParams;
15
+ if (params) {
16
+ parameters = params(defaultParams);
17
+ }
18
+ const container = document.createElement('table');
19
+ const queries = render(<DataGridHeader {...parameters} data-testid="dataGridHeader" />, {
20
+ container,
21
+ });
22
+ const dataGridHeader = queries.getByTestId('dataGridHeader');
23
+
24
+ return {
25
+ ...queries,
26
+ dataGridHeader,
27
+ };
28
+ };
29
+
30
+ describe('DataGridHeader should render', () => {
31
+ it('renders without crashing', () => {
32
+ const { dataGridHeader, getAllByRole, getByRole } = createDataGridHeader();
33
+
34
+ expect(dataGridHeader).toBeDefined();
35
+ expect(getAllByRole('columnheader')).toHaveLength(2);
36
+ expect(getByRole('cell')).toBeDefined(); //context-menu empty cell
37
+
38
+ expect(dataGridHeader.querySelectorAll('[data-icon]')).toHaveLength(0);
39
+ });
40
+
41
+ it('renders without hidden cell', () => {
42
+ const { dataGridHeader, getByRole } = createDataGridHeader((params) => ({
43
+ ...params,
44
+ headers: [...params.headers].map((header, idx) => ({ ...header, hidden: idx === 1 })),
45
+ }));
46
+
47
+ expect(dataGridHeader).toBeDefined();
48
+ expect(getByRole('columnheader')).toHaveTextContent(defaultParams.headers[0].headline);
49
+ expect(getByRole('cell')).toBeDefined(); //context-menu empty cell
50
+
51
+ expect(dataGridHeader.querySelectorAll('[data-icon]')).toHaveLength(0);
52
+ });
53
+
54
+ it('renders without contenxt-menu empty cell', () => {
55
+ const { dataGridHeader, getAllByRole, queryByRole } = createDataGridHeader((params) => ({
56
+ ...params,
57
+ disableContextMenuColumn: true,
58
+ }));
59
+
60
+ expect(dataGridHeader).toBeDefined();
61
+ expect(getAllByRole('columnheader')).toHaveLength(2);
62
+ expect(queryByRole('cell')).toBeNull(); //context-menu empty cell
63
+ });
64
+
65
+ it('renders headers cells without sorting indicators even when initialSort is provided', () => {
66
+ const { dataGridHeader } = createDataGridHeader((params) => ({
67
+ ...params,
68
+ initialSort: [{ name: params.headers[0].name, direction: 'ASC' }],
69
+ }));
70
+
71
+ expect(dataGridHeader.querySelectorAll('[data-icon]')).toHaveLength(0);
72
+ });
73
+
74
+ it('renders headers cells with sorting indicators', () => {
75
+ const onSort = jest.fn();
76
+ const { dataGridHeader } = createDataGridHeader((params) => ({
77
+ ...params,
78
+ onSort,
79
+ }));
80
+
81
+ const sortingIcons = dataGridHeader.querySelectorAll('[data-icon]');
82
+ expect(sortingIcons).toHaveLength(4);
83
+ sortingIcons.forEach((icon) => expect(icon).not.toHaveClass('hidden'));
84
+ });
85
+
86
+ it('renders headers cells with sorting indicators when initialSort provided with direction ASC', () => {
87
+ const onSort = jest.fn();
88
+ const { dataGridHeader, getAllByRole } = createDataGridHeader((params) => ({
89
+ ...params,
90
+ initialSort: [{ name: params.headers[0].name, direction: 'ASC' }],
91
+ onSort,
92
+ }));
93
+
94
+ expect(dataGridHeader.querySelectorAll('[data-icon]:not(.hidden)')).toHaveLength(3);
95
+ const [firstNameCell, lastNameCell] = getAllByRole('columnheader');
96
+ expect(firstNameCell).toHaveAttribute('aria-sort', 'ascending');
97
+ expect(lastNameCell).not.toHaveAttribute('aria-sort');
98
+ });
99
+
100
+ it('renders headers cells with sorting indicators when initialSort provided with direction DESC', () => {
101
+ const onSort = jest.fn();
102
+ const { dataGridHeader, getAllByRole } = createDataGridHeader((params) => ({
103
+ ...params,
104
+ initialSort: [{ name: params.headers[1].name, direction: 'DESC' }],
105
+ onSort,
106
+ }));
107
+
108
+ expect(dataGridHeader.querySelectorAll('[data-icon]:not(.hidden)')).toHaveLength(3);
109
+ const [firstNameCell, lastNameCell] = getAllByRole('columnheader');
110
+ expect(firstNameCell).not.toHaveAttribute('aria-sort');
111
+ expect(lastNameCell).toHaveAttribute('aria-sort', 'descending');
112
+ });
113
+ });
114
+
115
+ describe('DataGridHeader should be interactive', () => {
116
+ it('clicking on cell call onSort callback', () => {
117
+ const onSortHandler = jest.fn();
118
+ const { getAllByRole } = createDataGridHeader((params) => ({
119
+ ...params,
120
+ onSort: onSortHandler,
121
+ }));
122
+
123
+ const [firstNameCell, lastNameCell] = getAllByRole('columnheader');
124
+ userEvent.click(getByRole(firstNameCell, 'button'));
125
+
126
+ expect(onSortHandler).toBeCalledWith([
127
+ { name: defaultParams.headers[0].name, direction: 'ASC' },
128
+ ]);
129
+ expect(firstNameCell).toHaveAttribute('aria-sort', 'ascending');
130
+ expect(lastNameCell).not.toHaveAttribute('aria-sort');
131
+ });
132
+
133
+ it('clicking on multiple cells call onSort callback with one column selected when single-sorting is enabled', () => {
134
+ const onSortHandler = jest.fn();
135
+ const { getAllByRole } = createDataGridHeader((params) => ({
136
+ ...params,
137
+ onSort: onSortHandler,
138
+ }));
139
+
140
+ const [firstNameCell, lastNameCell] = getAllByRole('columnheader');
141
+
142
+ userEvent.click(getByRole(firstNameCell, 'button'));
143
+ userEvent.click(getByRole(lastNameCell, 'button'));
144
+ expect(onSortHandler).toBeCalledWith([
145
+ { name: defaultParams.headers[0].name, direction: 'ASC' },
146
+ ]);
147
+ expect(onSortHandler).toBeCalledWith([
148
+ { name: defaultParams.headers[1].name, direction: 'ASC' },
149
+ ]);
150
+ expect(firstNameCell).not.toHaveAttribute('aria-sort');
151
+ expect(lastNameCell).toHaveAttribute('aria-sort', 'ascending');
152
+
153
+ userEvent.click(getByRole(lastNameCell, 'button'));
154
+ expect(onSortHandler).toBeCalledWith([
155
+ { name: defaultParams.headers[1].name, direction: 'DESC' },
156
+ ]);
157
+ expect(firstNameCell).not.toHaveAttribute('aria-sort');
158
+ expect(lastNameCell).toHaveAttribute('aria-sort', 'descending');
159
+
160
+ userEvent.click(getByRole(firstNameCell, 'button'));
161
+ expect(onSortHandler).toBeCalledWith([
162
+ { name: defaultParams.headers[0].name, direction: 'ASC' },
163
+ ]);
164
+ expect(firstNameCell).toHaveAttribute('aria-sort', 'ascending');
165
+ expect(lastNameCell).not.toHaveAttribute('aria-sort');
166
+
167
+ userEvent.click(getByRole(firstNameCell, 'button'));
168
+ expect(onSortHandler).toBeCalledWith([
169
+ { name: defaultParams.headers[1].name, direction: 'DESC' },
170
+ ]);
171
+ expect(firstNameCell).toHaveAttribute('aria-sort', 'descending');
172
+ expect(lastNameCell).not.toHaveAttribute('aria-sort');
173
+
174
+ userEvent.click(getByRole(firstNameCell, 'button'));
175
+ expect(onSortHandler).toBeCalledWith([]);
176
+ expect(firstNameCell).not.toHaveAttribute('aria-sort');
177
+ expect(lastNameCell).not.toHaveAttribute('aria-sort');
178
+ });
179
+
180
+ it('clicking on multiple cells call onSort callback with multiple columns selected when multi-sorting is enabled', () => {
181
+ const onSortHandler = jest.fn();
182
+ const { getAllByRole } = createDataGridHeader((params) => ({
183
+ ...params,
184
+ enableMultiSorting: true,
185
+ onSort: onSortHandler,
186
+ }));
187
+
188
+ const [firstNameCell, lastNameCell] = getAllByRole('columnheader');
189
+
190
+ userEvent.click(getByRole(firstNameCell, 'button'));
191
+ userEvent.click(getByRole(lastNameCell, 'button'));
192
+ expect(onSortHandler).toBeCalledWith([
193
+ { name: defaultParams.headers[0].name, direction: 'ASC' },
194
+ ]);
195
+ expect(onSortHandler).toBeCalledWith([
196
+ { name: defaultParams.headers[1].name, direction: 'ASC' },
197
+ { name: defaultParams.headers[0].name, direction: 'ASC' },
198
+ ]);
199
+ expect(firstNameCell).toHaveAttribute('aria-sort', 'ascending');
200
+ expect(lastNameCell).toHaveAttribute('aria-sort', 'ascending');
201
+
202
+ userEvent.click(getByRole(lastNameCell, 'button'));
203
+ expect(onSortHandler).toBeCalledWith([
204
+ { name: defaultParams.headers[1].name, direction: 'DESC' },
205
+ { name: defaultParams.headers[0].name, direction: 'ASC' },
206
+ ]);
207
+ expect(firstNameCell).toHaveAttribute('aria-sort', 'ascending');
208
+ expect(lastNameCell).toHaveAttribute('aria-sort', 'descending');
209
+
210
+ userEvent.click(getByRole(firstNameCell, 'button'));
211
+ expect(onSortHandler).toBeCalledWith([
212
+ { name: defaultParams.headers[0].name, direction: 'DESC' },
213
+ { name: defaultParams.headers[1].name, direction: 'DESC' },
214
+ ]);
215
+ expect(firstNameCell).toHaveAttribute('aria-sort', 'descending');
216
+ expect(lastNameCell).toHaveAttribute('aria-sort', 'descending');
217
+
218
+ userEvent.click(getByRole(firstNameCell, 'button'));
219
+ expect(onSortHandler).toBeCalledWith([
220
+ { name: defaultParams.headers[1].name, direction: 'DESC' },
221
+ ]);
222
+ expect(firstNameCell).not.toHaveAttribute('aria-sort');
223
+ expect(lastNameCell).toHaveAttribute('aria-sort', 'descending');
224
+
225
+ userEvent.click(getByRole(lastNameCell, 'button'));
226
+ expect(onSortHandler).toBeCalledWith([]);
227
+ expect(firstNameCell).not.toHaveAttribute('aria-sort');
228
+ expect(lastNameCell).not.toHaveAttribute('aria-sort');
229
+ });
230
+ });
231
+
232
+ describe('ref should work', () => {
233
+ it('should give back the proper data prop, this also checks if the component propagates ...rest properly', () => {
234
+ const ExampleComponent = ({
235
+ propagateRef,
236
+ }: {
237
+ propagateRef: (ref: React.RefObject<HTMLElement>) => void;
238
+ }) => {
239
+ const ref = useRef(null);
240
+
241
+ useEffect(() => {
242
+ propagateRef(ref);
243
+ }, [ref]);
244
+
245
+ return <DataGridHeader {...defaultParams} data-ref="testing" ref={ref} />;
246
+ };
247
+
248
+ const refCheck = (ref: React.RefObject<HTMLElement>) => {
249
+ expect(ref.current).toHaveAttribute('data-ref', 'testing');
250
+ };
251
+
252
+ const container = document.createElement('table');
253
+ render(<ExampleComponent propagateRef={refCheck} />, { container });
254
+ });
255
+ });
@@ -0,0 +1,80 @@
1
+ import React, { ComponentPropsWithRef, useEffect, useState } from 'react';
2
+ import { ColumnName, Direction, HeaderCell, OnSortFunction, Sort } from '../datagrid.interfaces';
3
+ import { DataGridHeaderCell } from './DataGridHeaderCell';
4
+ import classes from './DataGridHeader.module.scss';
5
+
6
+ export interface Props extends ComponentPropsWithRef<'thead'> {
7
+ headers: HeaderCell[];
8
+ initialSort?: Sort;
9
+ onSort?: OnSortFunction;
10
+ disableContextMenuColumn?: boolean;
11
+ enableMultiSorting?: boolean;
12
+ }
13
+
14
+ const sortingStates = [undefined, 'ASC', 'DESC'] as (Direction | undefined)[];
15
+
16
+ export const DataGridHeader = React.forwardRef<HTMLTableSectionElement, Props>(
17
+ (
18
+ { initialSort, onSort, headers, disableContextMenuColumn, enableMultiSorting, ...rest }: Props,
19
+ ref
20
+ ) => {
21
+ const [sortList, setSortList] = useState(initialSort || []);
22
+
23
+ useEffect(() => {
24
+ setSortList(initialSort || []);
25
+ }, [initialSort]);
26
+
27
+ const calculateNextSortState = (direction?: Direction) => {
28
+ const currentDirectionIdx = sortingStates.findIndex((item) => item === direction);
29
+ return sortingStates[currentDirectionIdx + (1 % sortingStates.length)];
30
+ };
31
+
32
+ /**
33
+ * The sortList contains sorting columns objects. The order of those objects determinates priorities of sorting.
34
+ * Last modified sorting column has the highest priority.
35
+ */
36
+ const updateSortList = (name: ColumnName): Sort => {
37
+ const current = sortList.find((item) => item.name === name);
38
+ const restSortList = enableMultiSorting ? sortList.filter((item) => item.name !== name) : [];
39
+ const newSortDirection = calculateNextSortState(current?.direction);
40
+ return newSortDirection
41
+ ? [{ name, direction: newSortDirection }, ...restSortList]
42
+ : restSortList;
43
+ };
44
+
45
+ const wrapOnSort = (name: ColumnName) => {
46
+ const newSort = updateSortList(name);
47
+ onSort && onSort(newSort);
48
+ setSortList(newSort);
49
+ };
50
+
51
+ const headerCells = headers.map((header) => {
52
+ if (header.hidden) {
53
+ return null;
54
+ }
55
+
56
+ const sort = sortList.find((item) => item.name === header.name);
57
+ return (
58
+ <DataGridHeaderCell
59
+ key={header.name}
60
+ name={header.name}
61
+ headline={header.headline}
62
+ disableSorting={header.disableSorting || !onSort}
63
+ onSort={wrapOnSort}
64
+ activeSortDirection={sort?.direction}
65
+ />
66
+ );
67
+ });
68
+
69
+ return (
70
+ <thead {...rest} ref={ref} className={classes['thead']}>
71
+ <tr className={classes['row']}>
72
+ {headerCells}
73
+ {!disableContextMenuColumn && (
74
+ <td aria-label="context menu" className={classes['context-menu']}></td>
75
+ )}
76
+ </tr>
77
+ </thead>
78
+ );
79
+ }
80
+ );
@@ -0,0 +1,68 @@
1
+ .header-cell {
2
+ font-weight: normal;
3
+ text-align: left;
4
+ padding: 0 0.625rem;
5
+
6
+ &:first-child {
7
+ padding-left: 1rem;
8
+ }
9
+
10
+ &:last-child {
11
+ padding-right: 1rem;
12
+ }
13
+
14
+ & > * {
15
+ display: inline-flex;
16
+ }
17
+
18
+ & > button {
19
+ padding: 0;
20
+ margin: 0;
21
+ border: 0;
22
+ background: none;
23
+ cursor: pointer;
24
+ }
25
+ }
26
+
27
+ .headline {
28
+ font-family: var(--font-family);
29
+ font-size: var(--font-size);
30
+ line-height: 1.5;
31
+ font-weight: 700;
32
+ color: var(--default);
33
+ }
34
+
35
+ .sorting-indicator-container {
36
+ display: flex;
37
+ flex-direction: column;
38
+ padding-left: 0.5rem;
39
+ justify-content: center;
40
+
41
+ & > * {
42
+ font-size: 0.625rem;
43
+ }
44
+
45
+ .indicator {
46
+ color: var(--greyed-out);
47
+
48
+ &.active {
49
+ color: var(--color-primary);
50
+ }
51
+
52
+ &.hidden {
53
+ visibility: hidden;
54
+ }
55
+ }
56
+ }
57
+
58
+ @media only screen and (min-width: 50em) {
59
+ .header-cell {
60
+ &:first-child {
61
+ padding-left: 1.25rem;
62
+ }
63
+
64
+ &:last-child {
65
+ padding-right: 1.25rem;
66
+ }
67
+ }
68
+ }