@onewelcome/react-lib-components 0.1.6-alpha → 0.1.9-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 (102) 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 +32 -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 +17 -0
  8. package/dist/DataGrid/DataGridBody/DataGridCell.d.ts +10 -0
  9. package/dist/DataGrid/DataGridBody/DataGridRow.d.ts +9 -0
  10. package/dist/DataGrid/DataGridHeader/DataGridHeader.d.ts +11 -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/index.d.ts +48 -43
  25. package/dist/react-lib-components.cjs.development.js +3097 -2157
  26. package/dist/react-lib-components.cjs.development.js.map +1 -1
  27. package/dist/react-lib-components.cjs.production.min.js +1 -1
  28. package/dist/react-lib-components.cjs.production.min.js.map +1 -1
  29. package/dist/react-lib-components.esm.js +3094 -2158
  30. package/dist/react-lib-components.esm.js.map +1 -1
  31. package/package.json +11 -13
  32. package/src/Button/BaseButton.module.scss +3 -18
  33. package/src/Button/Button.module.scss +4 -311
  34. package/src/Button/IconButton.module.scss +21 -128
  35. package/src/Button/IconButton.test.tsx +24 -0
  36. package/src/Button/IconButton.tsx +6 -1
  37. package/src/ContextMenu/ContextMenu.test.tsx +121 -6
  38. package/src/ContextMenu/ContextMenu.tsx +99 -6
  39. package/src/ContextMenu/ContextMenuItem.tsx +57 -9
  40. package/src/DataGrid/DataGrid.module.scss +25 -0
  41. package/src/DataGrid/DataGrid.test.tsx +421 -0
  42. package/src/DataGrid/DataGrid.tsx +157 -0
  43. package/src/DataGrid/DataGridActions/DataGridActions.module.scss +35 -0
  44. package/src/DataGrid/DataGridActions/DataGridActions.test.tsx +184 -0
  45. package/src/DataGrid/DataGridActions/DataGridActions.tsx +109 -0
  46. package/src/DataGrid/DataGridActions/DataGridColumnsToggle.module.scss +41 -0
  47. package/src/DataGrid/DataGridActions/DataGridColumnsToggle.test.tsx +83 -0
  48. package/src/DataGrid/DataGridActions/DataGridColumnsToggle.tsx +88 -0
  49. package/src/DataGrid/DataGridBody/DataGridBody.module.scss +10 -0
  50. package/src/DataGrid/DataGridBody/DataGridBody.test.tsx +123 -0
  51. package/src/DataGrid/DataGridBody/DataGridBody.tsx +80 -0
  52. package/src/DataGrid/DataGridBody/DataGridCell.module.scss +33 -0
  53. package/src/DataGrid/DataGridBody/DataGridCell.test.tsx +74 -0
  54. package/src/DataGrid/DataGridBody/DataGridCell.tsx +58 -0
  55. package/src/DataGrid/DataGridBody/DataGridRow.module.scss +7 -0
  56. package/src/DataGrid/DataGridBody/DataGridRow.test.tsx +101 -0
  57. package/src/DataGrid/DataGridBody/DataGridRow.tsx +42 -0
  58. package/src/DataGrid/DataGridBody/__snapshots__/DataGridBody.test.tsx.snap +258 -0
  59. package/src/DataGrid/DataGridHeader/DataGridHeader.module.scss +26 -0
  60. package/src/DataGrid/DataGridHeader/DataGridHeader.test.tsx +255 -0
  61. package/src/DataGrid/DataGridHeader/DataGridHeader.tsx +103 -0
  62. package/src/DataGrid/DataGridHeader/DataGridHeaderCell.module.scss +68 -0
  63. package/src/DataGrid/DataGridHeader/DataGridHeaderCell.test.tsx +128 -0
  64. package/src/DataGrid/DataGridHeader/DataGridHeaderCell.tsx +72 -0
  65. package/src/DataGrid/datagrid.interfaces.ts +14 -0
  66. package/src/Form/Checkbox/Checkbox.test.tsx +144 -8
  67. package/src/Form/Checkbox/Checkbox.tsx +8 -8
  68. package/src/Form/Select/Option.tsx +39 -21
  69. package/src/Form/Select/Select.module.scss +1 -1
  70. package/src/Form/Select/Select.test.tsx +235 -56
  71. package/src/Form/Select/Select.tsx +194 -34
  72. package/src/Form/Toggle/Toggle.module.scss +1 -0
  73. package/src/Form/Toggle/Toggle.tsx +1 -1
  74. package/src/Form/Wrapper/CheckboxWrapper/CheckboxWrapper.test.tsx +1 -1
  75. package/src/Form/Wrapper/SelectWrapper/SelectWrapper.test.tsx +44 -0
  76. package/src/Form/Wrapper/SelectWrapper/SelectWrapper.tsx +4 -2
  77. package/src/Form/Wrapper/Wrapper/Wrapper.module.scss +1 -0
  78. package/src/Icon/Icon.module.scss +4 -0
  79. package/src/Icon/Icon.tsx +1 -0
  80. package/src/Link/Link.module.scss +20 -0
  81. package/src/Link/Link.test.tsx +33 -0
  82. package/src/Link/Link.tsx +8 -2
  83. package/src/Notifications/BaseModal/BaseModal.module.scss +1 -1
  84. package/src/Notifications/BaseModal/BaseModal.test.tsx +77 -12
  85. package/src/Notifications/BaseModal/BaseModal.tsx +27 -6
  86. package/src/Notifications/Dialog/Dialog.module.scss +1 -1
  87. package/src/Notifications/Dialog/Dialog.tsx +1 -1
  88. package/src/Notifications/SlideInModal/SlideInModal.module.scss +36 -0
  89. package/src/Notifications/SlideInModal/SlideInModal.test.tsx +69 -0
  90. package/src/Notifications/SlideInModal/SlideInModal.tsx +31 -0
  91. package/src/Notifications/Snackbar/SnackbarContainer/SnackbarContainer.module.scss +1 -1
  92. package/src/Notifications/Snackbar/SnackbarItem/SnackbarItem.module.scss +1 -1
  93. package/src/Pagination/Pagination.module.scss +74 -74
  94. package/src/StatusIndicator/StatusIndicator.module.scss +27 -0
  95. package/src/StatusIndicator/StatusIndicator.test.tsx +127 -0
  96. package/src/StatusIndicator/StatusIndicator.tsx +25 -0
  97. package/src/Tiles/Tile.module.scss +1 -1
  98. package/src/Tiles/Tile.test.tsx +4 -4
  99. package/src/_BaseStyling_/BaseStyling.tsx +14 -6
  100. package/src/index.ts +85 -48
  101. package/src/mixins.module.scss +171 -0
  102. package/src/readyclasses.module.scss +0 -30
