@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,9 +1,13 @@
1
1
  import React from 'react';
2
+ import { DataViewSelection } from '../InternalContext';
2
3
  export interface DataViewProps {
3
4
  /** Content rendered inside the data view */
4
5
  children: React.ReactNode;
5
6
  /** Custom OUIA ID */
6
7
  ouiaId?: string;
8
+ /** Selection context configuration */
9
+ selection?: DataViewSelection;
7
10
  }
11
+ export type DataViewImpementationProps = Omit<DataViewProps, 'onSelect' | 'isItemSelected' | 'isItemSelectDisabled'>;
8
12
  export declare const DataView: React.FC<DataViewProps>;
9
13
  export default DataView;
@@ -17,9 +17,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.DataView = void 0;
18
18
  const react_1 = __importDefault(require("react"));
19
19
  const react_core_1 = require("@patternfly/react-core");
20
- const DataView = (_a) => {
20
+ const InternalContext_1 = require("../InternalContext");
21
+ const DataViewImplementation = (_a) => {
21
22
  var { children, ouiaId = 'DataView' } = _a, props = __rest(_a, ["children", "ouiaId"]);
22
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)))));
23
24
  };
25
+ const DataView = (_a) => {
26
+ var { children, selection } = _a, props = __rest(_a, ["children", "selection"]);
27
+ return (react_1.default.createElement(InternalContext_1.InternalContextProvider, { selection: selection },
28
+ react_1.default.createElement(DataViewImplementation, Object.assign({}, props), children)));
29
+ };
24
30
  exports.DataView = DataView;
25
31
  exports.default = exports.DataView;
