@patternfly/react-data-view 5.1.0 → 5.1.2

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 (129) 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 +41 -0
  10. package/dist/cjs/DataViewTable/DataViewTable.js +18 -0
  11. package/dist/cjs/DataViewTable/DataViewTable.test.d.ts +1 -0
  12. package/dist/cjs/DataViewTable/DataViewTable.test.js +57 -0
  13. package/dist/cjs/DataViewTable/index.d.ts +2 -0
  14. package/dist/cjs/DataViewTable/index.js +23 -0
  15. package/dist/cjs/DataViewTableBasic/DataViewTableBasic.d.ts +13 -0
  16. package/dist/cjs/DataViewTableBasic/DataViewTableBasic.js +49 -0
  17. package/dist/cjs/DataViewTableBasic/DataViewTableBasic.test.d.ts +1 -0
  18. package/dist/cjs/DataViewTableBasic/DataViewTableBasic.test.js +27 -0
  19. package/dist/cjs/DataViewTableBasic/index.d.ts +2 -0
  20. package/dist/cjs/DataViewTableBasic/index.js +23 -0
  21. package/dist/cjs/DataViewTableHeader/DataViewTableHeader.d.ts +13 -0
  22. package/dist/cjs/DataViewTableHeader/DataViewTableHeader.js +57 -0
  23. package/dist/cjs/DataViewTableHeader/DataViewTableHeader.test.d.ts +1 -0
  24. package/dist/cjs/DataViewTableHeader/DataViewTableHeader.test.js +36 -0
  25. package/dist/cjs/DataViewTableHeader/index.d.ts +2 -0
  26. package/dist/cjs/DataViewTableHeader/index.js +23 -0
  27. package/dist/cjs/DataViewTableTree/DataViewTableTree.d.ts +19 -0
  28. package/dist/cjs/DataViewTableTree/DataViewTableTree.js +120 -0
  29. package/dist/cjs/DataViewTableTree/DataViewTableTree.test.d.ts +1 -0
  30. package/dist/cjs/DataViewTableTree/DataViewTableTree.test.js +75 -0
  31. package/dist/cjs/DataViewTableTree/index.d.ts +2 -0
  32. package/dist/cjs/DataViewTableTree/index.js +23 -0
  33. package/dist/cjs/Hooks/selection.d.ts +3 -3
  34. package/dist/cjs/Hooks/selection.js +4 -6
  35. package/dist/cjs/Hooks/selection.test.js +4 -4
  36. package/dist/cjs/InternalContext/InternalContext.d.ts +17 -0
  37. package/dist/cjs/InternalContext/InternalContext.js +35 -0
  38. package/dist/cjs/InternalContext/InternalContext.test.d.ts +1 -0
  39. package/dist/cjs/InternalContext/InternalContext.test.js +56 -0
  40. package/dist/cjs/InternalContext/index.d.ts +2 -0
  41. package/dist/cjs/InternalContext/index.js +23 -0
  42. package/dist/cjs/index.d.ts +12 -0
  43. package/dist/cjs/index.js +20 -2
  44. package/dist/dynamic/DataViewEventsContext/package.json +1 -0
  45. package/dist/dynamic/DataViewTable/package.json +1 -0
  46. package/dist/dynamic/DataViewTableBasic/package.json +1 -0
  47. package/dist/dynamic/DataViewTableHeader/package.json +1 -0
  48. package/dist/dynamic/DataViewTableTree/package.json +1 -0
  49. package/dist/dynamic/InternalContext/package.json +1 -0
  50. package/dist/esm/DataView/DataView.d.ts +4 -0
  51. package/dist/esm/DataView/DataView.js +7 -1
  52. package/dist/esm/DataViewEventsContext/DataViewEventsContext.d.ts +16 -0
  53. package/dist/esm/DataViewEventsContext/DataViewEventsContext.js +34 -0
  54. package/dist/esm/DataViewEventsContext/DataViewEventsContext.test.d.ts +1 -0
  55. package/dist/esm/DataViewEventsContext/DataViewEventsContext.test.js +67 -0
  56. package/dist/esm/DataViewEventsContext/index.d.ts +2 -0
  57. package/dist/esm/DataViewEventsContext/index.js +2 -0
  58. package/dist/esm/DataViewTable/DataViewTable.d.ts +41 -0
  59. package/dist/esm/DataViewTable/DataViewTable.js +8 -0
  60. package/dist/esm/DataViewTable/DataViewTable.test.d.ts +1 -0
  61. package/dist/esm/DataViewTable/DataViewTable.test.js +52 -0
  62. package/dist/esm/DataViewTable/index.d.ts +2 -0
  63. package/dist/esm/DataViewTable/index.js +2 -0
  64. package/dist/esm/DataViewTableBasic/DataViewTableBasic.d.ts +13 -0
  65. package/dist/esm/DataViewTableBasic/DataViewTableBasic.js +42 -0
  66. package/dist/esm/DataViewTableBasic/DataViewTableBasic.test.d.ts +1 -0
  67. package/dist/esm/DataViewTableBasic/DataViewTableBasic.test.js +22 -0
  68. package/dist/esm/DataViewTableBasic/index.d.ts +2 -0
  69. package/dist/esm/DataViewTableBasic/index.js +2 -0
  70. package/dist/esm/DataViewTableHeader/DataViewTableHeader.d.ts +13 -0
  71. package/dist/esm/DataViewTableHeader/DataViewTableHeader.js +30 -0
  72. package/dist/esm/DataViewTableHeader/DataViewTableHeader.test.d.ts +1 -0
  73. package/dist/esm/DataViewTableHeader/DataViewTableHeader.test.js +31 -0
  74. package/dist/esm/DataViewTableHeader/index.d.ts +2 -0
  75. package/dist/esm/DataViewTableHeader/index.js +2 -0
  76. package/dist/esm/DataViewTableTree/DataViewTableTree.d.ts +19 -0
  77. package/dist/esm/DataViewTableTree/DataViewTableTree.js +93 -0
  78. package/dist/esm/DataViewTableTree/DataViewTableTree.test.d.ts +1 -0
  79. package/dist/esm/DataViewTableTree/DataViewTableTree.test.js +70 -0
  80. package/dist/esm/DataViewTableTree/index.d.ts +2 -0
  81. package/dist/esm/DataViewTableTree/index.js +2 -0
  82. package/dist/esm/Hooks/selection.d.ts +3 -3
  83. package/dist/esm/Hooks/selection.js +4 -6
  84. package/dist/esm/Hooks/selection.test.js +4 -4
  85. package/dist/esm/InternalContext/InternalContext.d.ts +17 -0
  86. package/dist/esm/InternalContext/InternalContext.js +7 -0
  87. package/dist/esm/InternalContext/InternalContext.test.d.ts +1 -0
  88. package/dist/esm/InternalContext/InternalContext.test.js +51 -0
  89. package/dist/esm/InternalContext/index.d.ts +2 -0
  90. package/dist/esm/InternalContext/index.js +2 -0
  91. package/dist/esm/index.d.ts +12 -0
  92. package/dist/esm/index.js +12 -0
  93. package/package.json +1 -1
  94. package/patternfly-docs/content/extensions/data-view/examples/Components/Components.md +45 -3
  95. package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableExample.tsx +47 -0
  96. package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableTreeExample.tsx +69 -0
  97. package/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsContext.md +32 -0
  98. package/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsExample.tsx +108 -0
  99. package/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md +6 -5
  100. package/patternfly-docs/content/extensions/data-view/examples/Functionality/PaginationExample.tsx +15 -32
  101. package/patternfly-docs/content/extensions/data-view/examples/Functionality/SelectionExample.tsx +15 -46
  102. package/patternfly-docs/content/extensions/data-view/examples/Layout/Layout.md +1 -0
  103. package/patternfly-docs/content/extensions/data-view/examples/Layout/PredefinedLayoutExample.tsx +20 -51
  104. package/src/DataView/DataView.tsx +14 -2
  105. package/src/DataViewEventsContext/DataViewEventsContext.test.tsx +105 -0
  106. package/src/DataViewEventsContext/DataViewEventsContext.tsx +70 -0
  107. package/src/DataViewEventsContext/index.ts +2 -0
  108. package/src/DataViewTable/DataViewTable.test.tsx +80 -0
  109. package/src/DataViewTable/DataViewTable.tsx +37 -0
  110. package/src/DataViewTable/__snapshots__/DataViewTable.test.tsx.snap +1010 -0
  111. package/src/DataViewTable/index.ts +2 -0
  112. package/src/DataViewTableBasic/DataViewTableBasic.test.tsx +37 -0
  113. package/src/DataViewTableBasic/DataViewTableBasic.tsx +71 -0
  114. package/src/DataViewTableBasic/__snapshots__/DataViewTableBasic.test.tsx.snap +324 -0
  115. package/src/DataViewTableBasic/index.ts +2 -0
  116. package/src/DataViewTableHeader/DataViewTableHeader.test.tsx +52 -0
  117. package/src/DataViewTableHeader/DataViewTableHeader.tsx +53 -0
  118. package/src/DataViewTableHeader/__snapshots__/DataViewTableHeader.test.tsx.snap +227 -0
  119. package/src/DataViewTableHeader/index.ts +2 -0
  120. package/src/DataViewTableTree/DataViewTableTree.test.tsx +85 -0
  121. package/src/DataViewTableTree/DataViewTableTree.tsx +144 -0
  122. package/src/DataViewTableTree/__snapshots__/DataViewTableTree.test.tsx.snap +937 -0
  123. package/src/DataViewTableTree/index.ts +2 -0
  124. package/src/Hooks/selection.test.tsx +5 -5
  125. package/src/Hooks/selection.ts +6 -7
  126. package/src/InternalContext/InternalContext.test.tsx +89 -0
  127. package/src/InternalContext/InternalContext.tsx +35 -0
  128. package/src/InternalContext/index.ts +2 -0
  129. package/src/index.ts +18 -0
