@onewelcome/react-lib-components 0.1.7-alpha → 0.1.8-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/DataGrid/DataGrid.d.ts +30 -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 +16 -0
- package/dist/DataGrid/DataGridBody/DataGridCell.d.ts +6 -0
- package/dist/DataGrid/DataGridBody/DataGridRow.d.ts +7 -0
- package/dist/DataGrid/DataGridHeader/DataGridHeader.d.ts +10 -0
- package/dist/DataGrid/DataGridHeader/DataGridHeaderCell.d.ts +10 -0
- package/dist/DataGrid/datagrid.interfaces.d.ts +13 -0
- package/dist/Form/Select/Option.d.ts +9 -4
- package/dist/Form/Select/Select.d.ts +8 -2
- package/dist/Form/Wrapper/SelectWrapper/SelectWrapper.d.ts +1 -1
- package/dist/Icon/Icon.d.ts +1 -0
- package/dist/_BaseStyling_/BaseStyling.d.ts +4 -0
- package/dist/hooks/useSpacing.d.ts +1 -1
- package/dist/hooks/useWrapper.d.ts +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/react-lib-components.cjs.development.js +949 -272
- 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 +947 -273
- package/dist/react-lib-components.esm.js.map +1 -1
- package/dist/util/helper.d.ts +1 -1
- package/package.json +11 -11
- package/src/ContextMenu/ContextMenu.tsx +16 -1
- package/src/DataGrid/DataGrid.module.scss +21 -0
- package/src/DataGrid/DataGrid.test.tsx +276 -0
- package/src/DataGrid/DataGrid.tsx +101 -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 +64 -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 +25 -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 +27 -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 +80 -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/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/Wrapper/SelectWrapper/SelectWrapper.test.tsx +44 -0
- package/src/Form/Wrapper/SelectWrapper/SelectWrapper.tsx +4 -2
- package/src/Icon/Icon.module.scss +4 -0
- package/src/Icon/Icon.tsx +1 -0
- package/src/Notifications/BaseModal/BaseModal.module.scss +1 -1
- package/src/Notifications/BaseModal/BaseModal.test.tsx +2 -1
- package/src/Notifications/Dialog/Dialog.module.scss +1 -1
- 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/_BaseStyling_/BaseStyling.tsx +14 -6
- package/src/index.ts +6 -0
|
@@ -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,123 @@
|
|
|
1
|
+
import React, { useEffect, useRef } from 'react';
|
|
2
|
+
import { DataGridBody, Props } from './DataGridBody';
|
|
3
|
+
import { render, getAllByRole } from '@testing-library/react';
|
|
4
|
+
import { DataGridRow } from './DataGridRow';
|
|
5
|
+
import { DataGridCell } from './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
|
+
|
|
11
|
+
type DataType = { firstName: string; lastName: string; date: string };
|
|
12
|
+
|
|
13
|
+
const defaultParams: Props<DataType> = {
|
|
14
|
+
children: ({ item }) => (
|
|
15
|
+
<DataGridRow key={item.firstName}>
|
|
16
|
+
<DataGridCell>{item.firstName}</DataGridCell>
|
|
17
|
+
<DataGridCell>{item.lastName}</DataGridCell>
|
|
18
|
+
<DataGridCell>{item.date}</DataGridCell>
|
|
19
|
+
<DataGridCell>
|
|
20
|
+
<ContextMenu
|
|
21
|
+
id={`consent_menu_${item.firstName}`}
|
|
22
|
+
trigger={
|
|
23
|
+
<IconButton title={`Actions for ${item.firstName}`} color="default">
|
|
24
|
+
<Icon icon={Icons.EllipsisAlt} />
|
|
25
|
+
</IconButton>
|
|
26
|
+
}
|
|
27
|
+
>
|
|
28
|
+
<ContextMenuItem>Item 1</ContextMenuItem>
|
|
29
|
+
</ContextMenu>
|
|
30
|
+
</DataGridCell>
|
|
31
|
+
</DataGridRow>
|
|
32
|
+
),
|
|
33
|
+
headers: [
|
|
34
|
+
{ name: 'firstName', headline: 'First name' },
|
|
35
|
+
{ name: 'lastName', headline: 'Last name' },
|
|
36
|
+
{ name: 'date', headline: 'Date' },
|
|
37
|
+
],
|
|
38
|
+
data: [
|
|
39
|
+
{ firstName: 'Paweł', lastName: 'Napieracz', date: '12.12.1990' },
|
|
40
|
+
{ firstName: 'Michał', lastName: 'Górski', date: '12.12.1994' },
|
|
41
|
+
{ firstName: 'Daniel', lastName: 'Velden', date: '12.12.199x' },
|
|
42
|
+
{ firstName: 'Jasha', lastName: 'Joachimsthal', date: '12.12.198x' },
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const createDataGridBody = (params?: (defaultParams: Props<DataType>) => Props<DataType>) => {
|
|
47
|
+
let parameters = defaultParams;
|
|
48
|
+
if (params) {
|
|
49
|
+
parameters = params(defaultParams);
|
|
50
|
+
}
|
|
51
|
+
const container = document.createElement('table');
|
|
52
|
+
const queries = render(<DataGridBody {...parameters} data-testid="dataGridBody" />, {
|
|
53
|
+
container,
|
|
54
|
+
});
|
|
55
|
+
const dataGridBody = queries.getByTestId('dataGridBody');
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
...queries,
|
|
59
|
+
dataGridBody,
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
describe('DataGridBody should render', () => {
|
|
64
|
+
it('renders without crashing', () => {
|
|
65
|
+
const { dataGridBody } = createDataGridBody();
|
|
66
|
+
|
|
67
|
+
expect(dataGridBody).toBeDefined();
|
|
68
|
+
const rows = getAllByRole(dataGridBody, 'row');
|
|
69
|
+
expect(rows).toMatchSnapshot();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('renders loading state with only visible columns', () => {
|
|
73
|
+
const headers = [...defaultParams.headers];
|
|
74
|
+
headers[0] = { ...headers[0], hidden: true };
|
|
75
|
+
const { dataGridBody } = createDataGridBody((params) => ({ ...params, headers }));
|
|
76
|
+
|
|
77
|
+
expect(dataGridBody).toBeDefined();
|
|
78
|
+
const rows = getAllByRole(dataGridBody, 'row');
|
|
79
|
+
const firstRowCells = getAllByRole(rows[0], 'cell');
|
|
80
|
+
expect(firstRowCells).toHaveLength(3);
|
|
81
|
+
expect(firstRowCells[0]).toHaveTextContent(defaultParams.data?.[0].lastName!);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('renders empty state', () => {
|
|
85
|
+
const emptyLabel = 'emptyLabel';
|
|
86
|
+
const { dataGridBody } = createDataGridBody((params) => ({
|
|
87
|
+
...params,
|
|
88
|
+
emptyLabel,
|
|
89
|
+
data: undefined,
|
|
90
|
+
}));
|
|
91
|
+
|
|
92
|
+
const rows = getAllByRole(dataGridBody, 'row');
|
|
93
|
+
const firstRowCells = getAllByRole(rows[0], 'cell');
|
|
94
|
+
expect(firstRowCells).toHaveLength(1);
|
|
95
|
+
expect(firstRowCells[0]).toHaveAttribute('colspan', `${defaultParams.headers.length + 1}`);
|
|
96
|
+
expect(firstRowCells[0]).toHaveTextContent(emptyLabel);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('ref should work', () => {
|
|
101
|
+
it('should give back the proper data prop, this also checks if the component propagates ...rest properly', () => {
|
|
102
|
+
const ExampleComponent = ({
|
|
103
|
+
propagateRef,
|
|
104
|
+
}: {
|
|
105
|
+
propagateRef: (ref: React.RefObject<HTMLElement>) => void;
|
|
106
|
+
}) => {
|
|
107
|
+
const ref = useRef(null);
|
|
108
|
+
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
propagateRef(ref);
|
|
111
|
+
}, [ref]);
|
|
112
|
+
|
|
113
|
+
return <DataGridBody {...defaultParams} data-ref="testing" ref={ref} />;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const refCheck = (ref: React.RefObject<HTMLElement>) => {
|
|
117
|
+
expect(ref.current).toHaveAttribute('data-ref', 'testing');
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const container = document.createElement('table');
|
|
121
|
+
render(<ExampleComponent propagateRef={refCheck} />, { container });
|
|
122
|
+
});
|
|
123
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import React, { ComponentPropsWithRef, ReactElement, Ref } from 'react';
|
|
2
|
+
import { HeaderCell } from '../datagrid.interfaces';
|
|
3
|
+
import { DataGridCell } from './DataGridCell';
|
|
4
|
+
import { DataGridRow } from './DataGridRow';
|
|
5
|
+
import classes from './DataGridBody.module.scss';
|
|
6
|
+
import { Typography } from '../../Typography/Typography';
|
|
7
|
+
|
|
8
|
+
export interface Props<T> extends ComponentPropsWithRef<'tbody'> {
|
|
9
|
+
children: ({ item, index }: { item: T; index: number }) => ReactElement;
|
|
10
|
+
data?: T[];
|
|
11
|
+
headers: HeaderCell[];
|
|
12
|
+
isLoading?: boolean;
|
|
13
|
+
disableContextMenuColumn?: boolean;
|
|
14
|
+
emptyLabel?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const skeletonLoadingRows = 9;
|
|
18
|
+
|
|
19
|
+
const DataGridBodyInner = <T extends {}>(
|
|
20
|
+
{ children, data, headers, isLoading, disableContextMenuColumn, emptyLabel, ...rest }: Props<T>,
|
|
21
|
+
ref: Ref<HTMLTableSectionElement>
|
|
22
|
+
) => {
|
|
23
|
+
const renderContent = () => {
|
|
24
|
+
const visibleColumns = headers.filter((header) => !header.hidden).length;
|
|
25
|
+
if (isLoading) {
|
|
26
|
+
return Array.from(Array(skeletonLoadingRows)).map((_, rowIdx) => (
|
|
27
|
+
<DataGridRow key={rowIdx} isLoading>
|
|
28
|
+
{Array.from(Array(visibleColumns)).map((__, colIdx) => (
|
|
29
|
+
<DataGridCell key={colIdx} isLoading></DataGridCell>
|
|
30
|
+
))}
|
|
31
|
+
{!disableContextMenuColumn && <DataGridCell></DataGridCell>}
|
|
32
|
+
</DataGridRow>
|
|
33
|
+
));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const emptyData = !data || data.length === 0;
|
|
37
|
+
if (emptyData) {
|
|
38
|
+
return (
|
|
39
|
+
<tr>
|
|
40
|
+
<td
|
|
41
|
+
className={classes['empty']}
|
|
42
|
+
colSpan={visibleColumns + (disableContextMenuColumn ? 0 : 1)}
|
|
43
|
+
>
|
|
44
|
+
<Typography variant="body" spacing={{ margin: 0 }}>
|
|
45
|
+
{emptyLabel}
|
|
46
|
+
</Typography>
|
|
47
|
+
</td>
|
|
48
|
+
</tr>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return data?.map((item, index) => React.cloneElement(children({ item, index }), { headers }));
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<tbody {...rest} ref={ref}>
|
|
57
|
+
{renderContent()}
|
|
58
|
+
</tbody>
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const DataGridBody = React.forwardRef(DataGridBodyInner) as <T extends {}>(
|
|
63
|
+
p: Props<T> & { ref?: Ref<HTMLTableSectionElement> }
|
|
64
|
+
) => ReactElement;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
@import '../../mixins.module.scss';
|
|
2
|
+
|
|
3
|
+
.cell {
|
|
4
|
+
min-height: 3.5rem;
|
|
5
|
+
padding: 0.5rem 0.625rem;
|
|
6
|
+
|
|
7
|
+
&:first-child {
|
|
8
|
+
padding-left: 1rem;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
&:last-child {
|
|
12
|
+
padding-right: 1rem;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.loading {
|
|
16
|
+
@include skeletonLoading();
|
|
17
|
+
border-radius: 0.5rem;
|
|
18
|
+
height: 1.25rem;
|
|
19
|
+
margin: 0.625rem 0;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@media only screen and (min-width: 50em) {
|
|
24
|
+
.cell {
|
|
25
|
+
&:first-child {
|
|
26
|
+
padding-left: 1.25rem;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
&:last-child {
|
|
30
|
+
padding-right: 1.25rem;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import React, { useEffect, useRef } from 'react';
|
|
2
|
+
import { DataGridCell, Props } from './DataGridCell';
|
|
3
|
+
import { render } from '@testing-library/react';
|
|
4
|
+
|
|
5
|
+
const defaultParams: Props = {
|
|
6
|
+
children: 'cell',
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const createDataGridCell = (params?: (defaultParams: Props) => Props) => {
|
|
10
|
+
let parameters: Props = defaultParams;
|
|
11
|
+
if (params) {
|
|
12
|
+
parameters = params(defaultParams);
|
|
13
|
+
}
|
|
14
|
+
const container = document.createElement('tr');
|
|
15
|
+
const queries = render(<DataGridCell {...parameters} data-testid="dataGridCell" />, {
|
|
16
|
+
container,
|
|
17
|
+
});
|
|
18
|
+
const dataGridCell = queries.getByTestId('dataGridCell');
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
...queries,
|
|
22
|
+
dataGridCell,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
describe('DataGridCell should render', () => {
|
|
27
|
+
it('renders without crashing', () => {
|
|
28
|
+
const { dataGridCell } = createDataGridCell();
|
|
29
|
+
|
|
30
|
+
expect(dataGridCell).toBeDefined();
|
|
31
|
+
expect(dataGridCell).toHaveClass('cell');
|
|
32
|
+
expect(dataGridCell).toHaveTextContent(defaultParams.children as string);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('renders with additional class', () => {
|
|
36
|
+
const { dataGridCell } = createDataGridCell((params) => ({ ...params, className: 'test' }));
|
|
37
|
+
|
|
38
|
+
expect(dataGridCell).toHaveClass('cell', 'test');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('renders loading state', () => {
|
|
42
|
+
const { dataGridCell } = createDataGridCell((params) => ({ ...params, isLoading: true }));
|
|
43
|
+
|
|
44
|
+
const skeletonLoadingEl = dataGridCell.querySelector('div');
|
|
45
|
+
expect(skeletonLoadingEl).toHaveClass('loading');
|
|
46
|
+
expect(skeletonLoadingEl).toHaveAttribute('aria-busy', 'true');
|
|
47
|
+
expect(skeletonLoadingEl).toHaveAttribute('aria-live', 'polite');
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('ref should work', () => {
|
|
52
|
+
it('should give back the proper data prop, this also checks if the component propagates ...rest properly', () => {
|
|
53
|
+
const ExampleComponent = ({
|
|
54
|
+
propagateRef,
|
|
55
|
+
}: {
|
|
56
|
+
propagateRef: (ref: React.RefObject<HTMLElement>) => void;
|
|
57
|
+
}) => {
|
|
58
|
+
const ref = useRef(null);
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
propagateRef(ref);
|
|
62
|
+
}, [ref]);
|
|
63
|
+
|
|
64
|
+
return <DataGridCell {...defaultParams} data-ref="testing" ref={ref} />;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const refCheck = (ref: React.RefObject<HTMLElement>) => {
|
|
68
|
+
expect(ref.current).toHaveAttribute('data-ref', 'testing');
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const container = document.createElement('tr');
|
|
72
|
+
render(<ExampleComponent propagateRef={refCheck} />, { container });
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React, { ComponentPropsWithRef, ReactChild } from 'react';
|
|
2
|
+
import { Typography } from '../../Typography/Typography';
|
|
3
|
+
import classes from './DataGridCell.module.scss';
|
|
4
|
+
|
|
5
|
+
export interface Props extends ComponentPropsWithRef<'td'> {
|
|
6
|
+
children?: ReactChild;
|
|
7
|
+
isLoading?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const DataGridCell = React.forwardRef<HTMLTableCellElement, Props>(
|
|
11
|
+
({ children, className, isLoading, ...rest }: Props, ref) => {
|
|
12
|
+
return (
|
|
13
|
+
<td {...rest} ref={ref} className={`${classes['cell']} ${className ?? ''}`}>
|
|
14
|
+
{isLoading && (
|
|
15
|
+
<div className={classes['loading']} aria-busy="true" aria-live="polite"></div>
|
|
16
|
+
)}
|
|
17
|
+
{!isLoading && (
|
|
18
|
+
<Typography variant="body" tag="span">
|
|
19
|
+
{children}
|
|
20
|
+
</Typography>
|
|
21
|
+
)}
|
|
22
|
+
</td>
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
);
|