@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.
Files changed (85) 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/pagination.d.ts +13 -1
  16. package/dist/cjs/Hooks/pagination.js +36 -4
  17. package/dist/cjs/Hooks/pagination.test.js +53 -1
  18. package/dist/cjs/Hooks/selection.d.ts +3 -3
  19. package/dist/cjs/Hooks/selection.js +4 -6
  20. package/dist/cjs/Hooks/selection.test.js +4 -4
  21. package/dist/cjs/InternalContext/InternalContext.d.ts +17 -0
  22. package/dist/cjs/InternalContext/InternalContext.js +35 -0
  23. package/dist/cjs/InternalContext/InternalContext.test.d.ts +1 -0
  24. package/dist/cjs/InternalContext/InternalContext.test.js +55 -0
  25. package/dist/cjs/InternalContext/index.d.ts +2 -0
  26. package/dist/cjs/InternalContext/index.js +23 -0
  27. package/dist/cjs/index.d.ts +6 -0
  28. package/dist/cjs/index.js +11 -2
  29. package/dist/dynamic/DataViewEventsContext/package.json +1 -0
  30. package/dist/dynamic/DataViewTable/package.json +1 -0
  31. package/dist/dynamic/InternalContext/package.json +1 -0
  32. package/dist/esm/DataView/DataView.d.ts +4 -0
  33. package/dist/esm/DataView/DataView.js +7 -1
  34. package/dist/esm/DataViewEventsContext/DataViewEventsContext.d.ts +16 -0
  35. package/dist/esm/DataViewEventsContext/DataViewEventsContext.js +34 -0
  36. package/dist/esm/DataViewEventsContext/DataViewEventsContext.test.d.ts +1 -0
  37. package/dist/esm/DataViewEventsContext/DataViewEventsContext.test.js +67 -0
  38. package/dist/esm/DataViewEventsContext/index.d.ts +2 -0
  39. package/dist/esm/DataViewEventsContext/index.js +2 -0
  40. package/dist/esm/DataViewTable/DataViewTable.d.ts +37 -0
  41. package/dist/esm/DataViewTable/DataViewTable.js +49 -0
  42. package/dist/esm/DataViewTable/DataViewTable.test.d.ts +1 -0
  43. package/dist/esm/DataViewTable/DataViewTable.test.js +22 -0
  44. package/dist/esm/DataViewTable/index.d.ts +2 -0
  45. package/dist/esm/DataViewTable/index.js +2 -0
  46. package/dist/esm/Hooks/pagination.d.ts +13 -1
  47. package/dist/esm/Hooks/pagination.js +36 -4
  48. package/dist/esm/Hooks/pagination.test.js +53 -1
  49. package/dist/esm/Hooks/selection.d.ts +3 -3
  50. package/dist/esm/Hooks/selection.js +4 -6
  51. package/dist/esm/Hooks/selection.test.js +4 -4
  52. package/dist/esm/InternalContext/InternalContext.d.ts +17 -0
  53. package/dist/esm/InternalContext/InternalContext.js +7 -0
  54. package/dist/esm/InternalContext/InternalContext.test.d.ts +1 -0
  55. package/dist/esm/InternalContext/InternalContext.test.js +50 -0
  56. package/dist/esm/InternalContext/index.d.ts +2 -0
  57. package/dist/esm/InternalContext/index.js +2 -0
  58. package/dist/esm/index.d.ts +6 -0
  59. package/dist/esm/index.js +6 -0
  60. package/package.json +1 -1
  61. package/patternfly-docs/content/extensions/data-view/examples/Components/Components.md +33 -3
  62. package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableExample.tsx +47 -0
  63. package/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsContext.md +32 -0
  64. package/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsExample.tsx +108 -0
  65. package/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md +15 -5
  66. package/patternfly-docs/content/extensions/data-view/examples/Functionality/PaginationExample.tsx +20 -29
  67. package/patternfly-docs/content/extensions/data-view/examples/Functionality/SelectionExample.tsx +9 -40
  68. package/patternfly-docs/content/extensions/data-view/examples/Layout/Layout.md +1 -0
  69. package/patternfly-docs/content/extensions/data-view/examples/Layout/PredefinedLayoutExample.tsx +14 -45
  70. package/src/DataView/DataView.tsx +14 -2
  71. package/src/DataViewEventsContext/DataViewEventsContext.test.tsx +105 -0
  72. package/src/DataViewEventsContext/DataViewEventsContext.tsx +70 -0
  73. package/src/DataViewEventsContext/index.ts +2 -0
  74. package/src/DataViewTable/DataViewTable.test.tsx +37 -0
  75. package/src/DataViewTable/DataViewTable.tsx +96 -0
  76. package/src/DataViewTable/__snapshots__/DataViewTable.test.tsx.snap +320 -0
  77. package/src/DataViewTable/index.ts +2 -0
  78. package/src/Hooks/pagination.test.tsx +81 -1
  79. package/src/Hooks/pagination.ts +71 -15
  80. package/src/Hooks/selection.test.tsx +5 -5
  81. package/src/Hooks/selection.ts +6 -7
  82. package/src/InternalContext/InternalContext.test.tsx +88 -0
  83. package/src/InternalContext/InternalContext.tsx +35 -0
  84. package/src/InternalContext/index.ts +2 -0
  85. package/src/index.ts +9 -0
