@patternfly/react-data-view 5.0.1 → 5.1.1
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/cjs/DataView/DataView.d.ts +4 -0
- package/dist/cjs/DataView/DataView.js +7 -1
- package/dist/cjs/DataViewEventsContext/DataViewEventsContext.d.ts +16 -0
- package/dist/cjs/DataViewEventsContext/DataViewEventsContext.js +62 -0
- package/dist/cjs/DataViewEventsContext/DataViewEventsContext.test.d.ts +1 -0
- package/dist/cjs/DataViewEventsContext/DataViewEventsContext.test.js +72 -0
- package/dist/cjs/DataViewEventsContext/index.d.ts +2 -0
- package/dist/cjs/DataViewEventsContext/index.js +23 -0
- package/dist/cjs/DataViewTable/DataViewTable.d.ts +37 -0
- package/dist/cjs/DataViewTable/DataViewTable.js +59 -0
- package/dist/cjs/DataViewTable/DataViewTable.test.d.ts +1 -0
- package/dist/cjs/DataViewTable/DataViewTable.test.js +27 -0
- package/dist/cjs/DataViewTable/index.d.ts +2 -0
- package/dist/cjs/DataViewTable/index.js +23 -0
- package/dist/cjs/Hooks/pagination.d.ts +13 -1
- package/dist/cjs/Hooks/pagination.js +36 -4
- package/dist/cjs/Hooks/pagination.test.js +53 -1
- package/dist/cjs/Hooks/selection.d.ts +3 -3
- package/dist/cjs/Hooks/selection.js +4 -6
- package/dist/cjs/Hooks/selection.test.js +4 -4
- package/dist/cjs/InternalContext/InternalContext.d.ts +17 -0
- package/dist/cjs/InternalContext/InternalContext.js +35 -0
- package/dist/cjs/InternalContext/InternalContext.test.d.ts +1 -0
- package/dist/cjs/InternalContext/InternalContext.test.js +55 -0
- package/dist/cjs/InternalContext/index.d.ts +2 -0
- package/dist/cjs/InternalContext/index.js +23 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.js +11 -2
- package/dist/dynamic/DataViewEventsContext/package.json +1 -0
- package/dist/dynamic/DataViewTable/package.json +1 -0
- package/dist/dynamic/InternalContext/package.json +1 -0
- package/dist/esm/DataView/DataView.d.ts +4 -0
- package/dist/esm/DataView/DataView.js +7 -1
- package/dist/esm/DataViewEventsContext/DataViewEventsContext.d.ts +16 -0
- package/dist/esm/DataViewEventsContext/DataViewEventsContext.js +34 -0
- package/dist/esm/DataViewEventsContext/DataViewEventsContext.test.d.ts +1 -0
- package/dist/esm/DataViewEventsContext/DataViewEventsContext.test.js +67 -0
- package/dist/esm/DataViewEventsContext/index.d.ts +2 -0
- package/dist/esm/DataViewEventsContext/index.js +2 -0
- package/dist/esm/DataViewTable/DataViewTable.d.ts +37 -0
- package/dist/esm/DataViewTable/DataViewTable.js +49 -0
- package/dist/esm/DataViewTable/DataViewTable.test.d.ts +1 -0
- package/dist/esm/DataViewTable/DataViewTable.test.js +22 -0
- package/dist/esm/DataViewTable/index.d.ts +2 -0
- package/dist/esm/DataViewTable/index.js +2 -0
- package/dist/esm/Hooks/pagination.d.ts +13 -1
- package/dist/esm/Hooks/pagination.js +36 -4
- package/dist/esm/Hooks/pagination.test.js +53 -1
- package/dist/esm/Hooks/selection.d.ts +3 -3
- package/dist/esm/Hooks/selection.js +4 -6
- package/dist/esm/Hooks/selection.test.js +4 -4
- package/dist/esm/InternalContext/InternalContext.d.ts +17 -0
- package/dist/esm/InternalContext/InternalContext.js +7 -0
- package/dist/esm/InternalContext/InternalContext.test.d.ts +1 -0
- package/dist/esm/InternalContext/InternalContext.test.js +50 -0
- package/dist/esm/InternalContext/index.d.ts +2 -0
- package/dist/esm/InternalContext/index.js +2 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +6 -0
- package/package.json +1 -1
- package/patternfly-docs/content/extensions/data-view/examples/Components/Components.md +33 -3
- package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableExample.tsx +47 -0
- package/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsContext.md +32 -0
- package/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsExample.tsx +108 -0
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md +15 -5
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/PaginationExample.tsx +20 -29
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/SelectionExample.tsx +9 -40
- package/patternfly-docs/content/extensions/data-view/examples/Layout/Layout.md +1 -0
- package/patternfly-docs/content/extensions/data-view/examples/Layout/PredefinedLayoutExample.tsx +14 -45
- package/src/DataView/DataView.tsx +14 -2
- package/src/DataViewEventsContext/DataViewEventsContext.test.tsx +105 -0
- package/src/DataViewEventsContext/DataViewEventsContext.tsx +70 -0
- package/src/DataViewEventsContext/index.ts +2 -0
- package/src/DataViewTable/DataViewTable.test.tsx +37 -0
- package/src/DataViewTable/DataViewTable.tsx +96 -0
- package/src/DataViewTable/__snapshots__/DataViewTable.test.tsx.snap +320 -0
- package/src/DataViewTable/index.ts +2 -0
- package/src/Hooks/pagination.test.tsx +81 -1
- package/src/Hooks/pagination.ts +71 -15
- package/src/Hooks/selection.test.tsx +5 -5
- package/src/Hooks/selection.ts +6 -7
- package/src/InternalContext/InternalContext.test.tsx +88 -0
- package/src/InternalContext/InternalContext.tsx +35 -0
- package/src/InternalContext/index.ts +2 -0
- package/src/index.ts +9 -0
|
@@ -19,6 +19,7 @@ import { useDataViewPagination, useDataViewSelection } from '@patternfly/react-d
|
|
|
19
19
|
import { BulkSelect, BulkSelectValue } from '@patternfly/react-component-groups/dist/dynamic/BulkSelect';
|
|
20
20
|
import DataView from '@patternfly/react-data-view/dist/dynamic/DataView';
|
|
21
21
|
import DataViewToolbar from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
|
|
22
|
+
import DataViewTable from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
|
|
22
23
|
|
|
23
24
|
The **data view** component renders record data in a configured layout.
|
|
24
25
|
|
package/patternfly-docs/content/extensions/data-view/examples/Layout/PredefinedLayoutExample.tsx
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import React, { useMemo } from 'react';
|
|
2
2
|
import { Pagination } from '@patternfly/react-core';
|
|
3
|
-
import { Table, Tbody, Th, Thead, Tr, Td } from '@patternfly/react-table';
|
|
4
3
|
import { useDataViewPagination, useDataViewSelection } from '@patternfly/react-data-view/dist/dynamic/Hooks';
|
|
5
4
|
import { BulkSelect, BulkSelectValue } from '@patternfly/react-component-groups/dist/dynamic/BulkSelect';
|
|
6
5
|
import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
|
|
6
|
+
import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
|
|
7
7
|
import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
|
|
8
8
|
|
|
9
9
|
const perPageOptions = [
|
|
@@ -28,43 +28,39 @@ const repositories: Repository[] = [
|
|
|
28
28
|
{ name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
|
|
29
29
|
];
|
|
30
30
|
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
prs: 'Pull requests',
|
|
35
|
-
workspaces: 'Workspaces',
|
|
36
|
-
lastCommit: 'Last commit'
|
|
37
|
-
};
|
|
31
|
+
const rows = repositories.map(item => Object.values(item));
|
|
32
|
+
|
|
33
|
+
const columns = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ];
|
|
38
34
|
|
|
39
35
|
const ouiaId = 'LayoutExample';
|
|
40
36
|
|
|
41
37
|
export const BasicExample: React.FunctionComponent = () => {
|
|
42
38
|
const pagination = useDataViewPagination({ perPage: 5 });
|
|
43
39
|
const { page, perPage } = pagination;
|
|
44
|
-
const selection = useDataViewSelection({});
|
|
40
|
+
const selection = useDataViewSelection({ matchOption: (a, b) => a[0] === b[0] });
|
|
45
41
|
const { selected, onSelect, isSelected } = selection;
|
|
46
42
|
|
|
47
|
-
const
|
|
43
|
+
const pageRows = useMemo(() => rows.slice((page - 1) * perPage, ((page - 1) * perPage) + perPage), [ page, perPage ]);
|
|
48
44
|
|
|
49
45
|
const handleBulkSelect = (value: BulkSelectValue) => {
|
|
50
46
|
value === BulkSelectValue.none && onSelect(false);
|
|
51
|
-
value === BulkSelectValue.all && onSelect(true,
|
|
52
|
-
value === BulkSelectValue.nonePage && onSelect(false,
|
|
53
|
-
value === BulkSelectValue.page && onSelect(true,
|
|
47
|
+
value === BulkSelectValue.all && onSelect(true, rows);
|
|
48
|
+
value === BulkSelectValue.nonePage && onSelect(false, pageRows);
|
|
49
|
+
value === BulkSelectValue.page && onSelect(true, pageRows);
|
|
54
50
|
};
|
|
55
51
|
|
|
56
52
|
return (
|
|
57
|
-
<DataView>
|
|
53
|
+
<DataView selection={selection}>
|
|
58
54
|
<DataViewToolbar
|
|
59
55
|
ouiaId='LayoutExampleHeader'
|
|
60
56
|
bulkSelect={
|
|
61
57
|
<BulkSelect
|
|
62
58
|
canSelectAll
|
|
63
|
-
pageCount={
|
|
59
|
+
pageCount={pageRows.length}
|
|
64
60
|
totalCount={repositories.length}
|
|
65
61
|
selectedCount={selected.length}
|
|
66
|
-
pageSelected={
|
|
67
|
-
pagePartiallySelected={
|
|
62
|
+
pageSelected={pageRows.every(item => isSelected(item))}
|
|
63
|
+
pagePartiallySelected={pageRows.some(item => isSelected(item)) && !pageRows.every(item => isSelected(item))}
|
|
68
64
|
onSelect={handleBulkSelect}
|
|
69
65
|
/>
|
|
70
66
|
}
|
|
@@ -76,34 +72,7 @@ export const BasicExample: React.FunctionComponent = () => {
|
|
|
76
72
|
/>
|
|
77
73
|
}
|
|
78
74
|
/>
|
|
79
|
-
<
|
|
80
|
-
<Thead data-ouia-component-id={`${ouiaId}-thead`}>
|
|
81
|
-
<Tr ouiaId={`${ouiaId}-tr-head`}>
|
|
82
|
-
<Th key="row-select"/>
|
|
83
|
-
{Object.values(cols).map((column, index) => (
|
|
84
|
-
<Th key={index} data-ouia-component-id={`${ouiaId}-th-${index}`}>{column}</Th>
|
|
85
|
-
))}
|
|
86
|
-
</Tr>
|
|
87
|
-
</Thead>
|
|
88
|
-
<Tbody>
|
|
89
|
-
{pageData.map((repo, rowIndex) => (
|
|
90
|
-
<Tr key={repo.name} ouiaId={`${ouiaId}-tr-${rowIndex}`}>
|
|
91
|
-
<Td
|
|
92
|
-
key={`select-${rowIndex}`}
|
|
93
|
-
select={{
|
|
94
|
-
rowIndex,
|
|
95
|
-
onSelect: (_event, isSelecting) => onSelect(isSelecting, repo),
|
|
96
|
-
isSelected: isSelected(repo),
|
|
97
|
-
isDisabled: false
|
|
98
|
-
}}
|
|
99
|
-
/>
|
|
100
|
-
{Object.keys(cols).map((column, colIndex) => (
|
|
101
|
-
<Td key={colIndex} data-ouia-component-id={`${ouiaId}-td-${rowIndex}-${colIndex}`}>{repo[column]}</Td>
|
|
102
|
-
))}
|
|
103
|
-
</Tr>
|
|
104
|
-
))}
|
|
105
|
-
</Tbody>
|
|
106
|
-
</Table>
|
|
75
|
+
<DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={pageRows} />
|
|
107
76
|
<DataViewToolbar
|
|
108
77
|
ouiaId='LayoutExampleFooter'
|
|
109
78
|
pagination={
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { Stack, StackItem } from '@patternfly/react-core';
|
|
3
|
+
import { DataViewSelection, InternalContextProvider } from '../InternalContext';
|
|
4
|
+
|
|
3
5
|
export interface DataViewProps {
|
|
4
6
|
/** Content rendered inside the data view */
|
|
5
7
|
children: React.ReactNode;
|
|
6
8
|
/** Custom OUIA ID */
|
|
7
9
|
ouiaId?: string;
|
|
10
|
+
/** Selection context configuration */
|
|
11
|
+
selection?: DataViewSelection
|
|
8
12
|
}
|
|
9
13
|
|
|
10
|
-
export
|
|
14
|
+
export type DataViewImpementationProps = Omit<DataViewProps, 'onSelect' | 'isItemSelected' | 'isItemSelectDisabled'>;
|
|
15
|
+
|
|
16
|
+
const DataViewImplementation: React.FC<DataViewImpementationProps> = ({
|
|
11
17
|
children, ouiaId = 'DataView', ...props
|
|
12
|
-
}:
|
|
18
|
+
}: DataViewImpementationProps) => (
|
|
13
19
|
<Stack data-ouia-component-id={`${ouiaId}-stack}`} {...props}>
|
|
14
20
|
{React.Children.map(children, (child, index) => (
|
|
15
21
|
<StackItem data-ouia-component-id={`${ouiaId}-stack-item-${index}`}>
|
|
@@ -19,4 +25,10 @@ export const DataView: React.FC<DataViewProps> = ({
|
|
|
19
25
|
</Stack>
|
|
20
26
|
)
|
|
21
27
|
|
|
28
|
+
export const DataView: React.FC<DataViewProps> = ({ children, selection, ...props }: DataViewProps) => (
|
|
29
|
+
<InternalContextProvider selection={selection}>
|
|
30
|
+
<DataViewImplementation {...props}>{children}</DataViewImplementation>
|
|
31
|
+
</InternalContextProvider>
|
|
32
|
+
);
|
|
33
|
+
|
|
22
34
|
export default DataView;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render, fireEvent } from '@testing-library/react';
|
|
3
|
+
import { useDataViewEventsContext, DataViewEventsProvider, EventTypes } from './DataViewEventsContext';
|
|
4
|
+
|
|
5
|
+
let id = 0;
|
|
6
|
+
|
|
7
|
+
beforeAll(() => {
|
|
8
|
+
Object.defineProperty(global, 'crypto', {
|
|
9
|
+
value: {
|
|
10
|
+
randomUUID: jest.fn(() => `mocked-uuid-${id++}`),
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe('DataViewEventsContext', () => {
|
|
16
|
+
test('should provide context value and allow subscriptions', () => {
|
|
17
|
+
const callback = jest.fn();
|
|
18
|
+
|
|
19
|
+
const TestComponent = () => {
|
|
20
|
+
const { subscribe, trigger } = useDataViewEventsContext();
|
|
21
|
+
|
|
22
|
+
React.useEffect(() => {
|
|
23
|
+
const unsubscribe = subscribe(EventTypes.rowClick, callback);
|
|
24
|
+
return () => unsubscribe();
|
|
25
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
26
|
+
}, []);
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<button onClick={() => trigger(EventTypes.rowClick, 'some payload')}>Trigger</button>
|
|
30
|
+
);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const { getByText } = render(
|
|
34
|
+
<DataViewEventsProvider>
|
|
35
|
+
<TestComponent />
|
|
36
|
+
</DataViewEventsProvider>
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
fireEvent.click(getByText('Trigger'));
|
|
40
|
+
expect(callback).toHaveBeenCalledWith('some payload');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('should handle unsubscribing correctly', () => {
|
|
44
|
+
const callback = jest.fn();
|
|
45
|
+
|
|
46
|
+
const TestComponent = () => {
|
|
47
|
+
const { subscribe, trigger } = useDataViewEventsContext();
|
|
48
|
+
|
|
49
|
+
React.useEffect(() => {
|
|
50
|
+
const unsubscribe = subscribe(EventTypes.rowClick, callback);
|
|
51
|
+
unsubscribe();
|
|
52
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
53
|
+
}, []);
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<button onClick={() => trigger(EventTypes.rowClick, 'some payload')}>Trigger</button>
|
|
57
|
+
);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const { getByText } = render(
|
|
61
|
+
<DataViewEventsProvider>
|
|
62
|
+
<TestComponent />
|
|
63
|
+
</DataViewEventsProvider>
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
fireEvent.click(getByText('Trigger'));
|
|
67
|
+
|
|
68
|
+
expect(callback).not.toHaveBeenCalled();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('should handle multiple subscriptions and trigger events correctly', () => {
|
|
72
|
+
const callback1 = jest.fn();
|
|
73
|
+
const callback2 = jest.fn();
|
|
74
|
+
|
|
75
|
+
const TestComponent = () => {
|
|
76
|
+
const { subscribe, trigger } = useDataViewEventsContext();
|
|
77
|
+
|
|
78
|
+
React.useEffect(() => {
|
|
79
|
+
const unsubscribe1 = subscribe(EventTypes.rowClick, callback1);
|
|
80
|
+
const unsubscribe2 = subscribe(EventTypes.rowClick, callback2);
|
|
81
|
+
|
|
82
|
+
return () => {
|
|
83
|
+
unsubscribe1();
|
|
84
|
+
unsubscribe2();
|
|
85
|
+
};
|
|
86
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
87
|
+
}, []);
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<button onClick={() => trigger(EventTypes.rowClick, 'some payload')}>Trigger</button>
|
|
91
|
+
);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const { getByText } = render(
|
|
95
|
+
<DataViewEventsProvider>
|
|
96
|
+
<TestComponent />
|
|
97
|
+
</DataViewEventsProvider>
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
fireEvent.click(getByText('Trigger'));
|
|
101
|
+
|
|
102
|
+
expect(callback1).toHaveBeenCalledWith('some payload');
|
|
103
|
+
expect(callback2).toHaveBeenCalledWith('some payload');
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
PropsWithChildren,
|
|
3
|
+
createContext,
|
|
4
|
+
useCallback,
|
|
5
|
+
useContext,
|
|
6
|
+
useState
|
|
7
|
+
} from "react";
|
|
8
|
+
|
|
9
|
+
export const EventTypes = {
|
|
10
|
+
rowClick: 'rowClick'
|
|
11
|
+
} as const;
|
|
12
|
+
|
|
13
|
+
export type DataViewEvent = typeof EventTypes[keyof typeof EventTypes];
|
|
14
|
+
|
|
15
|
+
type Callback = (...args: any[]) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
16
|
+
interface Subscriptions { [id: string]: Callback }
|
|
17
|
+
type EventSubscriptions = { [event in DataViewEvent]: Subscriptions };
|
|
18
|
+
type Subscribe = (event: DataViewEvent, callback: Callback) => () => void;
|
|
19
|
+
type Trigger = (event: DataViewEvent, ...payload: any[]) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
20
|
+
|
|
21
|
+
export interface DataViewEventsContextValue {
|
|
22
|
+
subscribe: Subscribe;
|
|
23
|
+
trigger: Trigger;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const DataViewEventsContext = createContext<DataViewEventsContextValue>({
|
|
27
|
+
subscribe: () => () => null,
|
|
28
|
+
trigger: () => null
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export const DataViewEventsProvider = ({ children }: PropsWithChildren) => {
|
|
32
|
+
const [ subscriptions, setSubscriptions ] = useState<EventSubscriptions>({
|
|
33
|
+
[EventTypes.rowClick]: {}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const subscribe: Subscribe = (event, callback) => {
|
|
37
|
+
const id = crypto.randomUUID();
|
|
38
|
+
|
|
39
|
+
// set new subscription
|
|
40
|
+
setSubscriptions(prevSubscriptions => ({
|
|
41
|
+
...prevSubscriptions,
|
|
42
|
+
[event]: { ...prevSubscriptions[event], [id]: callback }
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
// return unsubscribe function
|
|
46
|
+
return () => {
|
|
47
|
+
setSubscriptions(prevSubscriptions => {
|
|
48
|
+
const updatedSubscriptions = { ...prevSubscriptions };
|
|
49
|
+
delete updatedSubscriptions[event][id];
|
|
50
|
+
return updatedSubscriptions;
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const trigger = useCallback((event: DataViewEvent, ...payload: unknown[]) => {
|
|
56
|
+
Object.values(subscriptions[event]).forEach(callback => {
|
|
57
|
+
callback(...payload);
|
|
58
|
+
});
|
|
59
|
+
}, [ subscriptions ]);
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<DataViewEventsContext.Provider value={{ subscribe, trigger }}>
|
|
63
|
+
{children}
|
|
64
|
+
</DataViewEventsContext.Provider>
|
|
65
|
+
);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const useDataViewEventsContext = () => useContext(DataViewEventsContext);
|
|
69
|
+
|
|
70
|
+
export default DataViewEventsContext;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
import { DataViewTable } from './DataViewTable';
|
|
4
|
+
|
|
5
|
+
interface Repository {
|
|
6
|
+
name: string;
|
|
7
|
+
branches: string | null;
|
|
8
|
+
prs: string | null;
|
|
9
|
+
workspaces: string;
|
|
10
|
+
lastCommit: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const repositories: Repository[] = [
|
|
14
|
+
{ name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' },
|
|
15
|
+
{ name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
|
|
16
|
+
{ name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' },
|
|
17
|
+
{ name: 'one - 4', branches: 'two - 4', prs: 'null', workspaces: 'four - 4', lastCommit: 'five - 4' },
|
|
18
|
+
{ name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' },
|
|
19
|
+
{ name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
const rows = repositories.map(repo => ({
|
|
23
|
+
row: Object.values(repo),
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
const columns = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ];
|
|
27
|
+
|
|
28
|
+
const ouiaId = 'TableExample';
|
|
29
|
+
|
|
30
|
+
describe('DataViewTable component', () => {
|
|
31
|
+
test('should render correctly', () => {
|
|
32
|
+
const { container } = render(
|
|
33
|
+
<DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={rows} />
|
|
34
|
+
);
|
|
35
|
+
expect(container).toMatchSnapshot();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Table,
|
|
4
|
+
TableProps,
|
|
5
|
+
Tbody,
|
|
6
|
+
Td,
|
|
7
|
+
TdProps,
|
|
8
|
+
Th,
|
|
9
|
+
Thead,
|
|
10
|
+
ThProps,
|
|
11
|
+
Tr,
|
|
12
|
+
TrProps
|
|
13
|
+
} from '@patternfly/react-table';
|
|
14
|
+
import { useInternalContext } from '../InternalContext';
|
|
15
|
+
|
|
16
|
+
export type DataViewTh = ReactNode | { cell: ReactNode; props?: ThProps };
|
|
17
|
+
export type DataViewTd = ReactNode | { cell: ReactNode; props?: TdProps };
|
|
18
|
+
export type DataViewTr = DataViewTd[] | { row: DataViewTd[], id?: string, props?: TrProps };
|
|
19
|
+
|
|
20
|
+
export const isDataViewThObject = (value: DataViewTh): value is { cell: ReactNode; props?: ThProps } => value != null && typeof value === 'object' && 'cell' in value;
|
|
21
|
+
export const isDataViewTdObject = (value: DataViewTd): value is { cell: ReactNode; props?: TdProps } => value != null && typeof value === 'object' && 'cell' in value;
|
|
22
|
+
export const isDataViewTrObject = (value: DataViewTr): value is { row: DataViewTd[], id?: string } => value != null && typeof value === 'object' && 'row' in value;
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export interface DataViewTableProps extends Omit<TableProps, 'onSelect' | 'rows'> {
|
|
26
|
+
/** Columns definition */
|
|
27
|
+
columns: DataViewTh[];
|
|
28
|
+
/** Current page rows */
|
|
29
|
+
rows: DataViewTr[];
|
|
30
|
+
/** Custom OUIA ID */
|
|
31
|
+
ouiaId?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const DataViewTable: React.FC<DataViewTableProps> = ({
|
|
35
|
+
columns,
|
|
36
|
+
rows,
|
|
37
|
+
ouiaId = 'DataViewTable',
|
|
38
|
+
...props
|
|
39
|
+
}: DataViewTableProps) => {
|
|
40
|
+
const { selection } = useInternalContext();
|
|
41
|
+
const { onSelect, isSelected, isSelectDisabled } = selection ?? {};
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<Table aria-label="Data table" ouiaId={ouiaId} {...props}>
|
|
45
|
+
<Thead data-ouia-component-id={`${ouiaId}-thead`}>
|
|
46
|
+
<Tr ouiaId={`${ouiaId}-tr-head`}>
|
|
47
|
+
{onSelect && isSelected && <Th key="row-select" />}
|
|
48
|
+
{columns.map((column, index) => (
|
|
49
|
+
<Th
|
|
50
|
+
key={index}
|
|
51
|
+
{...(isDataViewThObject(column) && (column?.props ?? {}))}
|
|
52
|
+
data-ouia-component-id={`${ouiaId}-th-${index}`}
|
|
53
|
+
>
|
|
54
|
+
{isDataViewThObject(column) ? column.cell : column}
|
|
55
|
+
</Th>
|
|
56
|
+
))}
|
|
57
|
+
</Tr>
|
|
58
|
+
</Thead>
|
|
59
|
+
<Tbody>
|
|
60
|
+
{rows.map((row, rowIndex) => {
|
|
61
|
+
const rowIsObject = isDataViewTrObject(row);
|
|
62
|
+
return (
|
|
63
|
+
<Tr key={rowIndex} ouiaId={`${ouiaId}-tr-${rowIndex}`} {...(rowIsObject && (row?.props ?? {}))}>
|
|
64
|
+
{onSelect && isSelected && (
|
|
65
|
+
<Td
|
|
66
|
+
key={`select-${rowIndex}`}
|
|
67
|
+
select={{
|
|
68
|
+
rowIndex,
|
|
69
|
+
onSelect: (_event, isSelecting) => {
|
|
70
|
+
onSelect?.(isSelecting, rowIsObject ? row : [ row ])
|
|
71
|
+
},
|
|
72
|
+
isSelected: isSelected?.(row) || false,
|
|
73
|
+
isDisabled: isSelectDisabled?.(row) || false,
|
|
74
|
+
}}
|
|
75
|
+
/>
|
|
76
|
+
)}
|
|
77
|
+
{(rowIsObject ? row.row : row).map((cell, colIndex) => {
|
|
78
|
+
const cellIsObject = isDataViewTdObject(cell);
|
|
79
|
+
return (
|
|
80
|
+
<Td
|
|
81
|
+
key={colIndex}
|
|
82
|
+
{...(cellIsObject && (cell?.props ?? {}))}
|
|
83
|
+
data-ouia-component-id={`${ouiaId}-td-${rowIndex}-${colIndex}`}
|
|
84
|
+
>
|
|
85
|
+
{cellIsObject ? cell.cell : cell}
|
|
86
|
+
</Td>
|
|
87
|
+
)
|
|
88
|
+
})}
|
|
89
|
+
</Tr>
|
|
90
|
+
)})}
|
|
91
|
+
</Tbody>
|
|
92
|
+
</Table>
|
|
93
|
+
);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export default DataViewTable;
|