@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,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
+ });
@@ -0,0 +1,109 @@
1
+ import React, { ComponentPropsWithRef, Fragment, useRef, useState } from 'react';
2
+ import { Button, Props as ButtonProps } from '../../Button/Button';
3
+ import { IconButton } from '../../Button/IconButton';
4
+ import { Icon, Icons } from '../../Icon/Icon';
5
+ import { HeaderCell, ColumnName } from '../datagrid.interfaces';
6
+ import classes from './DataGridActions.module.scss';
7
+ import { DataGridColumnsToggle } from './DataGridColumnsToggle';
8
+
9
+ export interface Props extends ComponentPropsWithRef<'div'> {
10
+ enableAddBtn?: boolean;
11
+ enableColumnsBtn?: boolean;
12
+ enableSearchBtn?: boolean;
13
+ addBtnProps?: ButtonProps;
14
+ columnsBtnProps?: ButtonProps;
15
+ searchBtnProps?: ButtonProps;
16
+ headers: HeaderCell[];
17
+ onColumnToggled: (colName: ColumnName) => void;
18
+ }
19
+
20
+ export const DataGridActions = React.forwardRef<HTMLDivElement, Props>(
21
+ (
22
+ {
23
+ className,
24
+ enableAddBtn,
25
+ enableColumnsBtn,
26
+ enableSearchBtn,
27
+ addBtnProps = {},
28
+ columnsBtnProps = {},
29
+ searchBtnProps = {},
30
+ headers,
31
+ onColumnToggled,
32
+ ...rest
33
+ }: Props,
34
+ ref
35
+ ) => {
36
+ const isHidden = !(enableAddBtn || enableColumnsBtn || enableSearchBtn);
37
+ const [showColsPopover, setShowColsPopover] = useState(false);
38
+ const showColumnBtn = useRef<HTMLButtonElement>(null);
39
+
40
+ return isHidden ? null : (
41
+ <div {...rest} ref={ref} className={`${classes['actions']} ${className ?? ''}`}>
42
+ <div className={classes['left-actions']}>
43
+ {enableAddBtn && (
44
+ <Button
45
+ color="primary"
46
+ startIcon={<Icon icon={Icons.Plus} />}
47
+ title="Add item"
48
+ type="button"
49
+ variant="outline"
50
+ children="Add item"
51
+ {...addBtnProps}
52
+ />
53
+ )}
54
+ </div>
55
+ <div className={classes['right-actions']}>
56
+ {enableColumnsBtn && (
57
+ <Fragment>
58
+ <Button
59
+ startIcon={<Icon icon={Icons.Change} />}
60
+ title="Show/hide columns"
61
+ variant="text"
62
+ children="Columns"
63
+ {...columnsBtnProps}
64
+ className={`${classes['desktop']} ${columnsBtnProps?.className ?? ''}`}
65
+ ref={showColumnBtn}
66
+ onClick={() => setShowColsPopover(true)}
67
+ />
68
+ <IconButton
69
+ title="Show/hide columns"
70
+ {...columnsBtnProps}
71
+ onClick={() => setShowColsPopover(true)}
72
+ className={`${classes['mobile']} ${columnsBtnProps?.className ?? ''}`}
73
+ >
74
+ <Icon icon={Icons.Change} />
75
+ </IconButton>
76
+ <DataGridColumnsToggle
77
+ aria-hidden={!showColsPopover}
78
+ open={showColsPopover}
79
+ headers={headers}
80
+ onClose={() => setShowColsPopover(false)}
81
+ onToggleClicked={onColumnToggled}
82
+ anchorEl={showColumnBtn}
83
+ />
84
+ </Fragment>
85
+ )}
86
+ {enableSearchBtn && (
87
+ <Fragment>
88
+ <Button
89
+ startIcon={<Icon icon={Icons.TableSearch} />}
90
+ title="Search"
91
+ variant="text"
92
+ children="Search"
93
+ {...searchBtnProps}
94
+ className={`${classes['desktop']} ${searchBtnProps?.className ?? ''}`}
95
+ />
96
+ <IconButton
97
+ title="Search"
98
+ {...searchBtnProps}
99
+ className={`${classes['mobile']} ${columnsBtnProps?.className ?? ''}`}
100
+ >
101
+ <Icon icon={Icons.TableSearch} />
102
+ </IconButton>
103
+ </Fragment>
104
+ )}
105
+ </div>
106
+ </div>
107
+ );
108
+ }
109
+ );
@@ -0,0 +1,41 @@
1
+ .popover {
2
+ background-color: var(--modal-background-color);
3
+ z-index: 1;
4
+ min-width: 17.5rem;
5
+ }
6
+
7
+ .backdrop {
8
+ content: '';
9
+ position: absolute;
10
+ width: 100%;
11
+ height: 100%;
12
+ left: 0;
13
+ top: 0;
14
+ background-color: transparent;
15
+ }
16
+
17
+ .header {
18
+ min-width: 5rem;
19
+ padding: 1rem 0.5rem 1rem 1.5rem;
20
+ background-color: var(--modal-header-background-color);
21
+ border-top-left-radius: 0.5rem;
22
+ border-top-right-radius: 0.5rem;
23
+ display: flex;
24
+ align-items: center;
25
+ }
26
+
27
+ .title {
28
+ flex: 1;
29
+ margin: 0;
30
+ }
31
+
32
+ .close-btn {
33
+ margin-left: 0.5rem;
34
+ }
35
+
36
+ .content {
37
+ padding: 1.5rem;
38
+ display: flex;
39
+ flex-direction: column;
40
+ gap: 1.25rem;
41
+ }
@@ -0,0 +1,83 @@
1
+ import React, { useEffect, useRef } from 'react';
2
+ import { DataGridColumnsToggle, Props } from './DataGridColumnsToggle';
3
+ import { render } from '@testing-library/react';
4
+ import userEvent from '@testing-library/user-event';
5
+
6
+ const defaultParams: Props = {
7
+ open: true,
8
+ headers: [
9
+ { headline: 'Name', name: 'name' },
10
+ { headline: 'Date', name: 'date', hidden: true },
11
+ ],
12
+ onClose: jest.fn(),
13
+ onToggleClicked: jest.fn(),
14
+ };
15
+
16
+ const createDataGridColumnsToggle = (params?: (defaultParams: Props) => Props) => {
17
+ let parameters: Props = defaultParams;
18
+ if (params) {
19
+ parameters = params(defaultParams);
20
+ }
21
+ const queries = render(
22
+ <DataGridColumnsToggle {...parameters} data-testid="dataGridColumnsToggle" />
23
+ );
24
+ const dataGridColumnsToggle = queries.getByTestId('dataGridColumnsToggle');
25
+
26
+ return {
27
+ ...queries,
28
+ dataGridColumnsToggle,
29
+ };
30
+ };
31
+
32
+ describe('DataGridColumnsToggle should render', () => {
33
+ it('renders without crashing', () => {
34
+ const { dataGridColumnsToggle, getByLabelText, getByText } = createDataGridColumnsToggle();
35
+
36
+ expect(dataGridColumnsToggle).toBeDefined();
37
+ expect(getByText('Show columns')).toBeDefined();
38
+ expect(getByLabelText(defaultParams.headers[0].headline)).toBeChecked();
39
+ expect(getByLabelText(defaultParams.headers[1].headline)).not.toBeChecked();
40
+ });
41
+ });
42
+
43
+ describe('DataGridColumnsToggle should be interactive', () => {
44
+ it('clicking on close calls onClose callback', () => {
45
+ const { getByRole } = createDataGridColumnsToggle();
46
+
47
+ userEvent.click(getByRole('button'));
48
+
49
+ expect(defaultParams.onClose).toBeCalledTimes(1);
50
+ });
51
+
52
+ it('clicking outside of container (on backdrop layer) calls onClose callback', () => {
53
+ createDataGridColumnsToggle();
54
+
55
+ userEvent.click(document.querySelector('.backdrop')!);
56
+
57
+ expect(defaultParams.onClose).toBeCalledTimes(1);
58
+ });
59
+ });
60
+
61
+ describe('ref should work', () => {
62
+ it('should give back the proper data prop, this also checks if the component propagates ...rest properly', () => {
63
+ const ExampleComponent = ({
64
+ propagateRef,
65
+ }: {
66
+ propagateRef: (ref: React.RefObject<HTMLElement>) => void;
67
+ }) => {
68
+ const ref = useRef(null);
69
+
70
+ useEffect(() => {
71
+ propagateRef(ref);
72
+ }, [ref]);
73
+
74
+ return <DataGridColumnsToggle {...defaultParams} data-ref="testing" ref={ref} />;
75
+ };
76
+
77
+ const refCheck = (ref: React.RefObject<HTMLElement>) => {
78
+ expect(ref.current).toHaveAttribute('data-ref', 'testing');
79
+ };
80
+
81
+ render(<ExampleComponent propagateRef={refCheck} />);
82
+ });
83
+ });
@@ -0,0 +1,88 @@
1
+ import React, { Fragment, useState } from 'react';
2
+ import { IconButton } from '../../Button/IconButton';
3
+ import { Toggle } from '../../Form/Toggle/Toggle';
4
+ import { Icon, Icons } from '../../Icon/Icon';
5
+ import { Popover, Props as PopoverProps } from '../../Popover/Popover';
6
+ import { Typography } from '../../Typography/Typography';
7
+ import { generateID } from '../../util/helper';
8
+ import { ColumnName, HeaderCell } from '../datagrid.interfaces';
9
+ import classes from './DataGridColumnsToggle.module.scss';
10
+ import { createPortal } from 'react-dom';
11
+
12
+ export interface Props extends PopoverProps {
13
+ open: boolean;
14
+ headers: HeaderCell[];
15
+ titleLabel?: string;
16
+ closeButtonTitle?: string;
17
+ onClose: () => void;
18
+ onToggleClicked: (colName: ColumnName) => void;
19
+ domRoot?: HTMLElement;
20
+ }
21
+
22
+ export const DataGridColumnsToggle = React.forwardRef<HTMLDivElement, Props>(
23
+ (
24
+ {
25
+ open,
26
+ headers,
27
+ titleLabel = 'Show columns',
28
+ closeButtonTitle = 'Close show columns dialog',
29
+ onClose,
30
+ onToggleClicked,
31
+ className,
32
+ domRoot = document.body,
33
+ ...rest
34
+ }: Props,
35
+ ref
36
+ ) => {
37
+ const [id] = useState(generateID());
38
+ const labelledId = id + '_header';
39
+
40
+ const toggles = headers.map((item) => (
41
+ <Toggle
42
+ key={item.name}
43
+ name={item.name}
44
+ checked={!item.hidden}
45
+ onChange={() => onToggleClicked(item.name)}
46
+ >
47
+ {item.headline}
48
+ </Toggle>
49
+ ));
50
+
51
+ const handleBackdropClick = () => onClose();
52
+
53
+ return (
54
+ <Popover
55
+ {...rest}
56
+ ref={ref}
57
+ className={`${classes['popover']} ${className}`}
58
+ show={open}
59
+ role="dialog"
60
+ aria-modal="true"
61
+ aria-labelledby={labelledId}
62
+ >
63
+ {open &&
64
+ createPortal(
65
+ <div className={classes['backdrop']} onClick={handleBackdropClick}></div>,
66
+ domRoot
67
+ )}
68
+ {open && (
69
+ <Fragment>
70
+ <div className={classes['header']}>
71
+ <Typography id={labelledId} className={classes['title']} tag="span" variant="h4">
72
+ {titleLabel}
73
+ </Typography>
74
+ <IconButton
75
+ onClick={onClose}
76
+ className={`${classes['close-btn']}`}
77
+ title={closeButtonTitle}
78
+ >
79
+ <Icon icon={Icons.Times} />
80
+ </IconButton>
81
+ </div>
82
+ <div className={classes['content']}>{toggles}</div>
83
+ </Fragment>
84
+ )}
85
+ </Popover>
86
+ );
87
+ }
88
+ );
@@ -0,0 +1,10 @@
1
+ .empty {
2
+ text-align: center;
3
+ height: 6rem;
4
+ padding: 2rem;
5
+ vertical-align: top;
6
+
7
+ p {
8
+ color: var(--greyed-out);
9
+ }
10
+ }