@@ -0,0 +1,16 @@
1
+ import React, { PropsWithChildren } from "react";
2
+ export declare const EventTypes: {
3
+ readonly rowClick: "rowClick";
4
+ };
5
+ export type DataViewEvent = typeof EventTypes[keyof typeof EventTypes];
6
+ type Callback = (...args: any[]) => void;
7
+ type Subscribe = (event: DataViewEvent, callback: Callback) => () => void;
8
+ type Trigger = (event: DataViewEvent, ...payload: any[]) => void;
9
+ export interface DataViewEventsContextValue {
10
+ subscribe: Subscribe;
11
+ trigger: Trigger;
12
+ }
13
+ export declare const DataViewEventsContext: React.Context<DataViewEventsContextValue>;
14
+ export declare const DataViewEventsProvider: ({ children }: PropsWithChildren) => React.JSX.Element;
15
+ export declare const useDataViewEventsContext: () => DataViewEventsContextValue;
16
+ export default DataViewEventsContext;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.useDataViewEventsContext = exports.DataViewEventsProvider = exports.DataViewEventsContext = exports.EventTypes = void 0;
27
+ const react_1 = __importStar(require("react"));
28
+ exports.EventTypes = {
29
+ rowClick: 'rowClick'
30
+ };
31
+ exports.DataViewEventsContext = (0, react_1.createContext)({
32
+ subscribe: () => () => null,
33
+ trigger: () => null
34
+ });
35
+ const DataViewEventsProvider = ({ children }) => {
36
+ const [subscriptions, setSubscriptions] = (0, react_1.useState)({
37
+ [exports.EventTypes.rowClick]: {}
38
+ });
39
+ const subscribe = (event, callback) => {
40
+ const id = crypto.randomUUID();
41
+ // set new subscription
42
+ setSubscriptions(prevSubscriptions => (Object.assign(Object.assign({}, prevSubscriptions), { [event]: Object.assign(Object.assign({}, prevSubscriptions[event]), { [id]: callback }) })));
43
+ // return unsubscribe function
44
+ return () => {
45
+ setSubscriptions(prevSubscriptions => {
46
+ const updatedSubscriptions = Object.assign({}, prevSubscriptions);
47
+ delete updatedSubscriptions[event][id];
48
+ return updatedSubscriptions;
49
+ });
50
+ };
51
+ };
52
+ const trigger = (0, react_1.useCallback)((event, ...payload) => {
53
+ Object.values(subscriptions[event]).forEach(callback => {
54
+ callback(...payload);
55
+ });
56
+ }, [subscriptions]);
57
+ return (react_1.default.createElement(exports.DataViewEventsContext.Provider, { value: { subscribe, trigger } }, children));
58
+ };
59
+ exports.DataViewEventsProvider = DataViewEventsProvider;
60
+ const useDataViewEventsContext = () => (0, react_1.useContext)(exports.DataViewEventsContext);
61
+ exports.useDataViewEventsContext = useDataViewEventsContext;
62
+ exports.default = exports.DataViewEventsContext;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_1 = __importDefault(require("react"));
7
+ const react_2 = require("@testing-library/react");
8
+ const DataViewEventsContext_1 = require("./DataViewEventsContext");
9
+ let id = 0;
10
+ beforeAll(() => {
11
+ Object.defineProperty(global, 'crypto', {
12
+ value: {
13
+ randomUUID: jest.fn(() => `mocked-uuid-${id++}`),
14
+ },
15
+ });
16
+ });
17
+ describe('DataViewEventsContext', () => {
18
+ test('should provide context value and allow subscriptions', () => {
19
+ const callback = jest.fn();
20
+ const TestComponent = () => {
21
+ const { subscribe, trigger } = (0, DataViewEventsContext_1.useDataViewEventsContext)();
22
+ react_1.default.useEffect(() => {
23
+ const unsubscribe = subscribe(DataViewEventsContext_1.EventTypes.rowClick, callback);
24
+ return () => unsubscribe();
25
+ // eslint-disable-next-line react-hooks/exhaustive-deps
26
+ }, []);
27
+ return (react_1.default.createElement("button", { onClick: () => trigger(DataViewEventsContext_1.EventTypes.rowClick, 'some payload') }, "Trigger"));
28
+ };
29
+ const { getByText } = (0, react_2.render)(react_1.default.createElement(DataViewEventsContext_1.DataViewEventsProvider, null,
30
+ react_1.default.createElement(TestComponent, null)));
31
+ react_2.fireEvent.click(getByText('Trigger'));
32
+ expect(callback).toHaveBeenCalledWith('some payload');
33
+ });
34
+ test('should handle unsubscribing correctly', () => {
35
+ const callback = jest.fn();
36
+ const TestComponent = () => {
37
+ const { subscribe, trigger } = (0, DataViewEventsContext_1.useDataViewEventsContext)();
38
+ react_1.default.useEffect(() => {
39
+ const unsubscribe = subscribe(DataViewEventsContext_1.EventTypes.rowClick, callback);
40
+ unsubscribe();
41
+ // eslint-disable-next-line react-hooks/exhaustive-deps
42
+ }, []);
43
+ return (react_1.default.createElement("button", { onClick: () => trigger(DataViewEventsContext_1.EventTypes.rowClick, 'some payload') }, "Trigger"));
44
+ };
45
+ const { getByText } = (0, react_2.render)(react_1.default.createElement(DataViewEventsContext_1.DataViewEventsProvider, null,
46
+ react_1.default.createElement(TestComponent, null)));
47
+ react_2.fireEvent.click(getByText('Trigger'));
48
+ expect(callback).not.toHaveBeenCalled();
49
+ });
50
+ test('should handle multiple subscriptions and trigger events correctly', () => {
51
+ const callback1 = jest.fn();
52
+ const callback2 = jest.fn();
53
+ const TestComponent = () => {
54
+ const { subscribe, trigger } = (0, DataViewEventsContext_1.useDataViewEventsContext)();
55
+ react_1.default.useEffect(() => {
56
+ const unsubscribe1 = subscribe(DataViewEventsContext_1.EventTypes.rowClick, callback1);
57
+ const unsubscribe2 = subscribe(DataViewEventsContext_1.EventTypes.rowClick, callback2);
58
+ return () => {
59
+ unsubscribe1();
60
+ unsubscribe2();
61
+ };
62
+ // eslint-disable-next-line react-hooks/exhaustive-deps
63
+ }, []);
64
+ return (react_1.default.createElement("button", { onClick: () => trigger(DataViewEventsContext_1.EventTypes.rowClick, 'some payload') }, "Trigger"));
65
+ };
66
+ const { getByText } = (0, react_2.render)(react_1.default.createElement(DataViewEventsContext_1.DataViewEventsProvider, null,
67
+ react_1.default.createElement(TestComponent, null)));
68
+ react_2.fireEvent.click(getByText('Trigger'));
69
+ expect(callback1).toHaveBeenCalledWith('some payload');
70
+ expect(callback2).toHaveBeenCalledWith('some payload');
71
+ });
72
+ });
@@ -0,0 +1,2 @@
1
+ export { default } from './DataViewEventsContext';
2
+ export * from './DataViewEventsContext';
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.default = void 0;
21
+ var DataViewEventsContext_1 = require("./DataViewEventsContext");
22
+ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(DataViewEventsContext_1).default; } });
23
+ __exportStar(require("./DataViewEventsContext"), exports);
@@ -0,0 +1,37 @@
1
+ import React, { ReactNode } from 'react';
2
+ import { TableProps, TdProps, ThProps, TrProps } from '@patternfly/react-table';
3
+ export type DataViewTh = ReactNode | {
4
+ cell: ReactNode;
5
+ props?: ThProps;
6
+ };
7
+ export type DataViewTd = ReactNode | {
8
+ cell: ReactNode;
9
+ props?: TdProps;
10
+ };
11
+ export type DataViewTr = DataViewTd[] | {
12
+ row: DataViewTd[];
13
+ id?: string;
14
+ props?: TrProps;
15
+ };
16
+ export declare const isDataViewThObject: (value: DataViewTh) => value is {
17
+ cell: ReactNode;
18
+ props?: ThProps | undefined;
19
+ };
20
+ export declare const isDataViewTdObject: (value: DataViewTd) => value is {
21
+ cell: ReactNode;
22
+ props?: TdProps | undefined;
23
+ };
24
+ export declare const isDataViewTrObject: (value: DataViewTr) => value is {
25
+ row: DataViewTd[];
26
+ id?: string | undefined;
27
+ };
28
+ export interface DataViewTableProps extends Omit<TableProps, 'onSelect' | 'rows'> {
29
+ /** Columns definition */
30
+ columns: DataViewTh[];
31
+ /** Current page rows */
32
+ rows: DataViewTr[];
33
+ /** Custom OUIA ID */
34
+ ouiaId?: string;
35
+ }
36
+ export declare const DataViewTable: React.FC<DataViewTableProps>;
37
+ export default DataViewTable;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.DataViewTable = exports.isDataViewTrObject = exports.isDataViewTdObject = exports.isDataViewThObject = void 0;
18
+ const react_1 = __importDefault(require("react"));
19
+ const react_table_1 = require("@patternfly/react-table");
20
+ const InternalContext_1 = require("../InternalContext");
21
+ const isDataViewThObject = (value) => value != null && typeof value === 'object' && 'cell' in value;
22
+ exports.isDataViewThObject = isDataViewThObject;
23
+ const isDataViewTdObject = (value) => value != null && typeof value === 'object' && 'cell' in value;
24
+ exports.isDataViewTdObject = isDataViewTdObject;
25
+ const isDataViewTrObject = (value) => value != null && typeof value === 'object' && 'row' in value;
26
+ exports.isDataViewTrObject = isDataViewTrObject;
27
+ const DataViewTable = (_a) => {
28
+ var { columns, rows, ouiaId = 'DataViewTable' } = _a, props = __rest(_a, ["columns", "rows", "ouiaId"]);
29
+ const { selection } = (0, InternalContext_1.useInternalContext)();
30
+ const { onSelect, isSelected, isSelectDisabled } = selection !== null && selection !== void 0 ? selection : {};
31
+ return (react_1.default.createElement(react_table_1.Table, Object.assign({ "aria-label": "Data table", ouiaId: ouiaId }, props),
32
+ react_1.default.createElement(react_table_1.Thead, { "data-ouia-component-id": `${ouiaId}-thead` },
33
+ react_1.default.createElement(react_table_1.Tr, { ouiaId: `${ouiaId}-tr-head` },
34
+ onSelect && isSelected && react_1.default.createElement(react_table_1.Th, { key: "row-select" }),
35
+ columns.map((column, index) => {
36
+ var _a;
37
+ return (react_1.default.createElement(react_table_1.Th, Object.assign({ key: index }, ((0, exports.isDataViewThObject)(column) && ((_a = column === null || column === void 0 ? void 0 : column.props) !== null && _a !== void 0 ? _a : {})), { "data-ouia-component-id": `${ouiaId}-th-${index}` }), (0, exports.isDataViewThObject)(column) ? column.cell : column));
38
+ }))),
39
+ react_1.default.createElement(react_table_1.Tbody, null, rows.map((row, rowIndex) => {
40
+ var _a;
41
+ const rowIsObject = (0, exports.isDataViewTrObject)(row);
42
+ 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 : {}))),
43
+ onSelect && isSelected && (react_1.default.createElement(react_table_1.Td, { key: `select-${rowIndex}`, select: {
44
+ rowIndex,
45
+ onSelect: (_event, isSelecting) => {
46
+ onSelect === null || onSelect === void 0 ? void 0 : onSelect(isSelecting, rowIsObject ? row : [row]);
47
+ },
48
+ isSelected: (isSelected === null || isSelected === void 0 ? void 0 : isSelected(row)) || false,
49
+ isDisabled: (isSelectDisabled === null || isSelectDisabled === void 0 ? void 0 : isSelectDisabled(row)) || false,
50
+ } })),
51
+ (rowIsObject ? row.row : row).map((cell, colIndex) => {
52
+ var _a;
53
+ const cellIsObject = (0, exports.isDataViewTdObject)(cell);
54
+ 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));
55
+ })));
56
+ }))));
57
+ };
58
+ exports.DataViewTable = DataViewTable;
59
+ exports.default = exports.DataViewTable;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_1 = __importDefault(require("react"));
7
+ const react_2 = require("@testing-library/react");
8
+ const DataViewTable_1 = require("./DataViewTable");
9
+ const repositories = [
10
+ { name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' },
11
+ { name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
12
+ { name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' },
13
+ { name: 'one - 4', branches: 'two - 4', prs: 'null', workspaces: 'four - 4', lastCommit: 'five - 4' },
14
+ { name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' },
15
+ { name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
16
+ ];
17
+ const rows = repositories.map(repo => ({
18
+ row: Object.values(repo),
19
+ }));
20
+ const columns = ['Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit'];
21
+ const ouiaId = 'TableExample';
22
+ describe('DataViewTable component', () => {
23
+ test('should render correctly', () => {
24
+ const { container } = (0, react_2.render)(react_1.default.createElement(DataViewTable_1.DataViewTable, { "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, rows: rows }));
25
+ expect(container).toMatchSnapshot();
26
+ });
27
+ });
@@ -0,0 +1,2 @@
1
+ export { default } from './DataViewTable';
2
+ export * from './DataViewTable';
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.default = void 0;
21
+ var DataViewTable_1 = require("./DataViewTable");
22
+ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(DataViewTable_1).default; } });
23
+ __exportStar(require("./DataViewTable"), exports);
@@ -1,14 +1,26 @@
1
+ export declare enum PaginationParams {
2
+ PAGE = "page",
3
+ PER_PAGE = "perPage"
4
+ }
1
5
  export interface UseDataViewPaginationProps {
2
6
  /** Initial page */
3
7
  page?: number;
4
8
  /** Items per page */
5
9
  perPage: number;
10
+ /** Current search parameters as a string */
11
+ searchParams?: URLSearchParams;
12
+ /** Function to set search parameters */
13
+ setSearchParams?: (params: URLSearchParams) => void;
14
+ /** Custom URL parameter name for page */
15
+ pageParam?: string;
16
+ /** Custom URL parameter name for per page */
17
+ perPageParam?: string;
6
18
  }
7
19
  export interface DataViewPaginationProps extends UseDataViewPaginationProps {
8
20
  /** Current page number */
9
21
  page: number;
10
22
  }
11
- export declare const useDataViewPagination: ({ page, perPage }: UseDataViewPaginationProps) => {
23
+ export declare const useDataViewPagination: ({ page, perPage, searchParams, setSearchParams, pageParam, perPageParam, }: UseDataViewPaginationProps) => {
12
24
  onPerPageSelect: (_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined, newPerPage: number) => void;
13
25
  onSetPage: (_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined, newPage: number) => void;
14
26
  page: number;
@@ -1,13 +1,45 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useDataViewPagination = void 0;
3
+ exports.useDataViewPagination = exports.PaginationParams = void 0;
4
4
  const react_1 = require("react");
5
- const useDataViewPagination = ({ page = 1, perPage }) => {
6
- const [state, setState] = (0, react_1.useState)({ page, perPage });
5
+ var PaginationParams;
6
+ (function (PaginationParams) {
7
+ PaginationParams["PAGE"] = "page";
8
+ PaginationParams["PER_PAGE"] = "perPage";
9
+ })(PaginationParams || (exports.PaginationParams = PaginationParams = {}));
10
+ const useDataViewPagination = ({ page = 1, perPage, searchParams, setSearchParams, pageParam = PaginationParams.PAGE, perPageParam = PaginationParams.PER_PAGE, }) => {
11
+ const [state, setState] = (0, react_1.useState)({
12
+ page: parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(pageParam)) || `${page}`),
13
+ perPage: parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(perPageParam)) || `${perPage}`),
14
+ });
15
+ const updateSearchParams = (page, perPage) => {
16
+ if (searchParams && setSearchParams) {
17
+ const params = new URLSearchParams(searchParams);
18
+ params.set(pageParam, `${page}`);
19
+ params.set(perPageParam, `${perPage}`);
20
+ setSearchParams(params);
21
+ }
22
+ };
23
+ (0, react_1.useEffect)(() => {
24
+ // Make sure search params are loaded or set if not present on mount
25
+ updateSearchParams(state.page, state.perPage);
26
+ // eslint-disable-next-line react-hooks/exhaustive-deps
27
+ }, []);
28
+ (0, react_1.useEffect)(() => {
29
+ // Listen on URL params changes
30
+ const currentPage = parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(pageParam)) || `${state.page}`);
31
+ const currentPerPage = parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(perPageParam)) || `${state.perPage}`);
32
+ if (currentPage !== state.page || currentPerPage !== state.perPage) {
33
+ setState({ page: currentPage, perPage: currentPerPage });
34
+ }
35
+ // eslint-disable-next-line react-hooks/exhaustive-deps
36
+ }, [searchParams === null || searchParams === void 0 ? void 0 : searchParams.toString()]);
7
37
  const onPerPageSelect = (_event, newPerPage) => {
8
- setState(prev => (Object.assign(Object.assign({}, prev), { perPage: newPerPage })));
38
+ updateSearchParams(1, newPerPage);
39
+ setState({ perPage: newPerPage, page: 1 });
9
40
  };
10
41
  const onSetPage = (_event, newPage) => {
42
+ updateSearchParams(newPage, state.perPage);
11
43
  setState(prev => (Object.assign(Object.assign({}, prev), { page: newPage })));
12
44
  };
13
45
  return Object.assign(Object.assign({}, state), { onPerPageSelect,
@@ -42,8 +42,60 @@ describe('useDataViewPagination', () => {
42
42
  expect(result.current).toEqual({
43
43
  onPerPageSelect: expect.any(Function),
44
44
  onSetPage: expect.any(Function),
45
- page: 3,
45
+ page: 1,
46
46
  perPage: 50
47
47
  });
48
48
  });
49
+ it('should read pagination state from URL', () => {
50
+ const mockSearchParams = new URLSearchParams('page=2&perPage=10');
51
+ const { result } = (0, react_1.renderHook)(() => (0, pagination_1.useDataViewPagination)({
52
+ searchParams: mockSearchParams,
53
+ setSearchParams: jest.fn(),
54
+ page: 1,
55
+ perPage: 5,
56
+ }));
57
+ expect(result.current).toEqual({
58
+ onPerPageSelect: expect.any(Function),
59
+ onSetPage: expect.any(Function),
60
+ page: 2,
61
+ perPage: 10,
62
+ });
63
+ });
64
+ it('should set pagination state in URL when page changes', () => {
65
+ const mockSetSearchParams = jest.fn();
66
+ const { result } = (0, react_1.renderHook)(() => (0, pagination_1.useDataViewPagination)({
67
+ searchParams: new URLSearchParams(),
68
+ setSearchParams: mockSetSearchParams,
69
+ page: 1,
70
+ perPage: 5,
71
+ }));
72
+ expect(mockSetSearchParams).toHaveBeenNthCalledWith(1, new URLSearchParams('page=1&perPage=5'));
73
+ (0, react_1.act)(() => {
74
+ result.current.onSetPage(undefined, 4);
75
+ });
76
+ expect(mockSetSearchParams).toHaveBeenNthCalledWith(2, new URLSearchParams('page=4&perPage=5'));
77
+ });
78
+ it('should set pagination state in URL when perPage changes', () => {
79
+ const mockSetSearchParams = jest.fn();
80
+ const { result } = (0, react_1.renderHook)(() => (0, pagination_1.useDataViewPagination)({
81
+ searchParams: new URLSearchParams('page=2'),
82
+ setSearchParams: mockSetSearchParams,
83
+ page: 1,
84
+ perPage: 5,
85
+ }));
86
+ (0, react_1.act)(() => {
87
+ result.current.onPerPageSelect(undefined, 20);
88
+ });
89
+ expect(mockSetSearchParams).toHaveBeenCalledWith(new URLSearchParams('page=1&perPage=20'));
90
+ });
91
+ it('should initialize URL with default values if not present', () => {
92
+ const mockSetSearchParams = jest.fn();
93
+ (0, react_1.renderHook)(() => (0, pagination_1.useDataViewPagination)({
94
+ searchParams: new URLSearchParams(),
95
+ setSearchParams: mockSetSearchParams,
96
+ page: 1,
97
+ perPage: 5,
98
+ }));
99
+ expect(mockSetSearchParams).toHaveBeenCalledWith(new URLSearchParams('page=1&perPage=5'));
100
+ });
49
101
  });
@@ -1,10 +1,10 @@
1
1
  export interface UseDataViewSelectionProps {
2
+ /** Function to compare items when checking if item is selected */
3
+ matchOption: (item: any, another: any) => boolean;
2
4
  /** Array of initially selected entries */
3
5
  initialSelected?: (any)[];
4
- /** Function to compare items when checking if entry is selected */
5
- matchOption?: (item: any, another: any) => boolean;
6
6
  }
7
- export declare const useDataViewSelection: (props: UseDataViewSelectionProps) => {
7
+ export declare const useDataViewSelection: ({ matchOption, initialSelected }: UseDataViewSelectionProps) => {
8
8
  selected: any[];
9
9
  onSelect: (isSelecting: boolean, items?: any[] | any) => void;
10
10
  isSelected: (item: any) => boolean;
@@ -3,12 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useDataViewSelection = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-explicit-any */
5
5
  const react_1 = require("react");
6
- const useDataViewSelection = (props) => {
7
- var _a;
8
- const [selected, setSelected] = (0, react_1.useState)((_a = props.initialSelected) !== null && _a !== void 0 ? _a : []);
9
- const matchOption = props.matchOption ? props.matchOption : (option, another) => (option === another);
6
+ const useDataViewSelection = ({ matchOption, initialSelected = [] }) => {
7
+ const [selected, setSelected] = (0, react_1.useState)(initialSelected);
10
8
  const onSelect = (isSelecting, items) => {
11
- isSelecting ?
9
+ isSelecting && items ?
12
10
  setSelected(prev => {
13
11
  const newSelectedItems = [...prev];
14
12
  (Array.isArray(items) ? items : [items]).forEach(newItem => !prev.some(prevItem => matchOption(prevItem, newItem)) && newSelectedItems.push(newItem));
@@ -16,7 +14,7 @@ const useDataViewSelection = (props) => {
16
14
  })
17
15
  : setSelected(items ? prev => prev.filter(prevSelected => !(Array.isArray(items) ? items : [items]).some(item => matchOption(item, prevSelected))) : []);
18
16
  };
19
- const isSelected = (item) => (props === null || props === void 0 ? void 0 : props.matchOption) ? Boolean(selected.find(selected => matchOption(selected, item))) : selected.includes(item);
17
+ const isSelected = (item) => Boolean(selected.find(selected => matchOption(selected, item)));
20
18
  return {
21
19
  selected,
22
20
  onSelect,
@@ -14,7 +14,7 @@ const react_1 = require("@testing-library/react");
14
14
  const selection_1 = require("./selection");
15
15
  describe('useDataViewSelection', () => {
16
16
  it('should get initial state correctly - no initialSelected', () => {
17
- const { result } = (0, react_1.renderHook)(() => (0, selection_1.useDataViewSelection)({}));
17
+ const { result } = (0, react_1.renderHook)(() => (0, selection_1.useDataViewSelection)({ matchOption: (a, b) => a.id === b.id }));
18
18
  expect(result.current).toEqual({
19
19
  selected: [],
20
20
  onSelect: expect.any(Function),
@@ -23,7 +23,7 @@ describe('useDataViewSelection', () => {
23
23
  });
24
24
  it('should get initial state correctly - with initialSelected', () => {
25
25
  const initialSelected = [{ id: 1, name: 'test1' }];
26
- const { result } = (0, react_1.renderHook)(() => (0, selection_1.useDataViewSelection)({ initialSelected }));
26
+ const { result } = (0, react_1.renderHook)(() => (0, selection_1.useDataViewSelection)({ initialSelected, matchOption: (a, b) => a.id === b.id }));
27
27
  expect(result.current).toEqual({
28
28
  selected: initialSelected,
29
29
  onSelect: expect.any(Function),
@@ -32,7 +32,7 @@ describe('useDataViewSelection', () => {
32
32
  });
33
33
  it('should select items correctly - objects', () => __awaiter(void 0, void 0, void 0, function* () {
34
34
  const initialSelected = [{ id: 1, name: 'test1' }];
35
- const { result } = (0, react_1.renderHook)(() => (0, selection_1.useDataViewSelection)({ initialSelected }));
35
+ const { result } = (0, react_1.renderHook)(() => (0, selection_1.useDataViewSelection)({ initialSelected, matchOption: (a, b) => a.id === b.id }));
36
36
  yield (0, react_1.act)(() => __awaiter(void 0, void 0, void 0, function* () {
37
37
  result.current.onSelect(true, { id: 2, name: 'test2' });
38
38
  }));
@@ -40,7 +40,7 @@ describe('useDataViewSelection', () => {
40
40
  }));
41
41
  it('should deselect items correctly - strings', () => __awaiter(void 0, void 0, void 0, function* () {
42
42
  const initialSelected = ['test1', 'test2'];
43
- const { result } = (0, react_1.renderHook)(() => (0, selection_1.useDataViewSelection)({ initialSelected }));
43
+ const { result } = (0, react_1.renderHook)(() => (0, selection_1.useDataViewSelection)({ initialSelected, matchOption: (a, b) => a === b }));
44
44
  yield (0, react_1.act)(() => __awaiter(void 0, void 0, void 0, function* () {
45
45
  result.current.onSelect(false, 'test2');
46
46
  }));
@@ -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;