@patternfly/react-data-view 5.1.4 → 5.2.0

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 (35) hide show
  1. package/dist/cjs/DataView/DataView.d.ts +8 -0
  2. package/dist/cjs/DataView/DataView.js +9 -4
  3. package/dist/cjs/DataViewTableBasic/DataViewTableBasic.d.ts +3 -2
  4. package/dist/cjs/DataViewTableBasic/DataViewTableBasic.js +6 -7
  5. package/dist/cjs/DataViewTableBasic/DataViewTableBasic.test.js +8 -1
  6. package/dist/cjs/DataViewTableTree/DataViewTableTree.d.ts +3 -2
  7. package/dist/cjs/DataViewTableTree/DataViewTableTree.js +16 -5
  8. package/dist/cjs/DataViewTableTree/DataViewTableTree.test.js +7 -1
  9. package/dist/cjs/InternalContext/InternalContext.d.ts +2 -0
  10. package/dist/cjs/InternalContext/InternalContext.js +3 -2
  11. package/dist/esm/DataView/DataView.d.ts +8 -0
  12. package/dist/esm/DataView/DataView.js +8 -3
  13. package/dist/esm/DataViewTableBasic/DataViewTableBasic.d.ts +3 -2
  14. package/dist/esm/DataViewTableBasic/DataViewTableBasic.js +6 -7
  15. package/dist/esm/DataViewTableBasic/DataViewTableBasic.test.js +8 -1
  16. package/dist/esm/DataViewTableTree/DataViewTableTree.d.ts +3 -2
  17. package/dist/esm/DataViewTableTree/DataViewTableTree.js +16 -5
  18. package/dist/esm/DataViewTableTree/DataViewTableTree.test.js +7 -1
  19. package/dist/esm/InternalContext/InternalContext.d.ts +2 -0
  20. package/dist/esm/InternalContext/InternalContext.js +3 -2
  21. package/package.json +1 -1
  22. package/patternfly-docs/content/extensions/data-view/examples/Components/Components.md +10 -3
  23. package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableEmptyExample.tsx +11 -8
  24. package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableErrorExample.tsx +38 -0
  25. package/patternfly-docs/content/extensions/data-view/examples/Layout/Layout.md +1 -1
  26. package/src/DataView/DataView.tsx +14 -4
  27. package/src/DataView/__snapshots__/DataView.test.tsx.snap +2 -2
  28. package/src/DataViewTableBasic/DataViewTableBasic.test.tsx +13 -1
  29. package/src/DataViewTableBasic/DataViewTableBasic.tsx +42 -39
  30. package/src/DataViewTableBasic/__snapshots__/DataViewTableBasic.test.tsx.snap +178 -73
  31. package/src/DataViewTableHeader/__snapshots__/DataViewTableHeader.test.tsx.snap +2 -2
  32. package/src/DataViewTableTree/DataViewTableTree.test.tsx +13 -1
  33. package/src/DataViewTableTree/DataViewTableTree.tsx +24 -11
  34. package/src/DataViewTableTree/__snapshots__/DataViewTableTree.test.tsx.snap +176 -71
  35. package/src/InternalContext/InternalContext.tsx +7 -3
@@ -1,5 +1,11 @@
1
1
  import React from 'react';
2
2
  import { DataViewSelection } from '../InternalContext';
