@patternfly/react-data-view 5.1.0 → 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.
Files changed (77) hide show
  1. package/dist/cjs/DataView/DataView.d.ts +4 -0
  2. package/dist/cjs/DataView/DataView.js +7 -1
  3. package/dist/cjs/DataViewEventsContext/DataViewEventsContext.d.ts +16 -0
  4. package/dist/cjs/DataViewEventsContext/DataViewEventsContext.js +62 -0
  5. package/dist/cjs/DataViewEventsContext/DataViewEventsContext.test.d.ts +1 -0
  6. package/dist/cjs/DataViewEventsContext/DataViewEventsContext.test.js +72 -0
  7. package/dist/cjs/DataViewEventsContext/index.d.ts +2 -0
  8. package/dist/cjs/DataViewEventsContext/index.js +23 -0
  9. package/dist/cjs/DataViewTable/DataViewTable.d.ts +37 -0
  10. package/dist/cjs/DataViewTable/DataViewTable.js +59 -0
  11. package/dist/cjs/DataViewTable/DataViewTable.test.d.ts +1 -0
  12. package/dist/cjs/DataViewTable/DataViewTable.test.js +27 -0
  13. package/dist/cjs/DataViewTable/index.d.ts +2 -0
  14. package/dist/cjs/DataViewTable/index.js +23 -0
  15. package/dist/cjs/Hooks/selection.d.ts +3 -3
  16. package/dist/cjs/Hooks/selection.js +4 -6
  17. package/dist/cjs/Hooks/selection.test.js +4 -4
  18. package/dist/cjs/InternalContext/InternalContext.d.ts +17 -0
  19. package/dist/cjs/InternalContext/InternalContext.js +35 -0
  20. package/dist/cjs/InternalContext/InternalContext.test.d.ts +1 -0
  21. package/dist/cjs/InternalContext/InternalContext.test.js +55 -0
  22. package/dist/cjs/InternalContext/index.d.ts +2 -0
  23. package/dist/cjs/InternalContext/index.js +23 -0
  24. package/dist/cjs/index.d.ts +6 -0
  25. package/dist/cjs/index.js +11 -2
  26. package/dist/dynamic/DataViewEventsContext/package.json +1 -0
  27. package/dist/dynamic/DataViewTable/package.json +1 -0
  28. package/dist/dynamic/InternalContext/package.json +1 -0
  29. package/dist/esm/DataView/DataView.d.ts +4 -0
  30. package/dist/esm/DataView/DataView.js +7 -1
  31. package/dist/esm/DataViewEventsContext/DataViewEventsContext.d.ts +16 -0
  32. package/dist/esm/DataViewEventsContext/DataViewEventsContext.js +34 -0
  33. package/dist/esm/DataViewEventsContext/DataViewEventsContext.test.d.ts +1 -0
  34. package/dist/esm/DataViewEventsContext/DataViewEventsContext.test.js +67 -0
  35. package/dist/esm/DataViewEventsContext/index.d.ts +2 -0
  36. package/dist/esm/DataViewEventsContext/index.js +2 -0
  37. package/dist/esm/DataViewTable/DataViewTable.d.ts +37 -0
  38. package/dist/esm/DataViewTable/DataViewTable.js +49 -0
  39. package/dist/esm/DataViewTable/DataViewTable.test.d.ts +1 -0
  40. package/dist/esm/DataViewTable/DataViewTable.test.js +22 -0
  41. package/dist/esm/DataViewTable/index.d.ts +2 -0
  42. package/dist/esm/DataViewTable/index.js +2 -0
  43. package/dist/esm/Hooks/selection.d.ts +3 -3
  44. package/dist/esm/Hooks/selection.js +4 -6
  45. package/dist/esm/Hooks/selection.test.js +4 -4
  46. package/dist/esm/InternalContext/InternalContext.d.ts +17 -0
  47. package/dist/esm/InternalContext/InternalContext.js +7 -0
  48. package/dist/esm/InternalContext/InternalContext.test.d.ts +1 -0
  49. package/dist/esm/InternalContext/InternalContext.test.js +50 -0
  50. package/dist/esm/InternalContext/index.d.ts +2 -0
  51. package/dist/esm/InternalContext/index.js +2 -0
  52. package/dist/esm/index.d.ts +6 -0
  53. package/dist/esm/index.js +6 -0
  54. package/package.json +1 -1
  55. package/patternfly-docs/content/extensions/data-view/examples/Components/Components.md +33 -3
  56. package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableExample.tsx +47 -0
  57. package/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsContext.md +32 -0
  58. package/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsExample.tsx +108 -0
  59. package/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md +6 -5
  60. package/patternfly-docs/content/extensions/data-view/examples/Functionality/PaginationExample.tsx +9 -26
  61. package/patternfly-docs/content/extensions/data-view/examples/Functionality/SelectionExample.tsx +9 -40
  62. package/patternfly-docs/content/extensions/data-view/examples/Layout/Layout.md +1 -0
  63. package/patternfly-docs/content/extensions/data-view/examples/Layout/PredefinedLayoutExample.tsx +14 -45
  64. package/src/DataView/DataView.tsx +14 -2
  65. package/src/DataViewEventsContext/DataViewEventsContext.test.tsx +105 -0
  66. package/src/DataViewEventsContext/DataViewEventsContext.tsx +70 -0
  67. package/src/DataViewEventsContext/index.ts +2 -0
  68. package/src/DataViewTable/DataViewTable.test.tsx +37 -0
  69. package/src/DataViewTable/DataViewTable.tsx +96 -0
  70. package/src/DataViewTable/__snapshots__/DataViewTable.test.tsx.snap +320 -0
  71. package/src/DataViewTable/index.ts +2 -0
  72. package/src/Hooks/selection.test.tsx +5 -5
  73. package/src/Hooks/selection.ts +6 -7
  74. package/src/InternalContext/InternalContext.test.tsx +88 -0
  75. package/src/InternalContext/InternalContext.tsx +35 -0
  76. package/src/InternalContext/index.ts +2 -0
  77. package/src/index.ts +9 -0
