@patternfly/react-data-view 5.1.0 → 5.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/DataView/DataView.d.ts +4 -0
- package/dist/cjs/DataView/DataView.js +7 -1
- package/dist/cjs/DataViewEventsContext/DataViewEventsContext.d.ts +16 -0
- package/dist/cjs/DataViewEventsContext/DataViewEventsContext.js +62 -0
- package/dist/cjs/DataViewEventsContext/DataViewEventsContext.test.d.ts +1 -0
- package/dist/cjs/DataViewEventsContext/DataViewEventsContext.test.js +72 -0
- package/dist/cjs/DataViewEventsContext/index.d.ts +2 -0
- package/dist/cjs/DataViewEventsContext/index.js +23 -0
- package/dist/cjs/DataViewTable/DataViewTable.d.ts +37 -0
- package/dist/cjs/DataViewTable/DataViewTable.js +59 -0
- package/dist/cjs/DataViewTable/DataViewTable.test.d.ts +1 -0
- package/dist/cjs/DataViewTable/DataViewTable.test.js +27 -0
- package/dist/cjs/DataViewTable/index.d.ts +2 -0
- package/dist/cjs/DataViewTable/index.js +23 -0
- package/dist/cjs/Hooks/selection.d.ts +3 -3
- package/dist/cjs/Hooks/selection.js +4 -6
- package/dist/cjs/Hooks/selection.test.js +4 -4
- package/dist/cjs/InternalContext/InternalContext.d.ts +17 -0
- package/dist/cjs/InternalContext/InternalContext.js +35 -0
- package/dist/cjs/InternalContext/InternalContext.test.d.ts +1 -0
- package/dist/cjs/InternalContext/InternalContext.test.js +55 -0
- package/dist/cjs/InternalContext/index.d.ts +2 -0
- package/dist/cjs/InternalContext/index.js +23 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.js +11 -2
- package/dist/dynamic/DataViewEventsContext/package.json +1 -0
- package/dist/dynamic/DataViewTable/package.json +1 -0
- package/dist/dynamic/InternalContext/package.json +1 -0
- package/dist/esm/DataView/DataView.d.ts +4 -0
- package/dist/esm/DataView/DataView.js +7 -1
- package/dist/esm/DataViewEventsContext/DataViewEventsContext.d.ts +16 -0
- package/dist/esm/DataViewEventsContext/DataViewEventsContext.js +34 -0
- package/dist/esm/DataViewEventsContext/DataViewEventsContext.test.d.ts +1 -0
- package/dist/esm/DataViewEventsContext/DataViewEventsContext.test.js +67 -0
- package/dist/esm/DataViewEventsContext/index.d.ts +2 -0
- package/dist/esm/DataViewEventsContext/index.js +2 -0
- package/dist/esm/DataViewTable/DataViewTable.d.ts +37 -0
- package/dist/esm/DataViewTable/DataViewTable.js +49 -0
- package/dist/esm/DataViewTable/DataViewTable.test.d.ts +1 -0
- package/dist/esm/DataViewTable/DataViewTable.test.js +22 -0
- package/dist/esm/DataViewTable/index.d.ts +2 -0
- package/dist/esm/DataViewTable/index.js +2 -0
- package/dist/esm/Hooks/selection.d.ts +3 -3
- package/dist/esm/Hooks/selection.js +4 -6
- package/dist/esm/Hooks/selection.test.js +4 -4
- package/dist/esm/InternalContext/InternalContext.d.ts +17 -0
- package/dist/esm/InternalContext/InternalContext.js +7 -0
- package/dist/esm/InternalContext/InternalContext.test.d.ts +1 -0
- package/dist/esm/InternalContext/InternalContext.test.js +50 -0
- package/dist/esm/InternalContext/index.d.ts +2 -0
- package/dist/esm/InternalContext/index.js +2 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +6 -0
- package/package.json +1 -1
- package/patternfly-docs/content/extensions/data-view/examples/Components/Components.md +33 -3
- package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableExample.tsx +47 -0
- package/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsContext.md +32 -0
- package/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsExample.tsx +108 -0
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md +6 -5
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/PaginationExample.tsx +9 -26
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/SelectionExample.tsx +9 -40
- package/patternfly-docs/content/extensions/data-view/examples/Layout/Layout.md +1 -0
- package/patternfly-docs/content/extensions/data-view/examples/Layout/PredefinedLayoutExample.tsx +14 -45
- package/src/DataView/DataView.tsx +14 -2
- package/src/DataViewEventsContext/DataViewEventsContext.test.tsx +105 -0
- package/src/DataViewEventsContext/DataViewEventsContext.tsx +70 -0
- package/src/DataViewEventsContext/index.ts +2 -0
- package/src/DataViewTable/DataViewTable.test.tsx +37 -0
- package/src/DataViewTable/DataViewTable.tsx +96 -0
- package/src/DataViewTable/__snapshots__/DataViewTable.test.tsx.snap +320 -0
- package/src/DataViewTable/index.ts +2 -0
- package/src/Hooks/selection.test.tsx +5 -5
- package/src/Hooks/selection.ts +6 -7
- package/src/InternalContext/InternalContext.test.tsx +88 -0
- package/src/InternalContext/InternalContext.tsx +35 -0
- package/src/InternalContext/index.ts +2 -0
- package/src/index.ts +9 -0
package/dist/cjs/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// this file is autogenerated by generate-index.js, modifying it manually will have no effect
|
|
2
3
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
4
|
if (k2 === undefined) k2 = k;
|
|
4
5
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -17,12 +18,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
17
18
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
19
|
};
|
|
19
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.DataView = exports.DataViewToolbar = void 0;
|
|
21
|
-
|
|
21
|
+
exports.DataView = exports.DataViewEventsContext = exports.DataViewTable = exports.DataViewToolbar = exports.InternalContext = void 0;
|
|
22
|
+
var InternalContext_1 = require("./InternalContext");
|
|
23
|
+
Object.defineProperty(exports, "InternalContext", { enumerable: true, get: function () { return __importDefault(InternalContext_1).default; } });
|
|
24
|
+
__exportStar(require("./InternalContext"), exports);
|
|
22
25
|
__exportStar(require("./Hooks"), exports);
|
|
23
26
|
var DataViewToolbar_1 = require("./DataViewToolbar");
|
|
24
27
|
Object.defineProperty(exports, "DataViewToolbar", { enumerable: true, get: function () { return __importDefault(DataViewToolbar_1).default; } });
|
|
25
28
|
__exportStar(require("./DataViewToolbar"), exports);
|
|
29
|
+
var DataViewTable_1 = require("./DataViewTable");
|
|
30
|
+
Object.defineProperty(exports, "DataViewTable", { enumerable: true, get: function () { return __importDefault(DataViewTable_1).default; } });
|
|
31
|
+
__exportStar(require("./DataViewTable"), exports);
|
|
32
|
+
var DataViewEventsContext_1 = require("./DataViewEventsContext");
|
|
33
|
+
Object.defineProperty(exports, "DataViewEventsContext", { enumerable: true, get: function () { return __importDefault(DataViewEventsContext_1).default; } });
|
|
34
|
+
__exportStar(require("./DataViewEventsContext"), exports);
|
|
26
35
|
var DataView_1 = require("./DataView");
|
|
27
36
|
Object.defineProperty(exports, "DataView", { enumerable: true, get: function () { return __importDefault(DataView_1).default; } });
|
|
28
37
|
__exportStar(require("./DataView"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"main":"../../cjs/DataViewEventsContext/index.js","module":"../../esm/DataViewEventsContext/index.js","typings":"../../esm/DataViewEventsContext/index.d.ts"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"main":"../../cjs/DataViewTable/index.js","module":"../../esm/DataViewTable/index.js","typings":"../../esm/DataViewTable/index.d.ts"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"main":"../../cjs/InternalContext/index.js","module":"../../esm/InternalContext/index.js","typings":"../../esm/InternalContext/index.d.ts"}
|
|
@@ -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;
|
|
@@ -11,8 +11,14 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
11
11
|
};
|
|
12
12
|
import React from 'react';
|
|
13
13
|
import { Stack, StackItem } from '@patternfly/react-core';
|
|
14
|
-
|
|
14
|
+
import { InternalContextProvider } from '../InternalContext';
|
|
15
|
+
const DataViewImplementation = (_a) => {
|
|
15
16
|
var { children, ouiaId = 'DataView' } = _a, props = __rest(_a, ["children", "ouiaId"]);
|
|
16
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)))));
|
|
17
18
|
};
|
|
19
|
+
export const DataView = (_a) => {
|
|
20
|
+
var { children, selection } = _a, props = __rest(_a, ["children", "selection"]);
|
|
21
|
+
return (React.createElement(InternalContextProvider, { selection: selection },
|
|
22
|
+
React.createElement(DataViewImplementation, Object.assign({}, props), children)));
|
|
23
|
+
};
|
|
18
24
|
export default 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,34 @@
|
|
|
1
|
+
import React, { createContext, useCallback, useContext, useState } from "react";
|
|
2
|
+
export const EventTypes = {
|
|
3
|
+
rowClick: 'rowClick'
|
|
4
|
+
};
|
|
5
|
+
export const DataViewEventsContext = createContext({
|
|
6
|
+
subscribe: () => () => null,
|
|
7
|
+
trigger: () => null
|
|
8
|
+
});
|
|
9
|
+
export const DataViewEventsProvider = ({ children }) => {
|
|
10
|
+
const [subscriptions, setSubscriptions] = useState({
|
|
11
|
+
[EventTypes.rowClick]: {}
|
|
12
|
+
});
|
|
13
|
+
const subscribe = (event, callback) => {
|
|
14
|
+
const id = crypto.randomUUID();
|
|
15
|
+
// set new subscription
|
|
16
|
+
setSubscriptions(prevSubscriptions => (Object.assign(Object.assign({}, prevSubscriptions), { [event]: Object.assign(Object.assign({}, prevSubscriptions[event]), { [id]: callback }) })));
|
|
17
|
+
// return unsubscribe function
|
|
18
|
+
return () => {
|
|
19
|
+
setSubscriptions(prevSubscriptions => {
|
|
20
|
+
const updatedSubscriptions = Object.assign({}, prevSubscriptions);
|
|
21
|
+
delete updatedSubscriptions[event][id];
|
|
22
|
+
return updatedSubscriptions;
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
const trigger = useCallback((event, ...payload) => {
|
|
27
|
+
Object.values(subscriptions[event]).forEach(callback => {
|
|
28
|
+
callback(...payload);
|
|
29
|
+
});
|
|
30
|
+
}, [subscriptions]);
|
|
31
|
+
return (React.createElement(DataViewEventsContext.Provider, { value: { subscribe, trigger } }, children));
|
|
32
|
+
};
|
|
33
|
+
export const useDataViewEventsContext = () => useContext(DataViewEventsContext);
|
|
34
|
+
export default DataViewEventsContext;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render, fireEvent } from '@testing-library/react';
|
|
3
|
+
import { useDataViewEventsContext, DataViewEventsProvider, EventTypes } from './DataViewEventsContext';
|
|
4
|
+
let id = 0;
|
|
5
|
+
beforeAll(() => {
|
|
6
|
+
Object.defineProperty(global, 'crypto', {
|
|
7
|
+
value: {
|
|
8
|
+
randomUUID: jest.fn(() => `mocked-uuid-${id++}`),
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
describe('DataViewEventsContext', () => {
|
|
13
|
+
test('should provide context value and allow subscriptions', () => {
|
|
14
|
+
const callback = jest.fn();
|
|
15
|
+
const TestComponent = () => {
|
|
16
|
+
const { subscribe, trigger } = useDataViewEventsContext();
|
|
17
|
+
React.useEffect(() => {
|
|
18
|
+
const unsubscribe = subscribe(EventTypes.rowClick, callback);
|
|
19
|
+
return () => unsubscribe();
|
|
20
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
21
|
+
}, []);
|
|
22
|
+
return (React.createElement("button", { onClick: () => trigger(EventTypes.rowClick, 'some payload') }, "Trigger"));
|
|
23
|
+
};
|
|
24
|
+
const { getByText } = render(React.createElement(DataViewEventsProvider, null,
|
|
25
|
+
React.createElement(TestComponent, null)));
|
|
26
|
+
fireEvent.click(getByText('Trigger'));
|
|
27
|
+
expect(callback).toHaveBeenCalledWith('some payload');
|
|
28
|
+
});
|
|
29
|
+
test('should handle unsubscribing correctly', () => {
|
|
30
|
+
const callback = jest.fn();
|
|
31
|
+
const TestComponent = () => {
|
|
32
|
+
const { subscribe, trigger } = useDataViewEventsContext();
|
|
33
|
+
React.useEffect(() => {
|
|
34
|
+
const unsubscribe = subscribe(EventTypes.rowClick, callback);
|
|
35
|
+
unsubscribe();
|
|
36
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
37
|
+
}, []);
|
|
38
|
+
return (React.createElement("button", { onClick: () => trigger(EventTypes.rowClick, 'some payload') }, "Trigger"));
|
|
39
|
+
};
|
|
40
|
+
const { getByText } = render(React.createElement(DataViewEventsProvider, null,
|
|
41
|
+
React.createElement(TestComponent, null)));
|
|
42
|
+
fireEvent.click(getByText('Trigger'));
|
|
43
|
+
expect(callback).not.toHaveBeenCalled();
|
|
44
|
+
});
|
|
45
|
+
test('should handle multiple subscriptions and trigger events correctly', () => {
|
|
46
|
+
const callback1 = jest.fn();
|
|
47
|
+
const callback2 = jest.fn();
|
|
48
|
+
const TestComponent = () => {
|
|
49
|
+
const { subscribe, trigger } = useDataViewEventsContext();
|
|
50
|
+
React.useEffect(() => {
|
|
51
|
+
const unsubscribe1 = subscribe(EventTypes.rowClick, callback1);
|
|
52
|
+
const unsubscribe2 = subscribe(EventTypes.rowClick, callback2);
|
|
53
|
+
return () => {
|
|
54
|
+
unsubscribe1();
|
|
55
|
+
unsubscribe2();
|
|
56
|
+
};
|
|
57
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
58
|
+
}, []);
|
|
59
|
+
return (React.createElement("button", { onClick: () => trigger(EventTypes.rowClick, 'some payload') }, "Trigger"));
|
|
60
|
+
};
|
|
61
|
+
const { getByText } = render(React.createElement(DataViewEventsProvider, null,
|
|
62
|
+
React.createElement(TestComponent, null)));
|
|
63
|
+
fireEvent.click(getByText('Trigger'));
|
|
64
|
+
expect(callback1).toHaveBeenCalledWith('some payload');
|
|
65
|
+
expect(callback2).toHaveBeenCalledWith('some payload');
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -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,49 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import React from 'react';
|
|
13
|
+
import { Table, Tbody, Td, Th, Thead, Tr } from '@patternfly/react-table';
|
|
14
|
+
import { useInternalContext } from '../InternalContext';
|
|
15
|
+
export const isDataViewThObject = (value) => value != null && typeof value === 'object' && 'cell' in value;
|
|
16
|
+
export const isDataViewTdObject = (value) => value != null && typeof value === 'object' && 'cell' in value;
|
|
17
|
+
export const isDataViewTrObject = (value) => value != null && typeof value === 'object' && 'row' in value;
|
|
18
|
+
export const DataViewTable = (_a) => {
|
|
19
|
+
var { columns, rows, ouiaId = 'DataViewTable' } = _a, props = __rest(_a, ["columns", "rows", "ouiaId"]);
|
|
20
|
+
const { selection } = useInternalContext();
|
|
21
|
+
const { onSelect, isSelected, isSelectDisabled } = selection !== null && selection !== void 0 ? selection : {};
|
|
22
|
+
return (React.createElement(Table, Object.assign({ "aria-label": "Data table", ouiaId: ouiaId }, props),
|
|
23
|
+
React.createElement(Thead, { "data-ouia-component-id": `${ouiaId}-thead` },
|
|
24
|
+
React.createElement(Tr, { ouiaId: `${ouiaId}-tr-head` },
|
|
25
|
+
onSelect && isSelected && React.createElement(Th, { key: "row-select" }),
|
|
26
|
+
columns.map((column, index) => {
|
|
27
|
+
var _a;
|
|
28
|
+
return (React.createElement(Th, Object.assign({ key: index }, (isDataViewThObject(column) && ((_a = column === null || column === void 0 ? void 0 : column.props) !== null && _a !== void 0 ? _a : {})), { "data-ouia-component-id": `${ouiaId}-th-${index}` }), isDataViewThObject(column) ? column.cell : column));
|
|
29
|
+
}))),
|
|
30
|
+
React.createElement(Tbody, null, rows.map((row, rowIndex) => {
|
|
31
|
+
var _a;
|
|
32
|
+
const rowIsObject = isDataViewTrObject(row);
|
|
33
|
+
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 : {}))),
|
|
34
|
+
onSelect && isSelected && (React.createElement(Td, { key: `select-${rowIndex}`, select: {
|
|
35
|
+
rowIndex,
|
|
36
|
+
onSelect: (_event, isSelecting) => {
|
|
37
|
+
onSelect === null || onSelect === void 0 ? void 0 : onSelect(isSelecting, rowIsObject ? row : [row]);
|
|
38
|
+
},
|
|
39
|
+
isSelected: (isSelected === null || isSelected === void 0 ? void 0 : isSelected(row)) || false,
|
|
40
|
+
isDisabled: (isSelectDisabled === null || isSelectDisabled === void 0 ? void 0 : isSelectDisabled(row)) || false,
|
|
41
|
+
} })),
|
|
42
|
+
(rowIsObject ? row.row : row).map((cell, colIndex) => {
|
|
43
|
+
var _a;
|
|
44
|
+
const cellIsObject = isDataViewTdObject(cell);
|
|
45
|
+
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));
|
|
46
|
+
})));
|
|
47
|
+
}))));
|
|
48
|
+
};
|
|
49
|
+
export default DataViewTable;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
import { DataViewTable } from './DataViewTable';
|
|
4
|
+
const repositories = [
|
|
5
|
+
{ name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' },
|
|
6
|
+
{ name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
|
|
7
|
+
{ name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' },
|
|
8
|
+
{ name: 'one - 4', branches: 'two - 4', prs: 'null', workspaces: 'four - 4', lastCommit: 'five - 4' },
|
|
9
|
+
{ name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' },
|
|
10
|
+
{ name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
|
|
11
|
+
];
|
|
12
|
+
const rows = repositories.map(repo => ({
|
|
13
|
+
row: Object.values(repo),
|
|
14
|
+
}));
|
|
15
|
+
const columns = ['Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit'];
|
|
16
|
+
const ouiaId = 'TableExample';
|
|
17
|
+
describe('DataViewTable component', () => {
|
|
18
|
+
test('should render correctly', () => {
|
|
19
|
+
const { container } = render(React.createElement(DataViewTable, { "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, rows: rows }));
|
|
20
|
+
expect(container).toMatchSnapshot();
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -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: (
|
|
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;
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import { useState } from "react";
|
|
3
|
-
export const useDataViewSelection = (
|
|
4
|
-
|
|
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) =>
|
|
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
|
+
});
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -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
|
@@ -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
|
|
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
|
+
|
package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableExample.tsx
ADDED
|
@@ -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
|
+
);
|