@@ -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,103 @@
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
+ spacing?: React.CSSProperties;
13
+ }
14
+
15
+ const sortingStates = [undefined, 'ASC', 'DESC'] as (Direction | undefined)[];
16
+
17
+ export const DataGridHeader = React.forwardRef<HTMLTableSectionElement, Props>(
18
+ (
19
+ {
20
+ initialSort,
21
+ onSort,
22
+ headers,
23
+ disableContextMenuColumn,
24
+ enableMultiSorting,
25
+ spacing,
26
+ ...rest
27
+ }: Props,
28
+ ref
29
+ ) => {
30
+ const [sortList, setSortList] = useState(initialSort || []);
31
+
32
+ useEffect(() => {
33
+ setSortList(initialSort || []);
34
+ }, [initialSort]);
35
+
36
+ const calculateNextSortState = (direction?: Direction) => {
37
+ const currentDirectionIdx = sortingStates.findIndex((item) => item === direction);
38
+ return sortingStates[currentDirectionIdx + (1 % sortingStates.length)];
39
+ };
40
+
41
+ /**
42
+ * The sortList contains sorting columns objects. The order of those objects determinates priorities of sorting.
43
+ * Last modified sorting column has the highest priority.
44
+ */
45
+ const updateSortList = (name: ColumnName): Sort => {
46
+ const current = sortList.find((item) => item.name === name);
47
+ const restSortList = enableMultiSorting ? sortList.filter((item) => item.name !== name) : [];
48
+ const newSortDirection = calculateNextSortState(current?.direction);
49
+ return newSortDirection
50
+ ? [{ name, direction: newSortDirection }, ...restSortList]
51
+ : restSortList;
52
+ };
53
+
54
+ const wrapOnSort = (name: ColumnName) => {
55
+ const newSort = updateSortList(name);
56
+ onSort && onSort(newSort);
57
+ setSortList(newSort);
58
+ };
59
+
60
+ const headerCells = headers.map((header, index) => {
61
+ if (header.hidden) {
62
+ return null;
63
+ }
64
+
65
+ let headerStyle: React.CSSProperties = {};
66
+
67
+ if (index === 0) {
68
+ headerStyle.paddingLeft = spacing?.paddingLeft;
69
+ }
70
+ if (index === headers.length - 1 && disableContextMenuColumn) {
71
+ headerStyle.paddingRight = spacing?.paddingRight;
72
+ }
73
+
74
+ const sort = sortList.find((item) => item.name === header.name);
75
+ return (
76
+ <DataGridHeaderCell
77
+ key={header.name}
78
+ name={header.name}
79
+ headline={header.headline}
80
+ disableSorting={header.disableSorting || !onSort}
81
+ onSort={wrapOnSort}
82
+ activeSortDirection={sort?.direction}
83
+ style={headerStyle}
84
+ />
85
+ );
86
+ });
87
+
88
+ return (
89
+ <thead {...rest} ref={ref} className={classes['thead']}>
90
+ <tr className={classes['row']}>
91
+ {headerCells}
92
+ {!disableContextMenuColumn && (
93
+ <td
94
+ style={{ paddingRight: spacing?.paddingRight }}
95
+ aria-label="context menu"
96
+ className={classes['context-menu']}
97
+ ></td>
98
+ )}
99
+ </tr>
100
+ </thead>
101
+ );
102
+ }
103
+ );
@@ -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
+ }
@@ -0,0 +1,128 @@
1
+ import React, { useEffect, useRef } from 'react';
2
+ import { DataGridHeaderCell, Props } from './DataGridHeaderCell';
3
+ import { render } from '@testing-library/react';
4
+ import userEvent from '@testing-library/user-event';
5
+
6
+ const defaultParams: Props = { name: 'name-123', headline: 'Test' };
7
+
8
+ const createDataGridHeaderCell = (params?: (defaultParams: Props) => Props) => {
9
+ let parameters: Props = defaultParams;
10
+ if (params) {
11
+ parameters = params(defaultParams);
12
+ }
13
+ const container = document.createElement('tr');
14
+ const queries = render(<DataGridHeaderCell {...parameters} data-testid="dataGridHeaderCell" />, {
15
+ container,
16
+ });
17
+ const dataGridHeaderCell = queries.getByTestId('dataGridHeaderCell');
18
+
19
+ return {
20
+ ...queries,
21
+ dataGridHeaderCell,
22
+ };
23
+ };
24
+
25
+ describe('DataGridHeaderCell should render', () => {
26
+ it('renders without crashing', () => {
27
+ const { dataGridHeaderCell, queryByRole } = createDataGridHeaderCell();
28
+
29
+ expect(dataGridHeaderCell).toBeDefined();
30
+ expect(dataGridHeaderCell).toHaveTextContent(defaultParams.headline);
31
+ expect(queryByRole('button')).toBeDefined();
32
+ expect(dataGridHeaderCell.querySelectorAll('[data-icon]')).toHaveLength(2);
33
+ });
34
+
35
+ it('renders without sorting indicators (icons)', () => {
36
+ const { dataGridHeaderCell } = createDataGridHeaderCell((params) => ({
37
+ ...params,
38
+ disableSorting: true,
39
+ }));
40
+
41
+ expect(dataGridHeaderCell.querySelector('[data-icon]')).toBeNull();
42
+ });
43
+
44
+ it('renders DESC and ASC sorting indicators (icons) when sorting is enabled and current sorting is not defined', () => {
45
+ const { dataGridHeaderCell } = createDataGridHeaderCell();
46
+
47
+ expect(dataGridHeaderCell).toBeDefined();
48
+ const sortingIcons = dataGridHeaderCell.querySelectorAll('[data-icon]');
49
+ expect(sortingIcons).toHaveLength(2);
50
+ sortingIcons.forEach((icon) => {
51
+ expect(icon).toHaveClass('indicator');
52
+ expect(icon).not.toHaveClass('active');
53
+ expect(icon).not.toHaveClass('hidden');
54
+ });
55
+ });
56
+
57
+ it('renders ASC sorting indicator (icon) when sorting is enabled and current sorting is ASC', () => {
58
+ const { dataGridHeaderCell } = createDataGridHeaderCell((params) => ({
59
+ ...params,
60
+ activeSortDirection: 'ASC',
61
+ }));
62
+
63
+ expect(dataGridHeaderCell).toBeDefined();
64
+ const sortingIcons = dataGridHeaderCell.querySelectorAll('[data-icon]');
65
+ expect(sortingIcons).toHaveLength(2);
66
+ sortingIcons.forEach((icon) => expect(icon).toHaveClass('indicator'));
67
+ expect(sortingIcons[0]).toHaveClass('indicator', 'active', 'icon-triangle-up');
68
+ expect(sortingIcons[0]).not.toHaveClass('hidden');
69
+ expect(sortingIcons[1]).toHaveClass('indicator', 'hidden', 'icon-triangle-down');
70
+ expect(sortingIcons[1]).not.toHaveClass('active');
71
+ });
72
+ });
73
+
74
+ it('renders DESC sorting indicator (icon) when sorting is enabled and current sorting is DESC', () => {
75
+ const { dataGridHeaderCell } = createDataGridHeaderCell((params) => ({
76
+ ...params,
77
+ activeSortDirection: 'DESC',
78
+ }));
79
+
80
+ expect(dataGridHeaderCell).toBeDefined();
81
+ const sortingIcons = dataGridHeaderCell.querySelectorAll('[data-icon]');
82
+ expect(sortingIcons).toHaveLength(2);
83
+ sortingIcons.forEach((icon) => expect(icon).toHaveClass('indicator'));
84
+ expect(sortingIcons[0]).toHaveClass('indicator', 'hidden', 'icon-triangle-up');
85
+ expect(sortingIcons[0]).not.toHaveClass('active');
86
+ expect(sortingIcons[1]).toHaveClass('indicator', 'active', 'icon-triangle-down');
87
+ expect(sortingIcons[1]).not.toHaveClass('hidden');
88
+ });
89
+
90
+ describe('DataGridHeaderCell should be interactive', () => {
91
+ it('clicking on cell calls onSort callback', () => {
92
+ const onSortHandler = jest.fn();
93
+ const { getByRole } = createDataGridHeaderCell((params) => ({
94
+ ...params,
95
+ disableSorting: false,
96
+ onSort: onSortHandler,
97
+ }));
98
+
99
+ userEvent.click(getByRole('button'));
100
+
101
+ expect(onSortHandler).toBeCalledWith(defaultParams.name);
102
+ });
103
+ });
104
+
105
+ describe('ref should work', () => {
106
+ it('should give back the proper data prop, this also checks if the component propagates ...rest properly', () => {
107
+ const ExampleComponent = ({
108
+ propagateRef,
109
+ }: {
110
+ propagateRef: (ref: React.RefObject<HTMLElement>) => void;
111
+ }) => {
112
+ const ref = useRef(null);
113
+
114
+ useEffect(() => {
115
+ propagateRef(ref);
116
+ }, [ref]);
117
+
118
+ return <DataGridHeaderCell {...defaultParams} data-ref="testing" ref={ref} />;
119
+ };
120
+
121
+ const refCheck = (ref: React.RefObject<HTMLElement>) => {
122
+ expect(ref.current).toHaveAttribute('data-ref', 'testing');
123
+ };
124
+
125
+ const container = document.createElement('tr');
126
+ render(<ExampleComponent propagateRef={refCheck} />, { container });
127
+ });
128
+ });
@@ -0,0 +1,72 @@
1
+ import React, { ComponentPropsWithRef, Fragment } from 'react';
2
+ import { Icon, Icons } from '../../Icon/Icon';
3
+ import { ColumnName, Direction } from '../datagrid.interfaces';
4
+ import classes from './DataGridHeaderCell.module.scss';
5
+
6
+ export interface Props extends ComponentPropsWithRef<'th'> {
7
+ headline: string;
8
+ name: ColumnName;
9
+ disableSorting?: boolean;
10
+ activeSortDirection?: Direction;
11
+ onSort?: (name: ColumnName) => void;
12
+ }
13
+
14
+ const ariaSortMapping = {
15
+ ASC: 'ascending',
16
+ DESC: 'descending',
17
+ } as const;
18
+
19
+ export const DataGridHeaderCell = React.forwardRef<HTMLTableCellElement, Props>(
20
+ ({ headline, name, disableSorting, activeSortDirection, onSort, ...rest }: Props, ref) => {
21
+ const onCellClick = () => {
22
+ onSort && onSort(name);
23
+ };
24
+
25
+ const sortingIndicator = () => {
26
+ const getSortingIndicatorClasses = (direction: Direction) => {
27
+ const sortingIndicatorClasses = [classes['indicator']];
28
+ activeSortDirection &&
29
+ sortingIndicatorClasses.push(
30
+ activeSortDirection === direction ? classes['active'] : classes['hidden']
31
+ );
32
+ return sortingIndicatorClasses;
33
+ };
34
+
35
+ return (
36
+ <Fragment>
37
+ <Icon className={getSortingIndicatorClasses('ASC').join(' ')} icon={Icons.TriangleUp} />
38
+ <Icon
39
+ className={getSortingIndicatorClasses('DESC').join(' ')}
40
+ icon={Icons.TriangleDown}
41
+ />
42
+ </Fragment>
43
+ );
44
+ };
45
+
46
+ const innerContent = (
47
+ <Fragment>
48
+ <span className={classes['headline']}>{headline}</span>
49
+ {!disableSorting && (
50
+ <div className={classes['sorting-indicator-container']}>{sortingIndicator()}</div>
51
+ )}
52
+ </Fragment>
53
+ );
54
+
55
+ return (
56
+ <th
57
+ {...rest}
58
+ ref={ref}
59
+ className={classes['header-cell']}
60
+ aria-sort={activeSortDirection && ariaSortMapping[activeSortDirection]}
61
+ >
62
+ {disableSorting ? (
63
+ <div key={name}>{innerContent}</div>
64
+ ) : (
65
+ <button key={name} onClick={onCellClick}>
66
+ {innerContent}
67
+ </button>
68
+ )}
69
+ </th>
70
+ );
71
+ }
72
+ );
@@ -0,0 +1,14 @@
1
+ export type ColumnName = string;
2
+ export type Direction = 'ASC' | 'DESC';
3
+ export type Sort = {
4
+ name: ColumnName;
5
+ direction: Direction;
6
+ }[];
7
+ export type OnSortFunction = (sort: Sort) => void;
8
+
9
+ export interface HeaderCell {
10
+ name: ColumnName;
11
+ headline: string;
12
+ disableSorting?: boolean;
13
+ hidden?: boolean;
14
+ }