@@ -0,0 +1,32 @@
1
+ ---
2
+ # Sidenav top-level section
3
+ # should be the same for all markdown files
4
+ section: extensions
5
+ subsection: Data view
6
+ # Sidenav secondary level section
7
+ # should be the same for all markdown files
8
+ id: Events context
9
+ # Tab (react | react-demos | html | html-demos | design-guidelines | accessibility)
10
+ source: react
11
+ # If you use typescript, the name of the interface to display props for
12
+ # These are found through the sourceProps function provided in patternfly-docs.source.js
13
+ sortValue: 3
14
+ sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsContext.md
15
+ ---
16
+ import { useState, useEffect, useRef, useMemo } from 'react';
17
+ import { Table, Tbody, Th, Thead, Tr, Td } from '@patternfly/react-table';
18
+ import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
19
+ import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
20
+ import { useDataViewEventsContext, DataViewEventsContext, DataViewEventsProvider, EventTypes } from '@patternfly/react-data-view/dist/dynamic/DataViewEventsContext';
21
+ import { Drawer, DrawerContent, DrawerContentBody } from '@patternfly/react-core';
22
+
23
+ The **data view events context** provides a way of listening to the data view events from the outside of the component.
24
+
25
+ ### Row click subscription example
26
+ The following example demonstrates how to use the `DataViewEventsContext` to manage shared state and handle events. The `DataViewEventsProvider` is used to wrap components that need access to the shared context. This example illustrates how to set up a layout that listens for data view row click events and displays detailed information about the selected row in a [drawer component](/components/drawer).
27
+
28
+
29
+ ```js file="./EventsExample.tsx"
30
+
31
+ ```
32
+
@@ -0,0 +1,108 @@
1
+ import React, { useEffect, useState, useRef, useMemo } from 'react';
2
+ import { Drawer, DrawerActions, DrawerCloseButton, DrawerContent, DrawerContentBody, DrawerHead, DrawerPanelContent, Title, Text } from '@patternfly/react-core';
3
+ import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
4
+ import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
5
+ import { DataViewEventsProvider, EventTypes, useDataViewEventsContext } from '@patternfly/react-data-view/dist/dynamic/DataViewEventsContext';
6
+
7
+ interface Repository {
8
+ name: string;
9
+ branches: string | null;
10
+ prs: string | null;
11
+ workspaces: string;
12
+ lastCommit: string;
13
+ }
14
+
15
+ const repositories: Repository[] = [
16
+ { name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' },
17
+ { name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
18
+ { name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' },
19
+ { name: 'one - 4', branches: 'two - 4', prs: 'null', workspaces: 'four - 4', lastCommit: 'five - 4' },
20
+ { name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' },
21
+ { name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
22
+ ];
23
+
24
+ const columns = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ];
25
+
26
+ const ouiaId = 'ContextExample';
27
+
28
+ interface RepositoryDetailProps {
29
+ selectedRepo?: Repository;
30
+ setSelectedRepo: React.Dispatch<React.SetStateAction<Repository | undefined>>;
31
+ }
32
+
33
+ const RepositoryDetail: React.FunctionComponent<RepositoryDetailProps> = ({ selectedRepo, setSelectedRepo }) => {
34
+ const context = useDataViewEventsContext();
35
+
36
+ useEffect(() => {
37
+ const unsubscribe = context.subscribe(EventTypes.rowClick, (repo: Repository) => {
38
+ setSelectedRepo(repo);
39
+ });
40
+
41
+ return () => unsubscribe();
42
+ // eslint-disable-next-line react-hooks/exhaustive-deps
43
+ }, []);
44
+
45
+ return (
46
+ <DrawerPanelContent>
47
+ <DrawerHead>
48
+ <Title className="pf-v5-u-mb-md" headingLevel="h2" ouiaId="detail-drawer-title">
49
+ Detail of repository {selectedRepo?.name}
50
+ </Title>
51
+ <Text>Branches: {selectedRepo?.branches}</Text>
52
+ <Text>Pull requests: {selectedRepo?.prs}</Text>
53
+ <Text>Workspaces: {selectedRepo?.workspaces}</Text>
54
+ <Text>Last commit: {selectedRepo?.lastCommit}</Text>
55
+ <DrawerActions>
56
+ <DrawerCloseButton onClick={() => setSelectedRepo(undefined)} data-ouia-component-id="detail-drawer-close-btn"/>
57
+ </DrawerActions>
58
+ </DrawerHead>
59
+ </DrawerPanelContent>
60
+ );
61
+ };
62
+
63
+ interface RepositoriesTableProps {
64
+ selectedRepo?: Repository;
65
+ }
66
+
67
+ const RepositoriesTable: React.FunctionComponent<RepositoriesTableProps> = ({ selectedRepo = undefined }) => {
68
+ const { trigger } = useDataViewEventsContext();
69
+ const rows = useMemo(() => {
70
+ const handleRowClick = (repo: Repository | undefined) => {
71
+ trigger(EventTypes.rowClick, repo);
72
+ };
73
+
74
+ return repositories.map(repo => ({
75
+ row: Object.values(repo),
76
+ props: {
77
+ isClickable: true,
78
+ onRowClick: () => handleRowClick(selectedRepo?.name === repo.name ? undefined : repo),
79
+ isRowSelected: selectedRepo?.name === repo.name
80
+ }
81
+ }));
82
+ }, [ selectedRepo?.name, trigger ]);
83
+
84
+ return (
85
+ <DataView>
86
+ <DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={rows} />
87
+ </DataView>
88
+ );
89
+ };
90
+
91
+ export const BasicExample: React.FunctionComponent = () => {
92
+ const [ selectedRepo, setSelectedRepo ] = useState<Repository>();
93
+ const drawerRef = useRef<HTMLDivElement>(null);
94
+
95
+ return (
96
+ <DataViewEventsProvider>
97
+ <Drawer isExpanded={Boolean(selectedRepo)} onExpand={() => drawerRef.current?.focus()} data-ouia-component-id="detail-drawer" >
98
+ <DrawerContent
99
+ panelContent={<RepositoryDetail selectedRepo={selectedRepo} setSelectedRepo={setSelectedRepo} />}
100
+ >
101
+ <DrawerContentBody>
102
+ <RepositoriesTable selectedRepo={selectedRepo} />
103
+ </DrawerContentBody>
104
+ </DrawerContent>
105
+ </Drawer>
106
+ </DataViewEventsProvider>
107
+ );
108
+ };
@@ -18,6 +18,7 @@ import { useDataViewPagination, useDataViewSelection } from '@patternfly/react-d
18
18
  import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
19
19
  import { BulkSelect, BulkSelectValue } from '@patternfly/react-component-groups/dist/dynamic/BulkSelect';
20
20
  import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
21
+ import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
21
22
  import { BrowserRouter, useSearchParams } from 'react-router-dom';
22
23
 
23
24
  This is a list of functionality you can use to manage data displayed in the **data view**.
@@ -34,9 +35,9 @@ The `useDataViewPagination` hook manages the pagination state of the data view.
34
35
 
35
36
  **Initial values:**
36
37
  - `perPage` initial value
37
- - (optional) `page` initial value
38
- - (optional) `searchParams` object
39
- - (optional) `seSearchParams` function
38
+ - optional `page` initial value
39
+ - optional `searchParams` object
40
+ - optional `setSearchParams` function
40
41
 
41
42
  While the hook works seamlessly with React Router library, you do not need to use it to take advantage of URL persistence. The `searchParams` and `setSearchParams` props can be managed using native browser APIs (`URLSearchParams` and `window.history.pushState`) or any other routing library of your choice. If you don't pass these two props, the pagination state will be stored internally without the URL usage.
42
43
 
@@ -68,8 +69,8 @@ Data view toolbar can display a bulk selection component using the `bulkSelect`
68
69
  The `useDataViewSelection` hook manages the selection state of the data view.
69
70
 
70
71
  **Initial values:**
71
- - (optional) `initialSelected` array of record's identifiers selected by default
72
- - (optional) `matchOption` function to check if given record is selected
72
+ - optional `initialSelected` array of record's identifiers selected by default
73
+ - `matchOption` function to check if given record is selected
73
74
 
74
75
  *When no `matchOption` is passed, the `Array.prototype.includes()` operation is performed on the `selected` array.*
75
76
 
@@ -1,10 +1,10 @@
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
- import { useDataViewPagination } from '@patternfly/react-data-view/dist/dynamic/Hooks';
5
- import DataView from '@patternfly/react-data-view/dist/dynamic/DataView';
6
- import DataViewToolbar from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
7
3
  import { BrowserRouter, useSearchParams } from 'react-router-dom';
4
+ import { useDataViewPagination } from '@patternfly/react-data-view/dist/dynamic/Hooks';
5
+ import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
6
+ import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
7
+ import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
8
8
 
9
9
  const perPageOptions = [
10
10
  { title: '5', value: 5 },
@@ -28,13 +28,9 @@ 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 cols = {
32
- name: 'Repositories',
33
- branches: 'Branches',
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
 
@@ -43,24 +39,11 @@ const MyTable: React.FunctionComponent = () => {
43
39
  const pagination = useDataViewPagination({ perPage: 5, searchParams, setSearchParams });
44
40
  const { page, perPage } = pagination;
45
41
 
46
- const data = useMemo(() => repositories.slice((page - 1) * perPage, ((page - 1) * perPage) + perPage), [ page, perPage ]);
42
+ const pageRows = useMemo(() => rows.slice((page - 1) * perPage, ((page - 1) * perPage) + perPage), [ page, perPage ]);
47
43
  return (
48
44
  <DataView>
49
45
  <DataViewToolbar ouiaId='DataViewHeader' pagination={<Pagination perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} />
50
- <Table aria-label="Repositories table" ouiaId={ouiaId}>
51
- <Thead data-ouia-component-id={`${ouiaId}-thead`}>
52
- <Tr ouiaId={`${ouiaId}-tr-head`}>
53
- {Object.values(cols).map((column, index) => <Th key={index} data-ouia-component-id={`${ouiaId}-th-${index}`}>{column}</Th>)}
54
- </Tr>
55
- </Thead>
56
- <Tbody>
57
- {data.map((repo, rowIndex) => (
58
- <Tr key={repo.name} ouiaId={`${ouiaId}-tr-${rowIndex}`}>
59
- {Object.keys(cols).map((column, colIndex) => <Td key={colIndex} data-ouia-component-id={`${ouiaId}-td-${rowIndex}-${colIndex}`}>{repo[column]}</Td>)}
60
- </Tr>
61
- ))}
62
- </Tbody>
63
- </Table>
46
+ <DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={pageRows} />
64
47
  <DataViewToolbar ouiaId='DataViewFooter' pagination={<Pagination isCompact perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} />
65
48
  </DataView>
66
49
  )
@@ -1,9 +1,9 @@
1
1
  import React from 'react';
2
- import { Table, Tbody, Th, Thead, Tr, Td } from '@patternfly/react-table';
3
2
  import { useDataViewSelection } from '@patternfly/react-data-view/dist/dynamic/Hooks';
4
3
  import { BulkSelect, BulkSelectValue } from '@patternfly/react-component-groups/dist/dynamic/BulkSelect';
5
4
  import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
6
5
  import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
6
+ import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
7
7
 
8
8
  interface Repository {
9
9
  name: string;
@@ -22,27 +22,23 @@ const repositories: Repository[] = [
22
22
  { name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
23
23
  ];
24
24
 
25
- const cols = {
26
- name: 'Repositories',
27
- branches: 'Branches',
28
- prs: 'Pull requests',
29
- workspaces: 'Workspaces',
30
- lastCommit: 'Last commit'
31
- };
25
+ const rows = repositories.map(item => Object.values(item));
26
+
27
+ const columns = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ];
32
28
 
33
29
  const ouiaId = 'LayoutExample';
34
30
 
35
31
  export const BasicExample: React.FunctionComponent = () => {
36
- const selection = useDataViewSelection({});
37
- const { selected, onSelect, isSelected } = selection;
32
+ const selection = useDataViewSelection({ matchOption: (a, b) => a[0] === b[0] });
33
+ const { selected, onSelect } = selection;
38
34
 
39
35
  const handleBulkSelect = (value: BulkSelectValue) => {
40
36
  value === BulkSelectValue.none && onSelect(false);
41
- value === BulkSelectValue.all && onSelect(true, repositories);
37
+ value === BulkSelectValue.all && onSelect(true, rows);
42
38
  };
43
39
 
44
40
  return (
45
- <DataView>
41
+ <DataView selection={selection}>
46
42
  <DataViewToolbar
47
43
  ouiaId='DataViewHeader'
48
44
  bulkSelect={
@@ -55,34 +51,7 @@ export const BasicExample: React.FunctionComponent = () => {
55
51
  />
56
52
  }
57
53
  />
58
- <Table aria-label="Repositories table" ouiaId={ouiaId}>
59
- <Thead data-ouia-component-id={`${ouiaId}-thead`}>
60
- <Tr ouiaId={`${ouiaId}-tr-head`}>
61
- <Th key="row-select"/>
62
- {Object.values(cols).map((column, index) => (
63
- <Th key={index} data-ouia-component-id={`${ouiaId}-th-${index}`}>{column}</Th>
64
- ))}
65
- </Tr>
66
- </Thead>
67
- <Tbody>
68
- {repositories.map((repo, rowIndex) => (
69
- <Tr key={repo.name} ouiaId={`${ouiaId}-tr-${rowIndex}`}>
70
- <Td
71
- key={`select-${rowIndex}`}
72
- select={{
73
- rowIndex,
74
- onSelect: (_event, isSelecting) => onSelect(isSelecting, repo),
75
- isSelected: isSelected(repo),
76
- isDisabled: false
77
- }}
78
- />
79
- {Object.keys(cols).map((column, colIndex) => (
80
- <Td key={colIndex} data-ouia-component-id={`${ouiaId}-td-${rowIndex}-${colIndex}`}>{repo[column]}</Td>
81
- ))}
82
- </Tr>
83
- ))}
84
- </Tbody>
85
- </Table>
54
+ <DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={rows} />
86
55
  </DataView>
87
56
  );
88
57
  }
@@ -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
 
@@ -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 cols = {
32
- name: 'Repositories',
33
- branches: 'Branches',
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 pageData = useMemo(() => repositories.slice((page - 1) * perPage, ((page - 1) * perPage) + perPage), [ page, perPage ]);
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, repositories);
52
- value === BulkSelectValue.nonePage && onSelect(false, pageData);
53
- value === BulkSelectValue.page && onSelect(true, pageData);
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={pageData.length}
59
+ pageCount={pageRows.length}
64
60
  totalCount={repositories.length}
65
61
  selectedCount={selected.length}
66
- pageSelected={pageData.every(item => isSelected(item))}
67
- pagePartiallySelected={pageData.some(item => isSelected(item)) && !pageData.every(item => isSelected(item))}
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
- <Table aria-label="Repositories table" ouiaId={ouiaId}>
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 const DataView: React.FC<DataViewProps> = ({
14
+ export type DataViewImpementationProps = Omit<DataViewProps, 'onSelect' | 'isItemSelected' | 'isItemSelectDisabled'>;
15
+
16
+ const DataViewImplementation: React.FC<DataViewImpementationProps> = ({
11
17
  children, ouiaId = 'DataView', ...props
12
- }: DataViewProps) => (
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,2 @@
1
+ export { default } from './DataViewEventsContext';
2
+ export * from './DataViewEventsContext';