@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.
- package/dist/Button/IconButton.d.ts +2 -1
- package/dist/ContextMenu/ContextMenu.d.ts +2 -3
- package/dist/ContextMenu/ContextMenuItem.d.ts +10 -3
- package/dist/DataGrid/DataGrid.d.ts +32 -0
- package/dist/DataGrid/DataGridActions/DataGridActions.d.ts +14 -0
- package/dist/DataGrid/DataGridActions/DataGridColumnsToggle.d.ts +13 -0
- package/dist/DataGrid/DataGridBody/DataGridBody.d.ts +17 -0
- package/dist/DataGrid/DataGridBody/DataGridCell.d.ts +10 -0
- package/dist/DataGrid/DataGridBody/DataGridRow.d.ts +9 -0
- package/dist/DataGrid/DataGridHeader/DataGridHeader.d.ts +11 -0
- package/dist/DataGrid/DataGridHeader/DataGridHeaderCell.d.ts +10 -0
- package/dist/DataGrid/datagrid.interfaces.d.ts +13 -0
- package/dist/Form/Checkbox/Checkbox.d.ts +2 -2
- package/dist/Form/Select/Option.d.ts +9 -4
- package/dist/Form/Select/Select.d.ts +8 -2
- package/dist/Form/Toggle/Toggle.d.ts +1 -1
- package/dist/Form/Wrapper/SelectWrapper/SelectWrapper.d.ts +1 -1
- package/dist/Icon/Icon.d.ts +1 -0
- package/dist/Link/Link.d.ts +4 -3
- package/dist/Notifications/BaseModal/BaseModal.d.ts +4 -2
- package/dist/Notifications/SlideInModal/SlideInModal.d.ts +4 -0
- package/dist/StatusIndicator/StatusIndicator.d.ts +9 -0
- package/dist/_BaseStyling_/BaseStyling.d.ts +4 -0
- package/dist/index.d.ts +48 -43
- package/dist/react-lib-components.cjs.development.js +3097 -2157
- package/dist/react-lib-components.cjs.development.js.map +1 -1
- package/dist/react-lib-components.cjs.production.min.js +1 -1
- package/dist/react-lib-components.cjs.production.min.js.map +1 -1
- package/dist/react-lib-components.esm.js +3094 -2158
- package/dist/react-lib-components.esm.js.map +1 -1
- package/package.json +11 -13
- package/src/Button/BaseButton.module.scss +3 -18
- package/src/Button/Button.module.scss +4 -311
- package/src/Button/IconButton.module.scss +21 -128
- package/src/Button/IconButton.test.tsx +24 -0
- package/src/Button/IconButton.tsx +6 -1
- package/src/ContextMenu/ContextMenu.test.tsx +121 -6
- package/src/ContextMenu/ContextMenu.tsx +99 -6
- package/src/ContextMenu/ContextMenuItem.tsx +57 -9
- package/src/DataGrid/DataGrid.module.scss +25 -0
- package/src/DataGrid/DataGrid.test.tsx +421 -0
- package/src/DataGrid/DataGrid.tsx +157 -0
- package/src/DataGrid/DataGridActions/DataGridActions.module.scss +35 -0
- package/src/DataGrid/DataGridActions/DataGridActions.test.tsx +184 -0
- package/src/DataGrid/DataGridActions/DataGridActions.tsx +109 -0
- package/src/DataGrid/DataGridActions/DataGridColumnsToggle.module.scss +41 -0
- package/src/DataGrid/DataGridActions/DataGridColumnsToggle.test.tsx +83 -0
- package/src/DataGrid/DataGridActions/DataGridColumnsToggle.tsx +88 -0
- package/src/DataGrid/DataGridBody/DataGridBody.module.scss +10 -0
- package/src/DataGrid/DataGridBody/DataGridBody.test.tsx +123 -0
- package/src/DataGrid/DataGridBody/DataGridBody.tsx +80 -0
- package/src/DataGrid/DataGridBody/DataGridCell.module.scss +33 -0
- package/src/DataGrid/DataGridBody/DataGridCell.test.tsx +74 -0
- package/src/DataGrid/DataGridBody/DataGridCell.tsx +58 -0
- package/src/DataGrid/DataGridBody/DataGridRow.module.scss +7 -0
- package/src/DataGrid/DataGridBody/DataGridRow.test.tsx +101 -0
- package/src/DataGrid/DataGridBody/DataGridRow.tsx +42 -0
- package/src/DataGrid/DataGridBody/__snapshots__/DataGridBody.test.tsx.snap +258 -0
- package/src/DataGrid/DataGridHeader/DataGridHeader.module.scss +26 -0
- package/src/DataGrid/DataGridHeader/DataGridHeader.test.tsx +255 -0
- package/src/DataGrid/DataGridHeader/DataGridHeader.tsx +103 -0
- package/src/DataGrid/DataGridHeader/DataGridHeaderCell.module.scss +68 -0
- package/src/DataGrid/DataGridHeader/DataGridHeaderCell.test.tsx +128 -0
- package/src/DataGrid/DataGridHeader/DataGridHeaderCell.tsx +72 -0
- package/src/DataGrid/datagrid.interfaces.ts +14 -0
- package/src/Form/Checkbox/Checkbox.test.tsx +144 -8
- package/src/Form/Checkbox/Checkbox.tsx +8 -8
- package/src/Form/Select/Option.tsx +39 -21
- package/src/Form/Select/Select.module.scss +1 -1
- package/src/Form/Select/Select.test.tsx +235 -56
- package/src/Form/Select/Select.tsx +194 -34
- package/src/Form/Toggle/Toggle.module.scss +1 -0
- package/src/Form/Toggle/Toggle.tsx +1 -1
- package/src/Form/Wrapper/CheckboxWrapper/CheckboxWrapper.test.tsx +1 -1
- package/src/Form/Wrapper/SelectWrapper/SelectWrapper.test.tsx +44 -0
- package/src/Form/Wrapper/SelectWrapper/SelectWrapper.tsx +4 -2
- package/src/Form/Wrapper/Wrapper/Wrapper.module.scss +1 -0
- package/src/Icon/Icon.module.scss +4 -0
- package/src/Icon/Icon.tsx +1 -0
- package/src/Link/Link.module.scss +20 -0
- package/src/Link/Link.test.tsx +33 -0
- package/src/Link/Link.tsx +8 -2
- package/src/Notifications/BaseModal/BaseModal.module.scss +1 -1
- package/src/Notifications/BaseModal/BaseModal.test.tsx +77 -12
- package/src/Notifications/BaseModal/BaseModal.tsx +27 -6
- package/src/Notifications/Dialog/Dialog.module.scss +1 -1
- package/src/Notifications/Dialog/Dialog.tsx +1 -1
- package/src/Notifications/SlideInModal/SlideInModal.module.scss +36 -0
- package/src/Notifications/SlideInModal/SlideInModal.test.tsx +69 -0
- package/src/Notifications/SlideInModal/SlideInModal.tsx +31 -0
- package/src/Notifications/Snackbar/SnackbarContainer/SnackbarContainer.module.scss +1 -1
- package/src/Notifications/Snackbar/SnackbarItem/SnackbarItem.module.scss +1 -1
- package/src/Pagination/Pagination.module.scss +74 -74
- package/src/StatusIndicator/StatusIndicator.module.scss +27 -0
- package/src/StatusIndicator/StatusIndicator.test.tsx +127 -0
- package/src/StatusIndicator/StatusIndicator.tsx +25 -0
- package/src/Tiles/Tile.module.scss +1 -1
- package/src/Tiles/Tile.test.tsx +4 -4
- package/src/_BaseStyling_/BaseStyling.tsx +14 -6
- package/src/index.ts +85 -48
- package/src/mixins.module.scss +171 -0
- package/src/readyclasses.module.scss +0 -30
|
@@ -0,0 +1,421 @@
|
|
|
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
|
+
});
|
|
277
|
+
|
|
278
|
+
describe('spacing should work correctly', () => {
|
|
279
|
+
it('gives the proper paddings to the right elements', () => {
|
|
280
|
+
const { dataGrid } = createDataGrid((defaultParams) => ({
|
|
281
|
+
...defaultParams,
|
|
282
|
+
spacing: { padding: 4 },
|
|
283
|
+
paginationProps: {
|
|
284
|
+
totalElements: 105,
|
|
285
|
+
pageSize: 10,
|
|
286
|
+
currentPage: 2,
|
|
287
|
+
onPageChange: jest.fn(),
|
|
288
|
+
onPageSizeChange: jest.fn(),
|
|
289
|
+
},
|
|
290
|
+
}));
|
|
291
|
+
|
|
292
|
+
const firstHeaderCell = dataGrid.querySelector('.table .thead .row .header-cell');
|
|
293
|
+
const lastHeaderCell = dataGrid.querySelector('.table .thead .row td');
|
|
294
|
+
const firstBodyCell = dataGrid.querySelector('.table tbody .row .cell');
|
|
295
|
+
const lastBodyCell = dataGrid.querySelector('.table tbody .row td:last-of-type');
|
|
296
|
+
const pagination = dataGrid.querySelector('.pagination-wrapper');
|
|
297
|
+
|
|
298
|
+
expect(dataGrid).toHaveStyle({ 'padding-top': '1rem', 'padding-bottom': '1rem' });
|
|
299
|
+
expect(firstHeaderCell).toHaveStyle({ 'padding-left': '1rem' });
|
|
300
|
+
expect(lastHeaderCell).toHaveStyle({ 'padding-right': '1rem' });
|
|
301
|
+
expect(firstBodyCell).toHaveStyle({ 'padding-left': '1rem' });
|
|
302
|
+
expect(lastBodyCell).toHaveStyle({ 'padding-right': '1rem' });
|
|
303
|
+
expect(pagination).toHaveStyle({ 'padding-left': '1rem', 'padding-right': '1rem' });
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('gives the proper paddings to the right elements', () => {
|
|
307
|
+
const { dataGrid } = createDataGrid((defaultParams) => ({
|
|
308
|
+
...defaultParams,
|
|
309
|
+
spacing: { padding: '4 5' },
|
|
310
|
+
paginationProps: {
|
|
311
|
+
totalElements: 105,
|
|
312
|
+
pageSize: 10,
|
|
313
|
+
currentPage: 2,
|
|
314
|
+
onPageChange: jest.fn(),
|
|
315
|
+
onPageSizeChange: jest.fn(),
|
|
316
|
+
},
|
|
317
|
+
}));
|
|
318
|
+
|
|
319
|
+
const firstHeaderCell = dataGrid.querySelector('.table .thead .row .header-cell');
|
|
320
|
+
const lastHeaderCell = dataGrid.querySelector('.table .thead .row td');
|
|
321
|
+
const firstBodyCell = dataGrid.querySelector('.table tbody .row .cell');
|
|
322
|
+
const lastBodyCell = dataGrid.querySelector('.table tbody .row td:last-of-type');
|
|
323
|
+
const pagination = dataGrid.querySelector('.pagination-wrapper');
|
|
324
|
+
|
|
325
|
+
expect(dataGrid).toHaveStyle({ 'padding-top': '1rem', 'padding-bottom': '1rem' });
|
|
326
|
+
expect(firstHeaderCell).toHaveStyle({ 'padding-left': '1.25rem' });
|
|
327
|
+
expect(lastHeaderCell).toHaveStyle({ 'padding-right': '1.25rem' });
|
|
328
|
+
expect(firstBodyCell).toHaveStyle({ 'padding-left': '1.25rem' });
|
|
329
|
+
expect(lastBodyCell).toHaveStyle({ 'padding-right': '1.25rem' });
|
|
330
|
+
expect(pagination).toHaveStyle({ 'padding-left': '1.25rem', 'padding-right': '1.25rem' });
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it('gives the proper paddings to the right elements', () => {
|
|
334
|
+
const { dataGrid } = createDataGrid((defaultParams) => ({
|
|
335
|
+
...defaultParams,
|
|
336
|
+
spacing: { padding: '4 5 8' },
|
|
337
|
+
paginationProps: {
|
|
338
|
+
totalElements: 105,
|
|
339
|
+
pageSize: 10,
|
|
340
|
+
currentPage: 2,
|
|
341
|
+
onPageChange: jest.fn(),
|
|
342
|
+
onPageSizeChange: jest.fn(),
|
|
343
|
+
},
|
|
344
|
+
}));
|
|
345
|
+
|
|
346
|
+
const firstHeaderCell = dataGrid.querySelector('.table .thead .row .header-cell');
|
|
347
|
+
const lastHeaderCell = dataGrid.querySelector('.table .thead .row td');
|
|
348
|
+
const firstBodyCell = dataGrid.querySelector('.table tbody .row .cell');
|
|
349
|
+
const lastBodyCell = dataGrid.querySelector('.table tbody .row td:last-of-type');
|
|
350
|
+
const pagination = dataGrid.querySelector('.pagination-wrapper');
|
|
351
|
+
|
|
352
|
+
expect(dataGrid).toHaveStyle({ 'padding-top': '1rem', 'padding-bottom': '2rem' });
|
|
353
|
+
expect(firstHeaderCell).toHaveStyle({ 'padding-left': '1.25rem' });
|
|
354
|
+
expect(lastHeaderCell).toHaveStyle({ 'padding-right': '1.25rem' });
|
|
355
|
+
expect(firstBodyCell).toHaveStyle({ 'padding-left': '1.25rem' });
|
|
356
|
+
expect(lastBodyCell).toHaveStyle({ 'padding-right': '1.25rem' });
|
|
357
|
+
expect(pagination).toHaveStyle({ 'padding-left': '1.25rem', 'padding-right': '1.25rem' });
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it('gives the proper paddings to the right elements', () => {
|
|
361
|
+
const { dataGrid } = createDataGrid((defaultParams) => ({
|
|
362
|
+
...defaultParams,
|
|
363
|
+
spacing: { padding: '6 4 8 8' },
|
|
364
|
+
paginationProps: {
|
|
365
|
+
totalElements: 105,
|
|
366
|
+
pageSize: 10,
|
|
367
|
+
currentPage: 2,
|
|
368
|
+
onPageChange: jest.fn(),
|
|
369
|
+
onPageSizeChange: jest.fn(),
|
|
370
|
+
},
|
|
371
|
+
}));
|
|
372
|
+
|
|
373
|
+
const firstHeaderCell = dataGrid.querySelector('.table .thead .row .header-cell');
|
|
374
|
+
const lastHeaderCell = dataGrid.querySelector('.table .thead .row td');
|
|
375
|
+
const firstBodyCell = dataGrid.querySelector('.table tbody .row .cell');
|
|
376
|
+
const lastBodyCell = dataGrid.querySelector('.table tbody .row td:last-of-type');
|
|
377
|
+
const pagination = dataGrid.querySelector('.pagination-wrapper');
|
|
378
|
+
|
|
379
|
+
expect(dataGrid).toHaveStyle({ 'padding-top': '1.5rem', 'padding-bottom': '2rem' });
|
|
380
|
+
expect(firstHeaderCell).toHaveStyle({ 'padding-left': '2rem' });
|
|
381
|
+
expect(lastHeaderCell).toHaveStyle({ 'padding-right': '1rem' });
|
|
382
|
+
expect(firstBodyCell).toHaveStyle({ 'padding-left': '2rem' });
|
|
383
|
+
expect(lastBodyCell).toHaveStyle({ 'padding-right': '1rem' });
|
|
384
|
+
expect(pagination).toHaveStyle({ 'padding-left': '2rem', 'padding-right': '1rem' });
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it('gives the proper paddings to the right elements, also with contextmenu disabled', () => {
|
|
388
|
+
const { dataGrid } = createDataGrid((defaultParams) => ({
|
|
389
|
+
...defaultParams,
|
|
390
|
+
spacing: { padding: '6 4 8 8' },
|
|
391
|
+
disableContextMenuColumn: true,
|
|
392
|
+
paginationProps: {
|
|
393
|
+
totalElements: 105,
|
|
394
|
+
pageSize: 10,
|
|
395
|
+
currentPage: 2,
|
|
396
|
+
onPageChange: jest.fn(),
|
|
397
|
+
onPageSizeChange: jest.fn(),
|
|
398
|
+
},
|
|
399
|
+
children: ({ item }) => (
|
|
400
|
+
<DataGridRow key={item.firstName}>
|
|
401
|
+
<DataGridCell>{item.firstName}</DataGridCell>
|
|
402
|
+
<DataGridCell>{item.lastName}</DataGridCell>
|
|
403
|
+
<DataGridCell>{item.date}</DataGridCell>
|
|
404
|
+
</DataGridRow>
|
|
405
|
+
),
|
|
406
|
+
}));
|
|
407
|
+
|
|
408
|
+
const firstHeaderCell = dataGrid.querySelector('.table .thead .row .header-cell');
|
|
409
|
+
const lastHeaderCell = dataGrid.querySelector('.table .thead .row th:last-of-type');
|
|
410
|
+
const firstBodyCell = dataGrid.querySelector('.table tbody .row .cell');
|
|
411
|
+
const lastBodyCell = dataGrid.querySelector('.table tbody .row td:last-of-type');
|
|
412
|
+
const pagination = dataGrid.querySelector('.pagination-wrapper');
|
|
413
|
+
|
|
414
|
+
expect(dataGrid).toHaveStyle({ 'padding-top': '1.5rem', 'padding-bottom': '2rem' });
|
|
415
|
+
expect(firstHeaderCell).toHaveStyle({ 'padding-left': '2rem' });
|
|
416
|
+
expect(lastHeaderCell).toHaveStyle({ 'padding-right': '1rem' });
|
|
417
|
+
expect(firstBodyCell).toHaveStyle({ 'padding-left': '2rem' });
|
|
418
|
+
expect(lastBodyCell).toHaveStyle({ 'padding-right': '1rem' });
|
|
419
|
+
expect(pagination).toHaveStyle({ 'padding-left': '2rem', 'padding-right': '1rem' });
|
|
420
|
+
});
|
|
421
|
+
});
|
|
@@ -0,0 +1,157 @@
|
|
|
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
|
+
import { Spacing, useSpacing } from '../hooks/useSpacing';
|
|
10
|
+
|
|
11
|
+
export interface Props<T> extends ComponentPropsWithRef<'div'> {
|
|
12
|
+
children: ({ item, index }: { item: T; index: number }) => ReactElement;
|
|
13
|
+
data?: T[];
|
|
14
|
+
initialSort?: Sort;
|
|
15
|
+
onSort?: OnSortFunction;
|
|
16
|
+
headers: HeaderCell[];
|
|
17
|
+
actions?: {
|
|
18
|
+
enableAddBtn?: boolean;
|
|
19
|
+
enableColumnsBtn?: boolean;
|
|
20
|
+
enableSearchBtn?: boolean;
|
|
21
|
+
addBtnProps?: ButtonProps;
|
|
22
|
+
columnsBtnProps?: ButtonProps;
|
|
23
|
+
searchBtnProps?: ButtonProps;
|
|
24
|
+
};
|
|
25
|
+
emptyLabel?: string;
|
|
26
|
+
paginationProps?: PaginationProps;
|
|
27
|
+
disableContextMenuColumn?: boolean;
|
|
28
|
+
isLoading?: boolean;
|
|
29
|
+
enableMultiSorting?: boolean;
|
|
30
|
+
spacing?: Spacing;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const DataGridInner = <T extends {}>(
|
|
34
|
+
{
|
|
35
|
+
children,
|
|
36
|
+
data,
|
|
37
|
+
initialSort,
|
|
38
|
+
onSort,
|
|
39
|
+
headers,
|
|
40
|
+
actions = {},
|
|
41
|
+
paginationProps,
|
|
42
|
+
disableContextMenuColumn,
|
|
43
|
+
isLoading,
|
|
44
|
+
enableMultiSorting,
|
|
45
|
+
emptyLabel,
|
|
46
|
+
spacing,
|
|
47
|
+
style,
|
|
48
|
+
...rest
|
|
49
|
+
}: Props<T>,
|
|
50
|
+
ref: Ref<HTMLDivElement>
|
|
51
|
+
) => {
|
|
52
|
+
if (!headers) {
|
|
53
|
+
throw new Error('Headers definition has to be provided');
|
|
54
|
+
}
|
|
55
|
+
if (!children) {
|
|
56
|
+
throw new Error('DataGridBody should be provieded with a template how to render rows');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const [internalHeaders, setInternalHeaders] = useState(headers);
|
|
60
|
+
const styleWithSpacing = useSpacing(spacing, style);
|
|
61
|
+
|
|
62
|
+
useEffect(() => setInternalHeaders(headers), [headers]);
|
|
63
|
+
|
|
64
|
+
const onColumnToggled = (colName: ColumnName) => {
|
|
65
|
+
setInternalHeaders(
|
|
66
|
+
internalHeaders.map((item) =>
|
|
67
|
+
item.name !== colName ? item : { ...item, hidden: !item.hidden }
|
|
68
|
+
)
|
|
69
|
+
);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
if (styleWithSpacing?.padding) {
|
|
73
|
+
const splitPaddings = styleWithSpacing.padding.toString().split(' ');
|
|
74
|
+
|
|
75
|
+
let paddingLeftIndex: number = 0;
|
|
76
|
+
|
|
77
|
+
if (splitPaddings.length >= 2) {
|
|
78
|
+
paddingLeftIndex = 1;
|
|
79
|
+
}
|
|
80
|
+
if (splitPaddings.length === 4) {
|
|
81
|
+
paddingLeftIndex = 3;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
Object.defineProperties(styleWithSpacing, {
|
|
85
|
+
paddingTop: {
|
|
86
|
+
value: splitPaddings[0],
|
|
87
|
+
},
|
|
88
|
+
paddingRight: {
|
|
89
|
+
value: splitPaddings[splitPaddings.length - 1 > 0 ? 1 : 0],
|
|
90
|
+
},
|
|
91
|
+
paddingBottom: {
|
|
92
|
+
value: splitPaddings[splitPaddings.length / 3 >= 1 ? 2 : 0],
|
|
93
|
+
},
|
|
94
|
+
paddingLeft: {
|
|
95
|
+
value: splitPaddings[paddingLeftIndex],
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return (
|
|
101
|
+
<div
|
|
102
|
+
{...rest}
|
|
103
|
+
className={classes['grid-wrapper']}
|
|
104
|
+
ref={ref}
|
|
105
|
+
style={{
|
|
106
|
+
paddingTop: styleWithSpacing?.paddingTop,
|
|
107
|
+
paddingBottom: styleWithSpacing?.paddingBottom,
|
|
108
|
+
}}
|
|
109
|
+
>
|
|
110
|
+
<DataGridActions
|
|
111
|
+
{...actions}
|
|
112
|
+
style={{
|
|
113
|
+
paddingLeft: styleWithSpacing?.paddingLeft,
|
|
114
|
+
paddingRight: styleWithSpacing?.paddingRight,
|
|
115
|
+
}}
|
|
116
|
+
headers={internalHeaders}
|
|
117
|
+
onColumnToggled={onColumnToggled}
|
|
118
|
+
/>
|
|
119
|
+
<div className={classes['table-wrapper']}>
|
|
120
|
+
<table className={classes['table']}>
|
|
121
|
+
<DataGridHeader
|
|
122
|
+
headers={internalHeaders}
|
|
123
|
+
initialSort={initialSort}
|
|
124
|
+
onSort={onSort}
|
|
125
|
+
disableContextMenuColumn={disableContextMenuColumn}
|
|
126
|
+
enableMultiSorting={enableMultiSorting}
|
|
127
|
+
spacing={styleWithSpacing}
|
|
128
|
+
/>
|
|
129
|
+
<DataGridBody
|
|
130
|
+
children={children}
|
|
131
|
+
data={data}
|
|
132
|
+
headers={internalHeaders}
|
|
133
|
+
isLoading={isLoading}
|
|
134
|
+
disableContextMenuColumn={disableContextMenuColumn}
|
|
135
|
+
emptyLabel={emptyLabel}
|
|
136
|
+
spacing={styleWithSpacing}
|
|
137
|
+
/>
|
|
138
|
+
</table>
|
|
139
|
+
</div>
|
|
140
|
+
{paginationProps && !isLoading && (
|
|
141
|
+
<Pagination
|
|
142
|
+
{...paginationProps}
|
|
143
|
+
style={{
|
|
144
|
+
...paginationProps.style,
|
|
145
|
+
paddingLeft: styleWithSpacing?.paddingLeft,
|
|
146
|
+
paddingRight: styleWithSpacing?.paddingRight,
|
|
147
|
+
}}
|
|
148
|
+
className={`${classes['pagination']} ${paginationProps.className ?? ''}`}
|
|
149
|
+
/>
|
|
150
|
+
)}
|
|
151
|
+
</div>
|
|
152
|
+
);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
export const DataGrid = React.forwardRef(DataGridInner) as <T extends {}>(
|
|
156
|
+
p: Props<T> & { ref?: Ref<HTMLDivElement> }
|
|
157
|
+
) => 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
|
+
}
|