@@ -1,11 +1,9 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  import { useState } from "react";
3
- export const useDataViewSelection = (props) => {
4
- var _a;
5
- const [selected, setSelected] = useState((_a = props.initialSelected) !== null && _a !== void 0 ? _a : []);
6
- const matchOption = props.matchOption ? props.matchOption : (option, another) => (option === another);
3
+ export const useDataViewSelection = ({ matchOption, initialSelected = [] }) => {
4
+ const [selected, setSelected] = useState(initialSelected);
7
5
  const onSelect = (isSelecting, items) => {
8
- isSelecting ?
6
+ isSelecting && items ?
9
7
  setSelected(prev => {
10
8
  const newSelectedItems = [...prev];
11
9
  (Array.isArray(items) ? items : [items]).forEach(newItem => !prev.some(prevItem => matchOption(prevItem, newItem)) && newSelectedItems.push(newItem));
@@ -13,7 +11,7 @@ export const useDataViewSelection = (props) => {
13
11
  })
14
12
  : setSelected(items ? prev => prev.filter(prevSelected => !(Array.isArray(items) ? items : [items]).some(item => matchOption(item, prevSelected))) : []);
15
13
  };
16
- const isSelected = (item) => (props === null || props === void 0 ? void 0 : props.matchOption) ? Boolean(selected.find(selected => matchOption(selected, item))) : selected.includes(item);
14
+ const isSelected = (item) => Boolean(selected.find(selected => matchOption(selected, item)));
17
15
  return {
18
16
  selected,
19
17
  onSelect,
@@ -12,7 +12,7 @@ import { renderHook, act } from '@testing-library/react';
12
12
  import { useDataViewSelection } from './selection';
13
13
  describe('useDataViewSelection', () => {
14
14
  it('should get initial state correctly - no initialSelected', () => {
15
- const { result } = renderHook(() => useDataViewSelection({}));
15
+ const { result } = renderHook(() => useDataViewSelection({ matchOption: (a, b) => a.id === b.id }));
16
16
  expect(result.current).toEqual({
17
17
  selected: [],
18
18
  onSelect: expect.any(Function),
@@ -21,7 +21,7 @@ describe('useDataViewSelection', () => {
21
21
  });
22
22
  it('should get initial state correctly - with initialSelected', () => {
23
23
  const initialSelected = [{ id: 1, name: 'test1' }];
24
- const { result } = renderHook(() => useDataViewSelection({ initialSelected }));
24
+ const { result } = renderHook(() => useDataViewSelection({ initialSelected, matchOption: (a, b) => a.id === b.id }));
25
25
  expect(result.current).toEqual({
26
26
  selected: initialSelected,
27
27
  onSelect: expect.any(Function),
@@ -30,7 +30,7 @@ describe('useDataViewSelection', () => {
30
30
  });
31
31
  it('should select items correctly - objects', () => __awaiter(void 0, void 0, void 0, function* () {
32
32
  const initialSelected = [{ id: 1, name: 'test1' }];
33
- const { result } = renderHook(() => useDataViewSelection({ initialSelected }));
33
+ const { result } = renderHook(() => useDataViewSelection({ initialSelected, matchOption: (a, b) => a.id === b.id }));
34
34
  yield act(() => __awaiter(void 0, void 0, void 0, function* () {
35
35
  result.current.onSelect(true, { id: 2, name: 'test2' });
36
36
  }));
@@ -38,7 +38,7 @@ describe('useDataViewSelection', () => {
38
38
  }));
39
39
  it('should deselect items correctly - strings', () => __awaiter(void 0, void 0, void 0, function* () {
40
40
  const initialSelected = ['test1', 'test2'];
41
- const { result } = renderHook(() => useDataViewSelection({ initialSelected }));
41
+ const { result } = renderHook(() => useDataViewSelection({ initialSelected, matchOption: (a, b) => a === b }));
42
42
  yield act(() => __awaiter(void 0, void 0, void 0, function* () {
43
43
  result.current.onSelect(false, 'test2');
44
44
  }));
@@ -0,0 +1,17 @@
1
+ import React, { PropsWithChildren } from 'react';
2
+ export interface DataViewSelection {
3
+ /** Called when the selection of items changes */
4
+ onSelect: (isSelecting: boolean, items?: any[] | any) => void;
5
+ /** Checks if a specific item is currently selected */
6
+ isSelected: (item: any) => boolean;
7
+ /** Determines if selection is disabled for a given item */
8
+ isSelectDisabled?: (item: any) => boolean;
9
+ }
10
+ export interface InternalContextValue {
11
+ selection?: DataViewSelection;
12
+ }
13
+ export declare const InternalContext: React.Context<InternalContextValue>;
14
+ export type InternalProviderProps = PropsWithChildren<InternalContextValue>;
15
+ export declare const InternalContextProvider: React.FC<InternalProviderProps>;
16
+ export declare const useInternalContext: () => InternalContextValue;
17
+ export default InternalContext;
@@ -0,0 +1,7 @@
1
+ import React, { createContext, useContext } from 'react';
2
+ export const InternalContext = createContext({
3
+ selection: undefined
4
+ });
5
+ export const InternalContextProvider = ({ children, selection }) => (React.createElement(InternalContext.Provider, { value: { selection } }, children));
6
+ export const useInternalContext = () => useContext(InternalContext);
7
+ export default InternalContext;
@@ -0,0 +1 @@
1
+ import '@testing-library/jest-dom';
@@ -0,0 +1,50 @@
1
+ import React from 'react';
2
+ import { render, fireEvent } from '@testing-library/react';
3
+ import '@testing-library/jest-dom';
4
+ import { InternalContextProvider, useInternalContext } from './InternalContext';
5
+ describe('InternalContext', () => {
6
+ const mockSelection = {
7
+ onSelect: jest.fn(),
8
+ isSelected: jest.fn(),
9
+ isSelectDisabled: jest.fn(),
10
+ };
11
+ test('should provide context value and allow consuming it', () => {
12
+ const TestComponent = () => {
13
+ const { selection } = useInternalContext();
14
+ return (React.createElement("div", null,
15
+ React.createElement("button", { onClick: () => selection === null || selection === void 0 ? void 0 : selection.onSelect(true, ['item1']) }, "Select item"),
16
+ React.createElement("span", null, (selection === null || selection === void 0 ? void 0 : selection.isSelected('item1')) ? 'Selected' : 'Not selected')));
17
+ };
18
+ const { getByText } = render(React.createElement(InternalContextProvider, { selection: mockSelection },
19
+ React.createElement(TestComponent, null)));
20
+ fireEvent.click(getByText('Select item'));
21
+ expect(mockSelection.onSelect).toHaveBeenCalledWith(true, ['item1']);
22
+ });
23
+ test('should handle selection state correctly', () => {
24
+ const mockSelectionState = Object.assign(Object.assign({}, mockSelection), { isSelected: jest.fn((item) => item === 'item1') });
25
+ const TestComponent = () => {
26
+ const { selection } = useInternalContext();
27
+ return (React.createElement("div", null,
28
+ React.createElement("span", null, (selection === null || selection === void 0 ? void 0 : selection.isSelected('item1')) ? 'Item 1 is selected' : 'Item 1 is not selected'),
29
+ React.createElement("span", null, (selection === null || selection === void 0 ? void 0 : selection.isSelected('item2')) ? 'Item 2 is selected' : 'Item 2 is not selected')));
30
+ };
31
+ const { getByText } = render(React.createElement(InternalContextProvider, { selection: mockSelectionState },
32
+ React.createElement(TestComponent, null)));
33
+ expect(getByText('Item 1 is selected')).toBeInTheDocument();
34
+ expect(getByText('Item 2 is not selected')).toBeInTheDocument();
35
+ });
36
+ test('should handle selection disabled correctly', () => {
37
+ const mockSelectionWithDisabled = Object.assign(Object.assign({}, mockSelection), { isSelectDisabled: jest.fn((item) => item === 'item3') });
38
+ const TestComponent = () => {
39
+ var _a, _b;
40
+ const { selection } = useInternalContext();
41
+ return (React.createElement("div", null,
42
+ React.createElement("span", null, ((_a = selection === null || selection === void 0 ? void 0 : selection.isSelectDisabled) === null || _a === void 0 ? void 0 : _a.call(selection, 'item3')) ? 'Item 3 is disabled' : 'Item 3 is enabled'),
43
+ React.createElement("span", null, ((_b = selection === null || selection === void 0 ? void 0 : selection.isSelectDisabled) === null || _b === void 0 ? void 0 : _b.call(selection, 'item1')) ? 'Item 1 is disabled' : 'Item 1 is enabled')));
44
+ };
45
+ const { getByText } = render(React.createElement(InternalContextProvider, { selection: mockSelectionWithDisabled },
46
+ React.createElement(TestComponent, null)));
47
+ expect(getByText('Item 3 is disabled')).toBeInTheDocument();
48
+ expect(getByText('Item 1 is enabled')).toBeInTheDocument();
49
+ });
50
+ });
@@ -0,0 +1,2 @@
1
+ export { default } from './InternalContext';
2
+ export * from './InternalContext';
@@ -0,0 +1,2 @@
1
+ export { default } from './InternalContext';
2
+ export * from './InternalContext';
@@ -1,5 +1,11 @@
1
+ export { default as InternalContext } from './InternalContext';
2
+ export * from './InternalContext';
1
3
  export * from './Hooks';
2
4
  export { default as DataViewToolbar } from './DataViewToolbar';
3
5
  export * from './DataViewToolbar';
6
+ export { default as DataViewTable } from './DataViewTable';
7
+ export * from './DataViewTable';
8
+ export { default as DataViewEventsContext } from './DataViewEventsContext';
9
+ export * from './DataViewEventsContext';
4
10
  export { default as DataView } from './DataView';
5
11
  export * from './DataView';
package/dist/esm/index.js CHANGED
@@ -1,6 +1,12 @@
1
1
  // this file is autogenerated by generate-index.js, modifying it manually will have no effect
2
+ export { default as InternalContext } from './InternalContext';
3
+ export * from './InternalContext';
2
4
  export * from './Hooks';
3
5
  export { default as DataViewToolbar } from './DataViewToolbar';
4
6
  export * from './DataViewToolbar';
7
+ export { default as DataViewTable } from './DataViewTable';
8
+ export * from './DataViewTable';
9
+ export { default as DataViewEventsContext } from './DataViewEventsContext';
10
+ export * from './DataViewEventsContext';
5
11
  export { default as DataView } from './DataView';
6
12
  export * from './DataView';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patternfly/react-data-view",
3
- "version": "5.0.1",
3
+ "version": "5.1.1",
4
4
  "description": "Data view used for Red Hat projects.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -11,11 +11,13 @@ source: react
11
11
  # If you use typescript, the name of the interface to display props for
12
12
  # These are found through the sourceProps function provided in patternfly-docs.source.js
13
13
  sortValue: 4
14
- propComponents: ['DataViewToolbar']
14
+ propComponents: ['DataViewToolbar', 'DataViewTable']
15
15
  sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/Components/Components.md
16
16
  ---
17
17
  import { BulkSelect } from '@patternfly/react-component-groups';
18
- import DataViewToolbar from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
18
+ import { ExclamationCircleIcon } from '@patternfly/react-icons';
19
+ import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
20
+ import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
19
21
 
20
22
  ## Data view toolbar
21
23
 
@@ -23,9 +25,37 @@ The **data view toolbar** component renders a default opinionated data view tool
23
25
 
24
26
  Data view toolbar can contain a `pagination`, `bulkSelect` or any other children content passed. The preffered way of passing children toolbar items is using the [toolbar item](/components/toolbar#toolbar-items) component.
25
27
 
26
- ### Basic example
28
+ ### Basic toolbar example
27
29
 
28
30
  ```js file="./DataViewToolbarExample.tsx"
29
31
 
30
32
  ```
31
33
 
34
+ ## Data view table
35
+
36
+ The **data view table** component renders your columns and rows definition into a [table](/components/table) component.
37
+
38
+ ### Rows and columns customization
39
+
40
+ This example shows possible formats of `rows` and `columns` passed to the `DataViewTable` which allow you various customizations of the table header and body.
41
+
42
+ ```js file="./DataViewTableExample.tsx"
43
+
44
+ ```
45
+
46
+ The `DataViewTable` component accepts the following props:
47
+
48
+ - `columns` defining the column headers of the table. Each item in the array can be a `ReactNode` (for simple headers) or an object with the following properties:
49
+ - `cell` (`ReactNode`) content to display in the column header.
50
+ - optional `props` (`ThProps`) to pass to the `<Th>` component, such as `width`, `sort`, and other table header cell properties.
51
+
52
+ - `rows` defining the rows to be displayed in the table. Each item in the array can be either an array of `DataViewTd` (for simple rows) or an object with the following properties:
53
+ - `row` (`DataViewTd[]`) defining the content for each cell in the row.
54
+ - optional `id` (`string`) for the row (can be used to match items in selection).
55
+ - optional `props` (`TrProps`) to pass to the `<Tr>` component, such as `isHoverable`, `isRowSelected`, and other table row properties.
56
+
57
+ - optional `ouiaId`
58
+
59
+ - optional `props` (`TableProps`) that are passed down to the `<Table>` component, except for `onSelect`, which is managed internally.
60
+
61
+
@@ -0,0 +1,47 @@
1
+ import React from 'react';
2
+ import { DataViewTable, DataViewTh, DataViewTr } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
3
+ import { ExclamationCircleIcon } from '@patternfly/react-icons';
4
+ import { Button } from '@patternfly/react-core';
5
+
6
+ interface Repository {
7
+ id: number;
8
+ name: string;
9
+ branches: string | null;
10
+ prs: string | null;
11
+ workspaces: string;
12
+ lastCommit: string;
13
+ }
14
+
15
+ const repositories: Repository[] = [
16
+ { id: 1, name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' },
17
+ { id: 2, name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
18
+ { id: 3, name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' },
19
+ { id: 4, name: 'one - 4', branches: 'two - 4', prs: 'null', workspaces: 'four - 4', lastCommit: 'five - 4' },
20
+ { id: 5, name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' },
21
+ { id: 6, name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
22
+ ];
23
+
24
+ // you can also pass props to Tr by returning { row: DataViewTd[], props: TrProps } }
25
+ const rows: DataViewTr[] = repositories.map(({ id, name, branches, prs, workspaces, lastCommit }) => [
26
+ { id, cell: workspaces, props: { favorites: { isFavorited: true } } },
27
+ { cell: <Button href='#' variant='link' isInline>{name}</Button> },
28
+ branches,
29
+ prs,
30
+ workspaces,
31
+ lastCommit
32
+ ]);
33
+
34
+ const columns: DataViewTh[] = [
35
+ null,
36
+ 'Repositories',
37
+ { cell: <>Branches<ExclamationCircleIcon className='pf-v5-u-ml-sm' color='var(--pf-v5-global--danger-color--100)'/></> },
38
+ 'Pull requests',
39
+ { cell: 'Workspaces', props: { info: { tooltip: 'More information' } } },
40
+ { cell: 'Last commit', props: { sort: { sortBy: {}, columnIndex: 4 } } }
41
+ ];
42
+
43
+ const ouiaId = 'TableExample';
44
+
45
+ export const BasicExample: React.FunctionComponent = () => (
46
+ <DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={rows} />
47
+ );
@@ -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,8 @@ 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';
22
+ import { BrowserRouter, useSearchParams } from 'react-router-dom';
21
23
 
22
24
  This is a list of functionality you can use to manage data displayed in the **data view**.
23
25
 
@@ -25,7 +27,7 @@ This is a list of functionality you can use to manage data displayed in the **da
25
27
  Allows to display data records on multiple pages and display the pagination state.
26
28
 
27
29
  ### Toolbar usage
28
- Data view toolbar can display a pagination using the `pagination` property accepting a React node. You can also pass a custom `ouiaId` for testing purposes.
30
+ Data view toolbar can display a pagination using the `pagination` property accepting a React node. You can also pass a custom `ouiaId` for testing purposes. Additionally, it offers an option to persist pagination values in the URL, which makes it easier to share or bookmark specific pages of your data.
29
31
 
30
32
  ### Pagination state
31
33
 
@@ -33,9 +35,15 @@ The `useDataViewPagination` hook manages the pagination state of the data view.
33
35
 
34
36
  **Initial values:**
35
37
  - `perPage` initial value
36
- - (optional) `page` initial value
38
+ - optional `page` initial value
39
+ - optional `searchParams` object
40
+ - optional `setSearchParams` function
37
41
 
38
- The retrieved values are named to match the PatternFly [pagination](/components/pagination) component props, so you can easily spread them.
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.
43
+
44
+ You can also pass custom `pageParam` or `perPageParam` names, renaming the pagination parameters in the URL.
45
+
46
+ The retrieved values are named to match the PatternFly [pagination](/components/pagination) component props, so you can easily spread them to the component.
39
47
 
40
48
  **Return values:**
41
49
  - current `page` number
@@ -44,6 +52,8 @@ The retrieved values are named to match the PatternFly [pagination](/components/
44
52
  - `onPerPageSelect` to modify per page value
45
53
 
46
54
  ### Pagination example
55
+ This example uses the URL for persisting the pagination state.
56
+
47
57
  ```js file="./PaginationExample.tsx"
48
58
 
49
59
  ```
@@ -59,8 +69,8 @@ Data view toolbar can display a bulk selection component using the `bulkSelect`
59
69
  The `useDataViewSelection` hook manages the selection state of the data view.
60
70
 
61
71
  **Initial values:**
62
- - (optional) `initialSelected` array of record's identifiers selected by default
63
- - (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
64
74
 
65
75
  *When no `matchOption` is passed, the `Array.prototype.includes()` operation is performed on the `selected` array.*
66
76
 
@@ -1,9 +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';
3
+ import { BrowserRouter, useSearchParams } from 'react-router-dom';
4
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';
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';
7
8
 
8
9
  const perPageOptions = [
9
10
  { title: '5', value: 5 },
@@ -27,39 +28,29 @@ const repositories: Repository[] = [
27
28
  { name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
28
29
  ];
29
30
 
30
- const cols = {
31
- name: 'Repositories',
32
- branches: 'Branches',
33
- prs: 'Pull requests',
34
- workspaces: 'Workspaces',
35
- lastCommit: 'Last commit'
36
- };
31
+ const rows = repositories.map(item => Object.values(item));
32
+
33
+ const columns = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ];
37
34
 
38
35
  const ouiaId = 'LayoutExample';
39
36
 
40
- export const BasicExample: React.FunctionComponent = () => {
41
- const pagination = useDataViewPagination({ perPage: 5 });
37
+ const MyTable: React.FunctionComponent = () => {
38
+ const [ searchParams, setSearchParams ] = useSearchParams()
39
+ const pagination = useDataViewPagination({ perPage: 5, searchParams, setSearchParams });
42
40
  const { page, perPage } = pagination;
43
41
 
44
- const data = useMemo(() => repositories.slice((page - 1) * perPage, ((page - 1) * perPage) + perPage), [ page, perPage ]);
45
-
42
+ const pageRows = useMemo(() => rows.slice((page - 1) * perPage, ((page - 1) * perPage) + perPage), [ page, perPage ]);
46
43
  return (
47
44
  <DataView>
48
45
  <DataViewToolbar ouiaId='DataViewHeader' pagination={<Pagination perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} />
49
- <Table aria-label="Repositories table" ouiaId={ouiaId}>
50
- <Thead data-ouia-component-id={`${ouiaId}-thead`}>
51
- <Tr ouiaId={`${ouiaId}-tr-head`}>
52
- {Object.values(cols).map((column, index) => <Th key={index} data-ouia-component-id={`${ouiaId}-th-${index}`}>{column}</Th>)}
53
- </Tr>
54
- </Thead>
55
- <Tbody>
56
- {data.map((repo, rowIndex) => (
57
- <Tr key={repo.name} ouiaId={`${ouiaId}-tr-${rowIndex}`}>
58
- {Object.keys(cols).map((column, colIndex) => <Td key={colIndex} data-ouia-component-id={`${ouiaId}-td-${rowIndex}-${colIndex}`}>{repo[column]}</Td>)}
59
- </Tr>
60
- ))}
61
- </Tbody>
62
- </Table>
46
+ <DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={pageRows} />
63
47
  <DataViewToolbar ouiaId='DataViewFooter' pagination={<Pagination isCompact perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} />
64
48
  </DataView>
65
- )}
49
+ )
50
+ }
51
+
52
+ export const BasicExample: React.FunctionComponent = () => (
53
+ <BrowserRouter>
54
+ <MyTable/>
55
+ </BrowserRouter>
56
+ )
@@ -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
  }