@@ -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;
@@ -14,35 +14,31 @@ interface Repository {
14
14
  }
15
15
 
16
16
  const repositories: Repository[] = [
17
- { name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' },
18
- { name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
19
- { name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' },
20
- { name: 'one - 4', branches: 'two - 4', prs: 'null', workspaces: 'four - 4', lastCommit: 'five - 4' },
21
- { name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' },
22
- { name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
17
+ { name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' },
18
+ { name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
19
+ { name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' },
20
+ { name: 'Repository four', branches: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' },
21
+ { name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' },
22
+ { name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' }
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 = [
@@ -20,51 +20,47 @@ interface Repository {
20
20
  }
21
21
 
22
22
  const repositories: Repository[] = [
23
- { name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' },
24
- { name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
25
- { name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' },
26
- { name: 'one - 4', branches: 'two - 4', prs: 'null', workspaces: 'four - 4', lastCommit: 'five - 4' },
27
- { name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' },
28
- { name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
23
+ { name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' },
24
+ { name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
25
+ { name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' },
26
+ { name: 'Repository four', branches: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' },
27
+ { name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' },
28
+ { name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' }
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';
@@ -0,0 +1,80 @@
1
+ import React from 'react';
2
+ import { render } from '@testing-library/react';
3
+ import { DataViewTable, DataViewTrTree } from './DataViewTable';
4
+
5
+ interface Repository {
6
+ name: string;
7
+ branches: string | null;
8
+ prs: string | null;
9
+ workspaces: string;
10
+ lastCommit: string;
11
+ children?: Repository[];
12
+ }
13
+
14
+ const repositories: Repository[] = [
15
+ { name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' },
16
+ { name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
17
+ { name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' },
18
+ { name: 'Repository four', branches: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' },
19
+ { name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' },
20
+ { name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' }
21
+ ];
22
+
23
+ const rows = repositories.map(repo => ({ row: Object.values(repo) }));
24
+
25
+ const repositoriesTree: Repository[] = [
26
+ {
27
+ name: 'Repository one',
28
+ branches: 'Branch one',
29
+ prs: 'Pull request one',
30
+ workspaces: 'Workspace one',
31
+ lastCommit: 'Timestamp one',
32
+ children: [
33
+ { name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
34
+ { name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' },
35
+ ]
36
+ },
37
+ {
38
+ name: 'Repository four',
39
+ branches: 'Branch four',
40
+ prs: 'Pull request four',
41
+ workspaces: 'Workspace four',
42
+ lastCommit: 'Timestamp four',
43
+ children: [ { name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' } ]
44
+ },
45
+ { name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' }
46
+ ];
47
+
48
+
49
+
50
+ const buildRows = (repositories: Repository[]): DataViewTrTree[] => repositories.map((repo) => ({
51
+ row: [ repo.name, repo.branches, repo.prs, repo.workspaces, repo.lastCommit ],
52
+ id: repo.name, // unique ID for each row
53
+ ...(repo.children
54
+ ? {
55
+ children: buildRows(repo.children) // build rows for children
56
+ }
57
+ : {})
58
+ }));
59
+
60
+ const treeRows = buildRows(repositoriesTree);
61
+
62
+ const columns = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ];
63
+
64
+ const ouiaId = 'TableExample';
65
+
66
+ describe('DataViewTable component', () => {
67
+ test('should render a basic table correctly', () => {
68
+ const { container } = render(
69
+ <DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={rows} />
70
+ );
71
+ expect(container).toMatchSnapshot();
72
+ });
73
+
74
+ test('should render a tree table correctly', () => {
75
+ const { container } = render(
76
+ <DataViewTable isTreeTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={treeRows} />
77
+ );
78
+ expect(container).toMatchSnapshot();
79
+ });
80
+ });
@@ -0,0 +1,37 @@
1
+ import React, { ReactNode } from 'react';
2
+ import {
3
+ TdProps,
4
+ ThProps,
5
+ TrProps
6
+ } from '@patternfly/react-table';
7
+ import { DataViewTableTree, DataViewTableTreeProps } from '../DataViewTableTree';
8
+ import { DataViewTableBasic, DataViewTableBasicProps } from '../DataViewTableBasic';
9
+
10
+ // Table header typings
11
+ export type DataViewTh = ReactNode | { cell: ReactNode; props?: ThProps };
12
+ export const isDataViewThObject = (value: DataViewTh): value is { cell: ReactNode; props?: ThProps } => value != null && typeof value === 'object' && 'cell' in value;
13
+
14
+ // Basic table typings
15
+ export interface DataViewTrObject { row: DataViewTd[], id?: string, props?: TrProps }
16
+ export type DataViewTd = ReactNode | { cell: ReactNode; props?: TdProps };
17
+ export type DataViewTr = DataViewTd[] | DataViewTrObject;
18
+
19
+ export const isDataViewTdObject = (value: DataViewTd): value is { cell: ReactNode; props?: TdProps } => value != null && typeof value === 'object' && 'cell' in value;
20
+ export const isDataViewTrObject = (value: DataViewTr): value is { row: DataViewTd[], id?: string } => value != null && typeof value === 'object' && 'row' in value;
21
+
22
+ // Tree table typings
23
+ export interface DataViewTrTree extends DataViewTrObject { id: string, children?: DataViewTrTree[] }
24
+
25
+ export type DataViewTableProps =
26
+ | ({
27
+ isTreeTable: true;
28
+ } & DataViewTableTreeProps)
29
+ | ({
30
+ isTreeTable?: false;
31
+ } & DataViewTableBasicProps);
32
+
33
+ export const DataViewTable: React.FC<DataViewTableProps> = (props) => (
34
+ props.isTreeTable ? <DataViewTableTree {...props} /> : <DataViewTableBasic {...props} />
35
+ );
36
+
37
+ export default DataViewTable;