3
+ export declare const DataViewState: {
4
+ readonly empty: "empty";
5
+ readonly loading: "loading";
6
+ readonly error: "error";
7
+ };
8
+ export type DataViewState = typeof DataViewState[keyof typeof DataViewState];
3
9
  export interface DataViewProps {
4
10
  /** Content rendered inside the data view */
5
11
  children: React.ReactNode;
@@ -7,6 +13,8 @@ export interface DataViewProps {
7
13
  ouiaId?: string;
8
14
  /** Selection context configuration */
9
15
  selection?: DataViewSelection;
16
+ /** Currently active state */
17
+ activeState?: DataViewState;
10
18
  }
11
19
  export type DataViewImpementationProps = Omit<DataViewProps, 'onSelect' | 'isItemSelected' | 'isItemSelectDisabled'>;
12
20
  export declare const DataView: React.FC<DataViewProps>;
@@ -14,17 +14,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.DataView = void 0;
17
+ exports.DataView = exports.DataViewState = void 0;
18
18
  const react_1 = __importDefault(require("react"));
19
19
  const react_core_1 = require("@patternfly/react-core");
20
20
  const InternalContext_1 = require("../InternalContext");
21
+ exports.DataViewState = {
22
+ empty: 'empty',
23
+ loading: 'loading',
24
+ error: 'error'
25
+ };
21
26
  const DataViewImplementation = (_a) => {
22
27
  var { children, ouiaId = 'DataView' } = _a, props = __rest(_a, ["children", "ouiaId"]);
23
- return (react_1.default.createElement(react_core_1.Stack, Object.assign({ "data-ouia-component-id": `${ouiaId}-stack}` }, props), react_1.default.Children.map(children, (child, index) => (react_1.default.createElement(react_core_1.StackItem, { "data-ouia-component-id": `${ouiaId}-stack-item-${index}` }, child)))));
28
+ return (react_1.default.createElement(react_core_1.Stack, Object.assign({ "data-ouia-component-id": `${ouiaId}-stack` }, props), react_1.default.Children.map(children, (child, index) => (react_1.default.createElement(react_core_1.StackItem, { "data-ouia-component-id": `${ouiaId}-stack-item-${index}` }, child)))));
24
29
  };
25
30
  const DataView = (_a) => {
26
- var { children, selection } = _a, props = __rest(_a, ["children", "selection"]);
27
- return (react_1.default.createElement(InternalContext_1.InternalContextProvider, { selection: selection },
31
+ var { children, selection, activeState } = _a, props = __rest(_a, ["children", "selection", "activeState"]);
32
+ return (react_1.default.createElement(InternalContext_1.InternalContextProvider, { selection: selection, activeState: activeState },
28
33
  react_1.default.createElement(DataViewImplementation, Object.assign({}, props), children)));
29
34
  };
30
35
  exports.DataView = DataView;
@@ -1,13 +1,14 @@
1
1
  import React from 'react';
2
2
  import { TableProps } from '@patternfly/react-table';
3
3
  import { DataViewTh, DataViewTr } from '../DataViewTable';
4
+ import { DataViewState } from '../DataView/DataView';
4
5
  export interface DataViewTableBasicProps extends Omit<TableProps, 'onSelect' | 'rows'> {
5
6
  /** Columns definition */
6
7
  columns: DataViewTh[];
7
8
  /** Current page rows */
8
9
  rows: DataViewTr[];
9
- /** Empty state to be displayed */
10
- emptyState?: React.ReactNode;
10
+ /** States to be displayed when active */
11
+ states?: Partial<Record<DataViewState, React.ReactNode>>;
11
12
  /** Custom OUIA ID */
12
13
  ouiaId?: string;
13
14
  }
@@ -41,16 +41,16 @@ const InternalContext_1 = require("../InternalContext");
41
41
  const DataViewTableHeader_1 = require("../DataViewTableHeader");
42
42
  const DataViewTable_1 = require("../DataViewTable");
43
43
  const DataViewTableBasic = (_a) => {
44
- var { columns, rows, ouiaId = 'DataViewTableBasic', emptyState = null } = _a, props = __rest(_a, ["columns", "rows", "ouiaId", "emptyState"]);
45
- const { selection } = (0, InternalContext_1.useInternalContext)();
44
+ var { columns, rows, ouiaId = 'DataViewTableBasic', states = {} } = _a, props = __rest(_a, ["columns", "rows", "ouiaId", "states"]);
45
+ const { selection, activeState } = (0, InternalContext_1.useInternalContext)();
46
46
  const { onSelect, isSelected, isSelectDisabled } = selection !== null && selection !== void 0 ? selection : {};
47
47
  const isSelectable = (0, react_1.useMemo)(() => Boolean(onSelect && isSelected), [onSelect, isSelected]);
48
48
  return (react_1.default.createElement(react_table_1.Table, Object.assign({ "aria-label": "Data table", ouiaId: ouiaId }, props),
49
49
  react_1.default.createElement(DataViewTableHeader_1.DataViewTableHeader, { columns: columns, ouiaId: ouiaId }),
50
- react_1.default.createElement(react_table_1.Tbody, null, (rows === null || rows === void 0 ? void 0 : rows.length) > 0 ? rows.map((row, rowIndex) => {
51
- var _a;
50
+ react_1.default.createElement(react_table_1.Tbody, null, activeState && Object.keys(states).includes(activeState) ? (react_1.default.createElement(react_table_1.Tr, { key: activeState, ouiaId: `${ouiaId}-tr-${activeState}` },
51
+ react_1.default.createElement(react_table_1.Td, { colSpan: columns.length + Number(isSelectable) }, states[activeState]))) : (rows.map((row, rowIndex) => {
52
52
  const rowIsObject = (0, DataViewTable_1.isDataViewTrObject)(row);
53
- return (react_1.default.createElement(react_table_1.Tr, Object.assign({ key: rowIndex, ouiaId: `${ouiaId}-tr-${rowIndex}` }, (rowIsObject && ((_a = row === null || row === void 0 ? void 0 : row.props) !== null && _a !== void 0 ? _a : {}))),
53
+ return (react_1.default.createElement(react_table_1.Tr, Object.assign({ key: rowIndex, ouiaId: `${ouiaId}-tr-${rowIndex}` }, (rowIsObject && (row === null || row === void 0 ? void 0 : row.props))),
54
54
  isSelectable && (react_1.default.createElement(react_table_1.Td, { key: `select-${rowIndex}`, select: {
55
55
  rowIndex,
56
56
  onSelect: (_event, isSelecting) => {
@@ -64,8 +64,7 @@ const DataViewTableBasic = (_a) => {
64
64
  const cellIsObject = (0, DataViewTable_1.isDataViewTdObject)(cell);
65
65
  return (react_1.default.createElement(react_table_1.Td, Object.assign({ key: colIndex }, (cellIsObject && ((_a = cell === null || cell === void 0 ? void 0 : cell.props) !== null && _a !== void 0 ? _a : {})), { "data-ouia-component-id": `${ouiaId}-td-${rowIndex}-${colIndex}` }), cellIsObject ? cell.cell : cell));
66
66
  })));
67
- }) : (react_1.default.createElement(react_table_1.Tr, { key: "empty", ouiaId: `${ouiaId}-tr-empty` },
68
- react_1.default.createElement(react_table_1.Td, { colSpan: columns.length + Number(isSelectable) }, emptyState))))));
67
+ })))));
69
68
  };
70
69
  exports.DataViewTableBasic = DataViewTableBasic;
71
70
  exports.default = exports.DataViewTableBasic;
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const react_1 = __importDefault(require("react"));
7
7
  const react_2 = require("@testing-library/react");
8
8
  const DataViewTableBasic_1 = require("./DataViewTableBasic");
9
+ const DataView_1 = __importDefault(require("../DataView/DataView"));
9
10
  const repositories = [
10
11
  { name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' },
11
12
  { name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
@@ -25,7 +26,13 @@ describe('DataViewTable component', () => {
25
26
  expect(container).toMatchSnapshot();
26
27
  });
27
28
  test('should render with an empty state', () => {
28
- const { container } = (0, react_2.render)(react_1.default.createElement(DataViewTableBasic_1.DataViewTableBasic, { "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, emptyState: "No data found", rows: [] }));
29
+ const { container } = (0, react_2.render)(react_1.default.createElement(DataView_1.default, { activeState: "empty" },
30
+ react_1.default.createElement(DataViewTableBasic_1.DataViewTableBasic, { "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, states: { empty: "No data found" }, rows: [] })));
31
+ expect(container).toMatchSnapshot();
32
+ });
33
+ test('should render with an error state', () => {
34
+ const { container } = (0, react_2.render)(react_1.default.createElement(DataView_1.default, { activeState: "error" },
35
+ react_1.default.createElement(DataViewTableBasic_1.DataViewTableBasic, { "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, states: { error: "Some error" }, rows: [] })));
29
36
  expect(container).toMatchSnapshot();
30
37
  });
31
38
  });
@@ -1,13 +1,14 @@
1
1
  import React from 'react';
2
2
  import { TableProps } from '@patternfly/react-table';
3
3
  import { DataViewTh, DataViewTrTree } from '../DataViewTable';
4
+ import { DataViewState } from '../DataView/DataView';
4
5
  export interface DataViewTableTreeProps extends Omit<TableProps, 'onSelect' | 'rows'> {
5
6
  /** Columns definition */
6
7
  columns: DataViewTh[];
7
8
  /** Current page rows */
8
9
  rows: DataViewTrTree[];
9
- /** Empty state to be displayed */
10
- emptyState?: React.ReactNode;
10
+ /** States to be displayed when active */
11
+ states?: Partial<Record<DataViewState, React.ReactNode>>;
11
12
  /** Optional icon for the leaf rows */
12
13
  leafIcon?: React.ReactNode;
13
14
  /** Optional icon for the expanded parent rows */
@@ -55,8 +55,8 @@ const isNodeChecked = (node, isSelected) => {
55
55
  return allSelected;
56
56
  };
57
57
  const DataViewTableTree = (_a) => {
58
- var { columns, rows, emptyState = null, leafIcon = null, expandedIcon = null, collapsedIcon = null, ouiaId = 'DataViewTableTree' } = _a, props = __rest(_a, ["columns", "rows", "emptyState", "leafIcon", "expandedIcon", "collapsedIcon", "ouiaId"]);
59
- const { selection } = (0, InternalContext_1.useInternalContext)();
58
+ var { columns, rows, states = {}, leafIcon = null, expandedIcon = null, collapsedIcon = null, ouiaId = 'DataViewTableTree' } = _a, props = __rest(_a, ["columns", "rows", "states", "leafIcon", "expandedIcon", "collapsedIcon", "ouiaId"]);
59
+ const { selection, activeState } = (0, InternalContext_1.useInternalContext)();
60
60
  const { onSelect, isSelected, isSelectDisabled } = selection !== null && selection !== void 0 ? selection : {};
61
61
  const [expandedNodeIds, setExpandedNodeIds] = react_1.default.useState([]);
62
62
  const [expandedDetailsNodeNames, setExpandedDetailsNodeIds] = react_1.default.useState([]);
@@ -110,11 +110,22 @@ const DataViewTableTree = (_a) => {
110
110
  ];
111
111
  };
112
112
  return renderRows(rows);
113
- }, [rows, expandedNodeIds, expandedDetailsNodeNames, leafIcon, expandedIcon, collapsedIcon, isSelected, onSelect, isSelectDisabled, ouiaId]);
113
+ }, [
114
+ rows,
115
+ expandedNodeIds,
116
+ expandedDetailsNodeNames,
117
+ leafIcon,
118
+ expandedIcon,
119
+ collapsedIcon,
120
+ isSelected,
121
+ onSelect,
122
+ isSelectDisabled,
123
+ ouiaId
124
+ ]);
114
125
  return (react_1.default.createElement(react_table_1.Table, Object.assign({ isTreeTable: true, "aria-label": "Data table", ouiaId: ouiaId }, props),
115
126
  react_1.default.createElement(DataViewTableHeader_1.DataViewTableHeader, { isTreeTable: true, columns: columns, ouiaId: ouiaId }),
116
- react_1.default.createElement(react_table_1.Tbody, null, nodes.length > 0 ? nodes : (react_1.default.createElement(react_table_1.Tr, { key: "empty", ouiaId: `${ouiaId}-tr-empty` },
117
- react_1.default.createElement(react_table_1.Td, { colSpan: columns.length }, emptyState))))));
127
+ react_1.default.createElement(react_table_1.Tbody, null, activeState && Object.keys(states).includes(activeState) ? (react_1.default.createElement(react_table_1.Tr, { key: activeState, ouiaId: `${ouiaId}-tr-${activeState}` },
128
+ react_1.default.createElement(react_table_1.Td, { colSpan: columns.length }, states[activeState]))) : nodes)));
118
129
  };
119
130
  exports.DataViewTableTree = DataViewTableTree;
120
131
  exports.default = exports.DataViewTableTree;
@@ -73,7 +73,13 @@ describe('DataViewTableTree component', () => {
73
73
  expect(container).toMatchSnapshot();
74
74
  });
75
75
  test('should render tree table with an empty state', () => {
76
- const { container } = (0, react_2.render)(react_1.default.createElement(DataViewTable_1.DataViewTable, { isTreeTable: true, "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, emptyState: "No data found", rows: [] }));
76
+ const { container } = (0, react_2.render)(react_1.default.createElement(DataView_1.DataView, { activeState: "empty" },
77
+ react_1.default.createElement(DataViewTable_1.DataViewTable, { isTreeTable: true, "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, states: { empty: "No data found" }, rows: [] })));
78
+ expect(container).toMatchSnapshot();
79
+ });
80
+ test('should render tree table with an error state', () => {
81
+ const { container } = (0, react_2.render)(react_1.default.createElement(DataView_1.DataView, { activeState: "error" },
82
+ react_1.default.createElement(DataViewTable_1.DataViewTable, { isTreeTable: true, "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, states: { error: "Some error" }, rows: [] })));
77
83
  expect(container).toMatchSnapshot();
78
84
  });
79
85
  });
@@ -1,4 +1,5 @@
1
1
  import React, { PropsWithChildren } from 'react';
2
+ import { DataViewState } from '../DataView';
2
3
  export interface DataViewSelection {
3
4
  /** Called when the selection of items changes */
4
5
  onSelect: (isSelecting: boolean, items?: any[] | any) => void;
@@ -9,6 +10,7 @@ export interface DataViewSelection {
9
10
  }
10
11
  export interface InternalContextValue {
11
12
  selection?: DataViewSelection;
13
+ activeState?: DataViewState;
12
14
  }
13
15
  export declare const InternalContext: React.Context<InternalContextValue>;
14
16
  export type InternalProviderProps = PropsWithChildren<InternalContextValue>;
@@ -26,9 +26,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.useInternalContext = exports.InternalContextProvider = exports.InternalContext = void 0;
27
27
  const react_1 = __importStar(require("react"));
28
28
  exports.InternalContext = (0, react_1.createContext)({
29
- selection: undefined
29
+ selection: undefined,
30
+ activeState: undefined
30
31
  });
31
- const InternalContextProvider = ({ children, selection }) => (react_1.default.createElement(exports.InternalContext.Provider, { value: { selection } }, children));
32
+ const InternalContextProvider = ({ children, selection, activeState }) => (react_1.default.createElement(exports.InternalContext.Provider, { value: { selection, activeState } }, children));
32
33
  exports.InternalContextProvider = InternalContextProvider;
33
34
  const useInternalContext = () => (0, react_1.useContext)(exports.InternalContext);
34
35
  exports.useInternalContext = useInternalContext;
@@ -1,5 +1,11 @@
1
1
  import React from 'react';
2
2
  import { DataViewSelection } from '../InternalContext';
3
+ export declare const DataViewState: {
4
+ readonly empty: "empty";
5
+ readonly loading: "loading";
6
+ readonly error: "error";
7
+ };
8
+ export type DataViewState = typeof DataViewState[keyof typeof DataViewState];
3
9
  export interface DataViewProps {
4
10
  /** Content rendered inside the data view */
5
11
  children: React.ReactNode;
@@ -7,6 +13,8 @@ export interface DataViewProps {
7
13
  ouiaId?: string;
8
14
  /** Selection context configuration */
9
15
  selection?: DataViewSelection;
16
+ /** Currently active state */
17
+ activeState?: DataViewState;
10
18
  }
11
19
  export type DataViewImpementationProps = Omit<DataViewProps, 'onSelect' | 'isItemSelected' | 'isItemSelectDisabled'>;
12
20
  export declare const DataView: React.FC<DataViewProps>;
@@ -12,13 +12,18 @@ var __rest = (this && this.__rest) || function (s, e) {
12
12
  import React from 'react';
13
13
  import { Stack, StackItem } from '@patternfly/react-core';
14
14
  import { InternalContextProvider } from '../InternalContext';
15
+ export const DataViewState = {
16
+ empty: 'empty',
17
+ loading: 'loading',
18
+ error: 'error'
19
+ };
15
20
  const DataViewImplementation = (_a) => {
16
21
  var { children, ouiaId = 'DataView' } = _a, props = __rest(_a, ["children", "ouiaId"]);
17
- return (React.createElement(Stack, Object.assign({ "data-ouia-component-id": `${ouiaId}-stack}` }, props), React.Children.map(children, (child, index) => (React.createElement(StackItem, { "data-ouia-component-id": `${ouiaId}-stack-item-${index}` }, child)))));
22
+ return (React.createElement(Stack, Object.assign({ "data-ouia-component-id": `${ouiaId}-stack` }, props), React.Children.map(children, (child, index) => (React.createElement(StackItem, { "data-ouia-component-id": `${ouiaId}-stack-item-${index}` }, child)))));
18
23
  };
19
24
  export const DataView = (_a) => {
20
- var { children, selection } = _a, props = __rest(_a, ["children", "selection"]);
21
- return (React.createElement(InternalContextProvider, { selection: selection },
25
+ var { children, selection, activeState } = _a, props = __rest(_a, ["children", "selection", "activeState"]);
26
+ return (React.createElement(InternalContextProvider, { selection: selection, activeState: activeState },
22
27
  React.createElement(DataViewImplementation, Object.assign({}, props), children)));
23
28
  };
24
29
  export default DataView;
@@ -1,13 +1,14 @@
1
1
  import React from 'react';
2
2
  import { TableProps } from '@patternfly/react-table';
3
3
  import { DataViewTh, DataViewTr } from '../DataViewTable';
4
+ import { DataViewState } from '../DataView/DataView';
4
5
  export interface DataViewTableBasicProps extends Omit<TableProps, 'onSelect' | 'rows'> {
5
6
  /** Columns definition */
6
7
  columns: DataViewTh[];
7
8
  /** Current page rows */
8
9
  rows: DataViewTr[];
9
- /** Empty state to be displayed */
10
- emptyState?: React.ReactNode;
10
+ /** States to be displayed when active */
11
+ states?: Partial<Record<DataViewState, React.ReactNode>>;
11
12
  /** Custom OUIA ID */
12
13
  ouiaId?: string;
13
14
  }
@@ -15,16 +15,16 @@ import { useInternalContext } from '../InternalContext';
15
15
  import { DataViewTableHeader } from '../DataViewTableHeader';
16
16
  import { isDataViewTdObject, isDataViewTrObject } from '../DataViewTable';
17
17
  export const DataViewTableBasic = (_a) => {
18
- var { columns, rows, ouiaId = 'DataViewTableBasic', emptyState = null } = _a, props = __rest(_a, ["columns", "rows", "ouiaId", "emptyState"]);
19
- const { selection } = useInternalContext();
18
+ var { columns, rows, ouiaId = 'DataViewTableBasic', states = {} } = _a, props = __rest(_a, ["columns", "rows", "ouiaId", "states"]);
19
+ const { selection, activeState } = useInternalContext();
20
20
  const { onSelect, isSelected, isSelectDisabled } = selection !== null && selection !== void 0 ? selection : {};
21
21
  const isSelectable = useMemo(() => Boolean(onSelect && isSelected), [onSelect, isSelected]);
22
22
  return (React.createElement(Table, Object.assign({ "aria-label": "Data table", ouiaId: ouiaId }, props),
23
23
  React.createElement(DataViewTableHeader, { columns: columns, ouiaId: ouiaId }),
24
- React.createElement(Tbody, null, (rows === null || rows === void 0 ? void 0 : rows.length) > 0 ? rows.map((row, rowIndex) => {
25
- var _a;
24
+ React.createElement(Tbody, null, activeState && Object.keys(states).includes(activeState) ? (React.createElement(Tr, { key: activeState, ouiaId: `${ouiaId}-tr-${activeState}` },
25
+ React.createElement(Td, { colSpan: columns.length + Number(isSelectable) }, states[activeState]))) : (rows.map((row, rowIndex) => {
26
26
  const rowIsObject = isDataViewTrObject(row);
27
- return (React.createElement(Tr, Object.assign({ key: rowIndex, ouiaId: `${ouiaId}-tr-${rowIndex}` }, (rowIsObject && ((_a = row === null || row === void 0 ? void 0 : row.props) !== null && _a !== void 0 ? _a : {}))),
27
+ return (React.createElement(Tr, Object.assign({ key: rowIndex, ouiaId: `${ouiaId}-tr-${rowIndex}` }, (rowIsObject && (row === null || row === void 0 ? void 0 : row.props))),
28
28
  isSelectable && (React.createElement(Td, { key: `select-${rowIndex}`, select: {
29
29
  rowIndex,
30
30
  onSelect: (_event, isSelecting) => {
@@ -38,7 +38,6 @@ export const DataViewTableBasic = (_a) => {
38
38
  const cellIsObject = isDataViewTdObject(cell);
39
39
  return (React.createElement(Td, Object.assign({ key: colIndex }, (cellIsObject && ((_a = cell === null || cell === void 0 ? void 0 : cell.props) !== null && _a !== void 0 ? _a : {})), { "data-ouia-component-id": `${ouiaId}-td-${rowIndex}-${colIndex}` }), cellIsObject ? cell.cell : cell));
40
40
  })));
41
- }) : (React.createElement(Tr, { key: "empty", ouiaId: `${ouiaId}-tr-empty` },
42
- React.createElement(Td, { colSpan: columns.length + Number(isSelectable) }, emptyState))))));
41
+ })))));
43
42
  };
44
43
  export default DataViewTableBasic;
@@ -1,6 +1,7 @@
1
1
  import React from 'react';
2
2
  import { render } from '@testing-library/react';
3
3
  import { DataViewTableBasic } from './DataViewTableBasic';
4
+ import DataView from '../DataView/DataView';
4
5
  const repositories = [
5
6
  { name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' },
6
7
  { name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
@@ -20,7 +21,13 @@ describe('DataViewTable component', () => {
20
21
  expect(container).toMatchSnapshot();
21
22
  });
22
23
  test('should render with an empty state', () => {
23
- const { container } = render(React.createElement(DataViewTableBasic, { "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, emptyState: "No data found", rows: [] }));
24
+ const { container } = render(React.createElement(DataView, { activeState: "empty" },
25
+ React.createElement(DataViewTableBasic, { "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, states: { empty: "No data found" }, rows: [] })));
26
+ expect(container).toMatchSnapshot();
27
+ });
28
+ test('should render with an error state', () => {
29
+ const { container } = render(React.createElement(DataView, { activeState: "error" },
30
+ React.createElement(DataViewTableBasic, { "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, states: { error: "Some error" }, rows: [] })));
24
31
  expect(container).toMatchSnapshot();
25
32
  });
26
33
  });
@@ -1,13 +1,14 @@
1
1
  import React from 'react';
2
2
  import { TableProps } from '@patternfly/react-table';
3
3
  import { DataViewTh, DataViewTrTree } from '../DataViewTable';
4
+ import { DataViewState } from '../DataView/DataView';
4
5
  export interface DataViewTableTreeProps extends Omit<TableProps, 'onSelect' | 'rows'> {
5
6
  /** Columns definition */
6
7
  columns: DataViewTh[];
7
8
  /** Current page rows */
8
9
  rows: DataViewTrTree[];
9
- /** Empty state to be displayed */
10
- emptyState?: React.ReactNode;
10
+ /** States to be displayed when active */
11
+ states?: Partial<Record<DataViewState, React.ReactNode>>;
11
12
  /** Optional icon for the leaf rows */
12
13
  leafIcon?: React.ReactNode;
13
14
  /** Optional icon for the expanded parent rows */
@@ -29,8 +29,8 @@ const isNodeChecked = (node, isSelected) => {
29
29
  return allSelected;
30
30
  };
31
31
  export const DataViewTableTree = (_a) => {
32
- var { columns, rows, emptyState = null, leafIcon = null, expandedIcon = null, collapsedIcon = null, ouiaId = 'DataViewTableTree' } = _a, props = __rest(_a, ["columns", "rows", "emptyState", "leafIcon", "expandedIcon", "collapsedIcon", "ouiaId"]);
33
- const { selection } = useInternalContext();
32
+ var { columns, rows, states = {}, leafIcon = null, expandedIcon = null, collapsedIcon = null, ouiaId = 'DataViewTableTree' } = _a, props = __rest(_a, ["columns", "rows", "states", "leafIcon", "expandedIcon", "collapsedIcon", "ouiaId"]);
33
+ const { selection, activeState } = useInternalContext();
34
34
  const { onSelect, isSelected, isSelectDisabled } = selection !== null && selection !== void 0 ? selection : {};
35
35
  const [expandedNodeIds, setExpandedNodeIds] = React.useState([]);
36
36
  const [expandedDetailsNodeNames, setExpandedDetailsNodeIds] = React.useState([]);
@@ -84,10 +84,21 @@ export const DataViewTableTree = (_a) => {
84
84
  ];
85
85
  };
86
86
  return renderRows(rows);
87
- }, [rows, expandedNodeIds, expandedDetailsNodeNames, leafIcon, expandedIcon, collapsedIcon, isSelected, onSelect, isSelectDisabled, ouiaId]);
87
+ }, [
88
+ rows,
89
+ expandedNodeIds,
90
+ expandedDetailsNodeNames,
91
+ leafIcon,
92
+ expandedIcon,
93
+ collapsedIcon,
94
+ isSelected,
95
+ onSelect,
96
+ isSelectDisabled,
97
+ ouiaId
98
+ ]);
88
99
  return (React.createElement(Table, Object.assign({ isTreeTable: true, "aria-label": "Data table", ouiaId: ouiaId }, props),
89
100
  React.createElement(DataViewTableHeader, { isTreeTable: true, columns: columns, ouiaId: ouiaId }),
90
- React.createElement(Tbody, null, nodes.length > 0 ? nodes : (React.createElement(Tr, { key: "empty", ouiaId: `${ouiaId}-tr-empty` },
91
- React.createElement(Td, { colSpan: columns.length }, emptyState))))));
101
+ React.createElement(Tbody, null, activeState && Object.keys(states).includes(activeState) ? (React.createElement(Tr, { key: activeState, ouiaId: `${ouiaId}-tr-${activeState}` },
102
+ React.createElement(Td, { colSpan: columns.length }, states[activeState]))) : nodes)));
92
103
  };
93
104
  export default DataViewTableTree;
@@ -68,7 +68,13 @@ describe('DataViewTableTree component', () => {
68
68
  expect(container).toMatchSnapshot();
69
69
  });
70
70
  test('should render tree table with an empty state', () => {
71
- const { container } = render(React.createElement(DataViewTable, { isTreeTable: true, "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, emptyState: "No data found", rows: [] }));
71
+ const { container } = render(React.createElement(DataView, { activeState: "empty" },
72
+ React.createElement(DataViewTable, { isTreeTable: true, "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, states: { empty: "No data found" }, rows: [] })));
73
+ expect(container).toMatchSnapshot();
74
+ });
75
+ test('should render tree table with an error state', () => {
76
+ const { container } = render(React.createElement(DataView, { activeState: "error" },
77
+ React.createElement(DataViewTable, { isTreeTable: true, "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, states: { error: "Some error" }, rows: [] })));
72
78
  expect(container).toMatchSnapshot();
73
79
  });
74
80
  });
@@ -1,4 +1,5 @@
1
1
  import React, { PropsWithChildren } from 'react';
2
+ import { DataViewState } from '../DataView';
2
3
  export interface DataViewSelection {
3
4
  /** Called when the selection of items changes */
4
5
  onSelect: (isSelecting: boolean, items?: any[] | any) => void;
@@ -9,6 +10,7 @@ export interface DataViewSelection {
9
10
  }
10
11
  export interface InternalContextValue {
11
12
  selection?: DataViewSelection;
13
+ activeState?: DataViewState;
12
14
  }
13
15
  export declare const InternalContext: React.Context<InternalContextValue>;
14
16
  export type InternalProviderProps = PropsWithChildren<InternalContextValue>;
@@ -1,7 +1,8 @@
1
1
  import React, { createContext, useContext } from 'react';
2
2
  export const InternalContext = createContext({
3
- selection: undefined
3
+ selection: undefined,
4
+ activeState: undefined
4
5
  });
5
- export const InternalContextProvider = ({ children, selection }) => (React.createElement(InternalContext.Provider, { value: { selection } }, children));
6
+ export const InternalContextProvider = ({ children, selection, activeState }) => (React.createElement(InternalContext.Provider, { value: { selection, activeState } }, children));
6
7
  export const useInternalContext = () => useContext(InternalContext);
7
8
  export default InternalContext;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patternfly/react-data-view",
3
- "version": "5.1.4",
3
+ "version": "5.2.0",
4
4
  "description": "Data view used for Red Hat projects.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -16,11 +16,11 @@ sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/mod
16
16
  ---
17
17
  import { Button, EmptyState, EmptyStateActions, EmptyStateBody, EmptyStateFooter, EmptyStateHeader, EmptyStateIcon } from '@patternfly/react-core';
18
18
  import { CubesIcon, FolderIcon, FolderOpenIcon, LeafIcon, ExclamationCircleIcon } from '@patternfly/react-icons';
19
- import { BulkSelect } from '@patternfly/react-component-groups';
19
+ import { BulkSelect, ErrorState } from '@patternfly/react-component-groups';
20
20
  import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
21
21
  import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
22
22
  import { useDataViewSelection } from '@patternfly/react-data-view/dist/dynamic/Hooks';
23
- import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
23
+ import { DataView, DataViewState } from '@patternfly/react-data-view/dist/dynamic/DataView';
24
24
 
25
25
  ## Data view toolbar
26
26
 
@@ -74,8 +74,15 @@ It is also possible to disable row selection using the `isSelectDisabled` functi
74
74
  ```
75
75
 
76
76
  ### Empty state example
77
- The data view table also supports displaying a custom empty state. You can pass it using the `emptyState` property and it will be displayed in case there are no rows to be rendered.
77
+ The data view table supports displaying a custom empty state. You can pass it using the `states` property and `empty` key. It will be automatically displayed in case there are no rows to be rendered.
78
78
 
79
79
  ```js file="./DataViewTableEmptyExample.tsx"
80
80
 
81
81
  ```
82
+
83
+ ### Error state example
84
+ The data view table also supports displaying an error state. You can pass it using the `states` property and `error` key. It will be displayed in case the data view recieves its `state` property set to `error`.
85
+
86
+ ```js file="./DataViewTableErrorExample.tsx"
87
+
88
+ ```
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
+ import { DataView, DataViewState } from '@patternfly/react-data-view/dist/dynamic/DataView';
2
3
  import { DataViewTable, DataViewTr, DataViewTh } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
3
4
  import { CubesIcon } from '@patternfly/react-icons';
4
5
  import { Button, EmptyState, EmptyStateActions, EmptyStateBody, EmptyStateFooter, EmptyStateHeader, EmptyStateIcon } from '@patternfly/react-core';
@@ -21,7 +22,7 @@ const columns: DataViewTh[] = [ 'Repositories', 'Branches', 'Pull requests', 'Wo
21
22
 
22
23
  const ouiaId = 'TableExample';
23
24
 
24
- const emptyState = (
25
+ const empty = (
25
26
  <EmptyState>
26
27
  <EmptyStateHeader titleText="No data found" headingLevel="h4" icon={<EmptyStateIcon icon={CubesIcon} />} />
27
28
  <EmptyStateBody>There are no matching data to be displayed.</EmptyStateBody>
@@ -38,11 +39,13 @@ const emptyState = (
38
39
  );
39
40
 
40
41
  export const BasicExample: React.FunctionComponent = () => (
41
- <DataViewTable
42
- aria-label='Repositories table'
43
- ouiaId={ouiaId}
44
- columns={columns}
45
- rows={rows}
46
- emptyState={emptyState}
47
- />
42
+ <DataView activeState={DataViewState.empty}>
43
+ <DataViewTable
44
+ aria-label='Repositories table'
45
+ ouiaId={ouiaId}
46
+ columns={columns}
47
+ rows={rows}
48
+ states={{ empty }}
49
+ />
50
+ </DataView>
48
51
  );
@@ -0,0 +1,38 @@
1
+ import React from 'react';
2
+ import { DataView, DataViewState } from '@patternfly/react-data-view/dist/dynamic/DataView';
3
+ import { DataViewTable, DataViewTr, DataViewTh } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
4
+ import { ErrorState } from '@patternfly/react-component-groups';
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
+
17
+ // you can also pass props to Tr by returning { row: DataViewTd[], props: TrProps } }
18
+ const rows: DataViewTr[] = repositories.map((repository) => Object.values(repository));
19
+
20
+ const columns: DataViewTh[] = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ];
21
+
22
+ const ouiaId = 'TableErrorExample';
23
+
24
+ const error = (
25
+ <ErrorState errorTitle='Unable to load data' errorDescription='There was an error retrieving data. Check your connection and reload the page.' />
26
+ );
27
+
28
+ export const BasicExample: React.FunctionComponent = () => (
29
+ <DataView activeState={DataViewState.error}>
30
+ <DataViewTable
31
+ aria-label='Repositories table'
32
+ ouiaId={ouiaId}
33
+ columns={columns}
34
+ rows={rows}
35
+ states={{ error }}
36
+ />
37
+ </DataView>
38
+ );
@@ -11,7 +11,7 @@ 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: 2
14
- propComponents: ['DataView']
14
+ propComponents: ['DataView', 'DataViewState']
15
15
  sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/Layout/Layout.md
16
16
  ---
17
17
  import { useMemo } from 'react';
@@ -2,13 +2,23 @@ import React from 'react';
2
2
  import { Stack, StackItem } from '@patternfly/react-core';
3
3
  import { DataViewSelection, InternalContextProvider } from '../InternalContext';
4
4
 
5
+ export const DataViewState = {
6
+ empty: 'empty',
7
+ loading: 'loading',
8
+ error: 'error'
9
+ } as const;
10
+
11
+ export type DataViewState = typeof DataViewState[keyof typeof DataViewState];
12
+
5
13
  export interface DataViewProps {
6
14
  /** Content rendered inside the data view */
7
15
  children: React.ReactNode;
8
16
  /** Custom OUIA ID */
9
17
  ouiaId?: string;
10
18
  /** Selection context configuration */
11
- selection?: DataViewSelection
19
+ selection?: DataViewSelection;
20
+ /** Currently active state */
21
+ activeState?: DataViewState;
12
22
  }
13
23
 
14
24
  export type DataViewImpementationProps = Omit<DataViewProps, 'onSelect' | 'isItemSelected' | 'isItemSelectDisabled'>;
@@ -16,7 +26,7 @@ export type DataViewImpementationProps = Omit<DataViewProps, 'onSelect' | 'isIte
16
26
  const DataViewImplementation: React.FC<DataViewImpementationProps> = ({
17
27
  children, ouiaId = 'DataView', ...props
18
28
  }: DataViewImpementationProps) => (
19
- <Stack data-ouia-component-id={`${ouiaId}-stack}`} {...props}>
29
+ <Stack data-ouia-component-id={`${ouiaId}-stack`} {...props}>
20
30
  {React.Children.map(children, (child, index) => (
21
31
  <StackItem data-ouia-component-id={`${ouiaId}-stack-item-${index}`}>
22
32
  {child}
@@ -25,8 +35,8 @@ const DataViewImplementation: React.FC<DataViewImpementationProps> = ({
25
35
  </Stack>
26
36
  )
27
37
 
28
- export const DataView: React.FC<DataViewProps> = ({ children, selection, ...props }: DataViewProps) => (
29
- <InternalContextProvider selection={selection}>
38
+ export const DataView: React.FC<DataViewProps> = ({ children, selection, activeState, ...props }: DataViewProps) => (
39
+ <InternalContextProvider selection={selection} activeState={activeState} >
30
40
  <DataViewImplementation {...props}>{children}</DataViewImplementation>
31
41
  </InternalContextProvider>
32
42
  );