@patternfly/react-data-view 7.0.0-prerelease.1 → 7.0.0-prerelease.3
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 +12 -0
- package/dist/cjs/DataView/DataView.js +14 -3
- package/dist/cjs/DataView/DataView.test.js +3 -4
- 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 +41 -0
- package/dist/cjs/DataViewTable/DataViewTable.js +18 -0
- package/dist/cjs/DataViewTable/DataViewTable.test.d.ts +1 -0
- package/dist/cjs/DataViewTable/DataViewTable.test.js +57 -0
- package/dist/cjs/DataViewTable/index.d.ts +2 -0
- package/dist/cjs/DataViewTable/index.js +23 -0
- package/dist/cjs/DataViewTableBasic/DataViewTableBasic.d.ts +18 -0
- package/dist/cjs/DataViewTableBasic/DataViewTableBasic.js +71 -0
- package/dist/cjs/DataViewTableBasic/DataViewTableBasic.test.d.ts +1 -0
- package/dist/cjs/DataViewTableBasic/DataViewTableBasic.test.js +43 -0
- package/dist/cjs/DataViewTableBasic/index.d.ts +2 -0
- package/dist/cjs/DataViewTableBasic/index.js +23 -0
- package/dist/cjs/DataViewTableHead/DataViewTableHead.d.ts +13 -0
- package/dist/cjs/DataViewTableHead/DataViewTableHead.js +57 -0
- package/dist/cjs/DataViewTableHead/DataViewTableHead.test.d.ts +1 -0
- package/dist/cjs/DataViewTableHead/DataViewTableHead.test.js +36 -0
- package/dist/cjs/DataViewTableHead/index.d.ts +2 -0
- package/dist/cjs/DataViewTableHead/index.js +23 -0
- package/dist/cjs/DataViewTableTree/DataViewTableTree.d.ts +24 -0
- package/dist/cjs/DataViewTableTree/DataViewTableTree.js +132 -0
- package/dist/cjs/DataViewTableTree/DataViewTableTree.test.d.ts +1 -0
- package/dist/cjs/DataViewTableTree/DataViewTableTree.test.js +90 -0
- package/dist/cjs/DataViewTableTree/index.d.ts +2 -0
- package/dist/cjs/DataViewTableTree/index.js +23 -0
- package/dist/cjs/DataViewToolbar/DataViewToolbar.d.ts +2 -0
- package/dist/cjs/DataViewToolbar/DataViewToolbar.js +2 -1
- package/dist/cjs/Hooks/pagination.d.ts +9 -5
- package/dist/cjs/Hooks/pagination.js +8 -8
- package/dist/cjs/Hooks/pagination.test.js +0 -52
- 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 +25 -0
- package/dist/cjs/InternalContext/InternalContext.js +40 -0
- package/dist/cjs/InternalContext/InternalContext.test.d.ts +1 -0
- package/dist/cjs/InternalContext/InternalContext.test.js +56 -0
- package/dist/cjs/InternalContext/index.d.ts +2 -0
- package/dist/cjs/InternalContext/index.js +23 -0
- package/dist/cjs/index.d.ts +12 -0
- package/dist/cjs/index.js +20 -2
- package/dist/dynamic/DataViewEventsContext/package.json +1 -0
- package/dist/dynamic/DataViewTable/package.json +1 -0
- package/dist/dynamic/DataViewTableBasic/package.json +1 -0
- package/dist/dynamic/DataViewTableHead/package.json +1 -0
- package/dist/dynamic/DataViewTableTree/package.json +1 -0
- package/dist/dynamic/InternalContext/package.json +1 -0
- package/dist/esm/DataView/DataView.d.ts +12 -0
- package/dist/esm/DataView/DataView.js +13 -2
- package/dist/esm/DataView/DataView.test.js +3 -4
- 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 +41 -0
- package/dist/esm/DataViewTable/DataViewTable.js +8 -0
- package/dist/esm/DataViewTable/DataViewTable.test.d.ts +1 -0
- package/dist/esm/DataViewTable/DataViewTable.test.js +52 -0
- package/dist/esm/DataViewTable/index.d.ts +2 -0
- package/dist/esm/DataViewTable/index.js +2 -0
- package/dist/esm/DataViewTableBasic/DataViewTableBasic.d.ts +18 -0
- package/dist/esm/DataViewTableBasic/DataViewTableBasic.js +44 -0
- package/dist/esm/DataViewTableBasic/DataViewTableBasic.test.d.ts +1 -0
- package/dist/esm/DataViewTableBasic/DataViewTableBasic.test.js +38 -0
- package/dist/esm/DataViewTableBasic/index.d.ts +2 -0
- package/dist/esm/DataViewTableBasic/index.js +2 -0
- package/dist/esm/DataViewTableHead/DataViewTableHead.d.ts +13 -0
- package/dist/esm/DataViewTableHead/DataViewTableHead.js +30 -0
- package/dist/esm/DataViewTableHead/DataViewTableHead.test.d.ts +1 -0
- package/dist/esm/DataViewTableHead/DataViewTableHead.test.js +31 -0
- package/dist/esm/DataViewTableHead/index.d.ts +2 -0
- package/dist/esm/DataViewTableHead/index.js +2 -0
- package/dist/esm/DataViewTableTree/DataViewTableTree.d.ts +24 -0
- package/dist/esm/DataViewTableTree/DataViewTableTree.js +105 -0
- package/dist/esm/DataViewTableTree/DataViewTableTree.test.d.ts +1 -0
- package/dist/esm/DataViewTableTree/DataViewTableTree.test.js +85 -0
- package/dist/esm/DataViewTableTree/index.d.ts +2 -0
- package/dist/esm/DataViewTableTree/index.js +2 -0
- package/dist/esm/DataViewToolbar/DataViewToolbar.d.ts +2 -0
- package/dist/esm/DataViewToolbar/DataViewToolbar.js +2 -1
- package/dist/esm/Hooks/pagination.d.ts +9 -5
- package/dist/esm/Hooks/pagination.js +9 -9
- package/dist/esm/Hooks/pagination.test.js +0 -52
- 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 +25 -0
- package/dist/esm/InternalContext/InternalContext.js +12 -0
- package/dist/esm/InternalContext/InternalContext.test.d.ts +1 -0
- package/dist/esm/InternalContext/InternalContext.test.js +51 -0
- package/dist/esm/InternalContext/index.d.ts +2 -0
- package/dist/esm/InternalContext/index.js +2 -0
- package/dist/esm/index.d.ts +12 -0
- package/dist/esm/index.js +12 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +11 -10
- package/patternfly-docs/content/extensions/data-view/examples/Components/Components.md +80 -5
- package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableEmptyExample.tsx +57 -0
- package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableErrorExample.tsx +45 -0
- package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableExample.tsx +67 -0
- package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableLoadingExample.tsx +27 -0
- package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableTreeExample.tsx +71 -0
- package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewToolbarActionsExample.tsx +27 -0
- package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewToolbarExample.tsx +4 -4
- 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 +12 -11
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/PaginationExample.tsx +20 -37
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/SelectionExample.tsx +15 -46
- package/patternfly-docs/content/extensions/data-view/examples/Layout/AbstractLayoutExample.tsx +3 -4
- package/patternfly-docs/content/extensions/data-view/examples/Layout/Layout.md +2 -1
- package/patternfly-docs/content/extensions/data-view/examples/Layout/PredefinedLayoutExample.tsx +20 -51
- package/patternfly-docs/pages/index.js +1 -1
- package/release.config.js +1 -5
- package/src/DataView/DataView.test.tsx +3 -4
- package/src/DataView/DataView.tsx +25 -3
- package/src/DataView/__snapshots__/DataView.test.tsx.snap +16 -16
- 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 +80 -0
- package/src/DataViewTable/DataViewTable.tsx +37 -0
- package/src/DataViewTable/__snapshots__/DataViewTable.test.tsx.snap +1042 -0
- package/src/DataViewTable/index.ts +2 -0
- package/src/DataViewTableBasic/DataViewTableBasic.test.tsx +65 -0
- package/src/DataViewTableBasic/DataViewTableBasic.tsx +82 -0
- package/src/DataViewTableBasic/__snapshots__/DataViewTableBasic.test.tsx.snap +555 -0
- package/src/DataViewTableBasic/index.ts +2 -0
- package/src/DataViewTableHead/DataViewTableHead.test.tsx +50 -0
- package/src/DataViewTableHead/DataViewTableHead.tsx +53 -0
- package/src/DataViewTableHead/__snapshots__/DataViewTableHead.test.tsx.snap +227 -0
- package/src/DataViewTableHead/index.ts +2 -0
- package/src/DataViewTableTree/DataViewTableTree.test.tsx +113 -0
- package/src/DataViewTableTree/DataViewTableTree.tsx +164 -0
- package/src/DataViewTableTree/__snapshots__/DataViewTableTree.test.tsx.snap +1200 -0
- package/src/DataViewTableTree/index.ts +2 -0
- package/src/DataViewToolbar/DataViewToolbar.tsx +9 -2
- package/src/DataViewToolbar/__snapshots__/DataViewToolbar.test.tsx.snap +208 -166
- package/src/Hooks/pagination.test.tsx +0 -80
- package/src/Hooks/pagination.ts +22 -16
- package/src/Hooks/selection.test.tsx +5 -5
- package/src/Hooks/selection.ts +6 -7
- package/src/InternalContext/InternalContext.test.tsx +89 -0
- package/src/InternalContext/InternalContext.tsx +51 -0
- package/src/InternalContext/index.ts +2 -0
- package/src/index.ts +18 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
import { DataView } from '../DataView';
|
|
4
|
+
import { DataViewTableBasic } from './DataViewTableBasic';
|
|
5
|
+
const repositories = [
|
|
6
|
+
{ name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' },
|
|
7
|
+
{ name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
|
|
8
|
+
{ name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' },
|
|
9
|
+
{ name: 'Repository four', branches: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' },
|
|
10
|
+
{ name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' },
|
|
11
|
+
{ name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' }
|
|
12
|
+
];
|
|
13
|
+
const rows = repositories.map(repo => ({
|
|
14
|
+
row: Object.values(repo),
|
|
15
|
+
}));
|
|
16
|
+
const columns = ['Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit'];
|
|
17
|
+
const ouiaId = 'TableExample';
|
|
18
|
+
describe('DataViewTable component', () => {
|
|
19
|
+
test('should render correctly', () => {
|
|
20
|
+
const { container } = render(React.createElement(DataViewTableBasic, { "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, rows: rows }));
|
|
21
|
+
expect(container).toMatchSnapshot();
|
|
22
|
+
});
|
|
23
|
+
test('should render with an empty state', () => {
|
|
24
|
+
const { container } = render(React.createElement(DataView, { activeState: "empty" },
|
|
25
|
+
React.createElement(DataViewTableBasic, { "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, bodyStates: { 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, bodyStates: { error: "Some error" }, rows: [] })));
|
|
31
|
+
expect(container).toMatchSnapshot();
|
|
32
|
+
});
|
|
33
|
+
test('should render with a loading state', () => {
|
|
34
|
+
const { container } = render(React.createElement(DataView, { activeState: "loading" },
|
|
35
|
+
React.createElement(DataViewTableBasic, { "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, bodyStates: { loading: "Data is loading" }, rows: [] })));
|
|
36
|
+
expect(container).toMatchSnapshot();
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { TheadProps } from '@patternfly/react-table';
|
|
3
|
+
import { DataViewTh } from '../DataViewTable';
|
|
4
|
+
export interface DataViewTableHeadProps extends TheadProps {
|
|
5
|
+
/** Indicates whether table is a tree */
|
|
6
|
+
isTreeTable?: boolean;
|
|
7
|
+
/** Columns definition */
|
|
8
|
+
columns: DataViewTh[];
|
|
9
|
+
/** Custom OUIA ID */
|
|
10
|
+
ouiaId?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare const DataViewTableHead: React.FC<DataViewTableHeadProps>;
|
|
13
|
+
export default DataViewTableHead;
|
|
@@ -0,0 +1,30 @@
|
|
|
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, { useMemo } from 'react';
|
|
13
|
+
import { Th, Thead, Tr } from '@patternfly/react-table';
|
|
14
|
+
import { useInternalContext } from '../InternalContext';
|
|
15
|
+
import { isDataViewThObject } from '../DataViewTable';
|
|
16
|
+
export const DataViewTableHead = (_a) => {
|
|
17
|
+
var { isTreeTable = false, columns, ouiaId = 'DataViewTableHead' } = _a, props = __rest(_a, ["isTreeTable", "columns", "ouiaId"]);
|
|
18
|
+
const { selection } = useInternalContext();
|
|
19
|
+
const { onSelect, isSelected } = selection !== null && selection !== void 0 ? selection : {};
|
|
20
|
+
const cells = useMemo(() => [
|
|
21
|
+
onSelect && isSelected && !isTreeTable ? (React.createElement(Th, { key: "row-select", screenReaderText: 'Data selection table head cell' })) : null,
|
|
22
|
+
...columns.map((column, index) => {
|
|
23
|
+
var _a;
|
|
24
|
+
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));
|
|
25
|
+
})
|
|
26
|
+
], [columns, ouiaId, onSelect, isSelected, isTreeTable]);
|
|
27
|
+
return (React.createElement(Thead, Object.assign({ "data-ouia-component-id": `${ouiaId}-thead` }, props),
|
|
28
|
+
React.createElement(Tr, { ouiaId: `${ouiaId}-tr-head` }, cells)));
|
|
29
|
+
};
|
|
30
|
+
export default DataViewTableHead;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
import { Table } from '@patternfly/react-table';
|
|
4
|
+
import { DataViewTableHead } from './DataViewTableHead';
|
|
5
|
+
import { DataView } from '../DataView';
|
|
6
|
+
const columns = ['Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit'];
|
|
7
|
+
const ouiaId = 'HeaderExample';
|
|
8
|
+
describe('DataViewTableHead component', () => {
|
|
9
|
+
const mockSelection = {
|
|
10
|
+
onSelect: jest.fn(),
|
|
11
|
+
isSelected: jest.fn(),
|
|
12
|
+
isSelectDisabled: jest.fn(),
|
|
13
|
+
};
|
|
14
|
+
test('should render correctly', () => {
|
|
15
|
+
const { container } = render(React.createElement(Table, null,
|
|
16
|
+
React.createElement(DataViewTableHead, { columns: columns, ouiaId: ouiaId })));
|
|
17
|
+
expect(container).toMatchSnapshot();
|
|
18
|
+
});
|
|
19
|
+
test('should render the tree table correctly when isTreeTable is true', () => {
|
|
20
|
+
const { container } = render(React.createElement(DataView, { selection: mockSelection },
|
|
21
|
+
React.createElement(Table, null,
|
|
22
|
+
React.createElement(DataViewTableHead, { isTreeTable: true, columns: columns, ouiaId: ouiaId }))));
|
|
23
|
+
expect(container).toMatchSnapshot();
|
|
24
|
+
});
|
|
25
|
+
test('should render selection column when selection is provided', () => {
|
|
26
|
+
const { container } = render(React.createElement(DataView, { selection: mockSelection },
|
|
27
|
+
React.createElement(Table, null,
|
|
28
|
+
React.createElement(DataViewTableHead, { columns: columns, ouiaId: ouiaId }))));
|
|
29
|
+
expect(container).toMatchSnapshot();
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { TableProps } from '@patternfly/react-table';
|
|
3
|
+
import { DataViewTh, DataViewTrTree } from '../DataViewTable';
|
|
4
|
+
import { DataViewState } from '../DataView/DataView';
|
|
5
|
+
export interface DataViewTableTreeProps extends Omit<TableProps, 'onSelect' | 'rows'> {
|
|
6
|
+
/** Columns definition */
|
|
7
|
+
columns: DataViewTh[];
|
|
8
|
+
/** Current page rows */
|
|
9
|
+
rows: DataViewTrTree[];
|
|
10
|
+
/** Table head states to be displayed when active */
|
|
11
|
+
headStates?: Partial<Record<DataViewState | string, React.ReactNode>>;
|
|
12
|
+
/** Table body states to be displayed when active */
|
|
13
|
+
bodyStates?: Partial<Record<DataViewState | string, React.ReactNode>>;
|
|
14
|
+
/** Optional icon for the leaf rows */
|
|
15
|
+
leafIcon?: React.ReactNode;
|
|
16
|
+
/** Optional icon for the expanded parent rows */
|
|
17
|
+
expandedIcon?: React.ReactNode;
|
|
18
|
+
/** Optional icon for the collapsed parent rows */
|
|
19
|
+
collapsedIcon?: React.ReactNode;
|
|
20
|
+
/** Custom OUIA ID */
|
|
21
|
+
ouiaId?: string;
|
|
22
|
+
}
|
|
23
|
+
export declare const DataViewTableTree: React.FC<DataViewTableTreeProps>;
|
|
24
|
+
export default DataViewTableTree;
|
|
@@ -0,0 +1,105 @@
|
|
|
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, { useMemo } from 'react';
|
|
13
|
+
import { Table, Tbody, Td, TreeRowWrapper, } from '@patternfly/react-table';
|
|
14
|
+
import { useInternalContext } from '../InternalContext';
|
|
15
|
+
import { DataViewTableHead } from '../DataViewTableHead';
|
|
16
|
+
import { isDataViewTdObject } from '../DataViewTable';
|
|
17
|
+
const getDescendants = (node) => (!node.children || !node.children.length) ? [node] : node.children.flatMap(getDescendants);
|
|
18
|
+
const isNodeChecked = (node, isSelected) => {
|
|
19
|
+
let allSelected = true;
|
|
20
|
+
let someSelected = false;
|
|
21
|
+
for (const descendant of getDescendants(node)) {
|
|
22
|
+
const selected = !!(isSelected === null || isSelected === void 0 ? void 0 : isSelected(descendant));
|
|
23
|
+
someSelected || (someSelected = selected);
|
|
24
|
+
allSelected && (allSelected = selected);
|
|
25
|
+
if (!allSelected && someSelected) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return allSelected;
|
|
30
|
+
};
|
|
31
|
+
export const DataViewTableTree = (_a) => {
|
|
32
|
+
var { columns, rows, headStates, bodyStates, leafIcon = null, expandedIcon = null, collapsedIcon = null, ouiaId = 'DataViewTableTree' } = _a, props = __rest(_a, ["columns", "rows", "headStates", "bodyStates", "leafIcon", "expandedIcon", "collapsedIcon", "ouiaId"]);
|
|
33
|
+
const { selection, activeState } = useInternalContext();
|
|
34
|
+
const { onSelect, isSelected, isSelectDisabled } = selection !== null && selection !== void 0 ? selection : {};
|
|
35
|
+
const [expandedNodeIds, setExpandedNodeIds] = React.useState([]);
|
|
36
|
+
const [expandedDetailsNodeNames, setExpandedDetailsNodeIds] = React.useState([]);
|
|
37
|
+
const activeHeadState = useMemo(() => activeState ? headStates === null || headStates === void 0 ? void 0 : headStates[activeState] : undefined, [activeState, headStates]);
|
|
38
|
+
const activeBodyState = useMemo(() => activeState ? bodyStates === null || bodyStates === void 0 ? void 0 : bodyStates[activeState] : undefined, [activeState, bodyStates]);
|
|
39
|
+
const nodes = useMemo(() => {
|
|
40
|
+
const renderRows = ([node, ...remainingNodes], level = 1, posinset = 1, rowIndex = 0, isHidden = false) => {
|
|
41
|
+
var _a, _b, _c, _d;
|
|
42
|
+
if (!node) {
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
const isExpanded = expandedNodeIds.includes(node.id);
|
|
46
|
+
const isDetailsExpanded = expandedDetailsNodeNames.includes(node.id);
|
|
47
|
+
const isChecked = isSelected && isNodeChecked(node, isSelected);
|
|
48
|
+
let icon = leafIcon;
|
|
49
|
+
if (node.children) {
|
|
50
|
+
icon = isExpanded ? expandedIcon : collapsedIcon;
|
|
51
|
+
}
|
|
52
|
+
const treeRow = {
|
|
53
|
+
onCollapse: () => setExpandedNodeIds(prevExpanded => {
|
|
54
|
+
const otherExpandedNodeIds = prevExpanded.filter(id => id !== node.id);
|
|
55
|
+
return isExpanded ? otherExpandedNodeIds : [...otherExpandedNodeIds, node.id];
|
|
56
|
+
}),
|
|
57
|
+
onToggleRowDetails: () => setExpandedDetailsNodeIds(prevDetailsExpanded => {
|
|
58
|
+
const otherDetailsExpandedNodeIds = prevDetailsExpanded.filter(id => id !== node.id);
|
|
59
|
+
return isDetailsExpanded ? otherDetailsExpandedNodeIds : [...otherDetailsExpandedNodeIds, node.id];
|
|
60
|
+
}),
|
|
61
|
+
onCheckChange: ((isSelectDisabled === null || isSelectDisabled === void 0 ? void 0 : isSelectDisabled(node)) || !onSelect) ? undefined : (_event, isChecking) => onSelect === null || onSelect === void 0 ? void 0 : onSelect(isChecking, getDescendants(node)),
|
|
62
|
+
rowIndex,
|
|
63
|
+
props: {
|
|
64
|
+
isExpanded,
|
|
65
|
+
isDetailsExpanded,
|
|
66
|
+
isHidden,
|
|
67
|
+
'aria-level': level,
|
|
68
|
+
'aria-posinset': posinset,
|
|
69
|
+
'aria-setsize': (_b = (_a = node.children) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0,
|
|
70
|
+
isChecked,
|
|
71
|
+
checkboxId: `checkbox_id_${(_c = node.id) === null || _c === void 0 ? void 0 : _c.toLowerCase().replace(/\s+/g, '_')}`,
|
|
72
|
+
icon,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
const childRows = ((_d = node.children) === null || _d === void 0 ? void 0 : _d.length)
|
|
76
|
+
? renderRows(node.children, level + 1, 1, rowIndex + 1, !isExpanded || isHidden)
|
|
77
|
+
: [];
|
|
78
|
+
return [
|
|
79
|
+
React.createElement(TreeRowWrapper, { key: node.id, row: { props: treeRow.props }, ouiaId: `${ouiaId}-tr-${rowIndex}` }, node.row.map((cell, colIndex) => {
|
|
80
|
+
var _a;
|
|
81
|
+
const cellIsObject = isDataViewTdObject(cell);
|
|
82
|
+
return (React.createElement(Td, Object.assign({ key: colIndex, treeRow: colIndex === 0 ? treeRow : undefined }, (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));
|
|
83
|
+
})),
|
|
84
|
+
...childRows,
|
|
85
|
+
...renderRows(remainingNodes, level, posinset + 1, rowIndex + 1 + childRows.length, isHidden),
|
|
86
|
+
];
|
|
87
|
+
};
|
|
88
|
+
return renderRows(rows);
|
|
89
|
+
}, [
|
|
90
|
+
rows,
|
|
91
|
+
expandedNodeIds,
|
|
92
|
+
expandedDetailsNodeNames,
|
|
93
|
+
leafIcon,
|
|
94
|
+
expandedIcon,
|
|
95
|
+
collapsedIcon,
|
|
96
|
+
isSelected,
|
|
97
|
+
onSelect,
|
|
98
|
+
isSelectDisabled,
|
|
99
|
+
ouiaId
|
|
100
|
+
]);
|
|
101
|
+
return (React.createElement(Table, Object.assign({ isTreeTable: true, "aria-label": "Data table", ouiaId: ouiaId }, props),
|
|
102
|
+
activeHeadState || React.createElement(DataViewTableHead, { isTreeTable: true, columns: columns, ouiaId: ouiaId }),
|
|
103
|
+
activeBodyState || React.createElement(Tbody, null, nodes)));
|
|
104
|
+
};
|
|
105
|
+
export default DataViewTableTree;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
import { FolderIcon, FolderOpenIcon, LeafIcon } from '@patternfly/react-icons';
|
|
4
|
+
import { DataView } from '../DataView';
|
|
5
|
+
import { DataViewTable } from '../DataViewTable';
|
|
6
|
+
const repositories = [
|
|
7
|
+
{
|
|
8
|
+
name: 'Repository one',
|
|
9
|
+
branches: 'Branch one',
|
|
10
|
+
prs: 'Pull request one',
|
|
11
|
+
workspaces: 'Workspace one',
|
|
12
|
+
lastCommit: 'Timestamp one',
|
|
13
|
+
children: [
|
|
14
|
+
{
|
|
15
|
+
name: 'Repository two',
|
|
16
|
+
branches: 'Branch two',
|
|
17
|
+
prs: 'Pull request two',
|
|
18
|
+
workspaces: 'Workspace two',
|
|
19
|
+
lastCommit: 'Timestamp two',
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
name: 'Repository three',
|
|
23
|
+
branches: 'Branch three',
|
|
24
|
+
prs: 'Pull request three',
|
|
25
|
+
workspaces: 'Workspace three',
|
|
26
|
+
lastCommit: 'Timestamp three',
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'Repository four',
|
|
32
|
+
branches: 'Branch four',
|
|
33
|
+
prs: 'Pull request four',
|
|
34
|
+
workspaces: 'Workspace four',
|
|
35
|
+
lastCommit: 'Timestamp four',
|
|
36
|
+
children: [
|
|
37
|
+
{
|
|
38
|
+
name: 'Repository five',
|
|
39
|
+
branches: 'Branch five',
|
|
40
|
+
prs: 'Pull request five',
|
|
41
|
+
workspaces: 'Workspace five',
|
|
42
|
+
lastCommit: 'Timestamp five',
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: 'Repository six',
|
|
48
|
+
branches: 'Branch six',
|
|
49
|
+
prs: 'Pull request six',
|
|
50
|
+
workspaces: 'Workspace six',
|
|
51
|
+
lastCommit: 'Timestamp six',
|
|
52
|
+
},
|
|
53
|
+
];
|
|
54
|
+
// Build rows for DataViewTable
|
|
55
|
+
const buildRows = (repositories) => repositories.map((repo) => (Object.assign({ row: [repo.name, repo.branches, repo.prs, repo.workspaces, repo.lastCommit], id: repo.name }, (repo.children ? { children: buildRows(repo.children) } : {}))));
|
|
56
|
+
const rows = buildRows(repositories);
|
|
57
|
+
const columns = ['Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit'];
|
|
58
|
+
const ouiaId = 'TreeTableExample';
|
|
59
|
+
describe('DataViewTableTree component', () => {
|
|
60
|
+
const mockSelection = {
|
|
61
|
+
onSelect: jest.fn(),
|
|
62
|
+
isSelected: jest.fn(),
|
|
63
|
+
isSelectDisabled: jest.fn(),
|
|
64
|
+
};
|
|
65
|
+
test('should render the tree table correctly', () => {
|
|
66
|
+
const { container } = render(React.createElement(DataView, { selection: mockSelection },
|
|
67
|
+
React.createElement(DataViewTable, { isTreeTable: true, "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, rows: rows, leafIcon: React.createElement(LeafIcon, null), expandedIcon: React.createElement(FolderOpenIcon, { "aria-hidden": true }), collapsedIcon: React.createElement(FolderIcon, { "aria-hidden": true }) })));
|
|
68
|
+
expect(container).toMatchSnapshot();
|
|
69
|
+
});
|
|
70
|
+
test('should render tree table with an empty state', () => {
|
|
71
|
+
const { container } = render(React.createElement(DataView, { activeState: "empty" },
|
|
72
|
+
React.createElement(DataViewTable, { isTreeTable: true, "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, bodyStates: { 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, bodyStates: { error: "Some error" }, rows: [] })));
|
|
78
|
+
expect(container).toMatchSnapshot();
|
|
79
|
+
});
|
|
80
|
+
test('should render tree table with a loading state', () => {
|
|
81
|
+
const { container } = render(React.createElement(DataView, { activeState: "loading" },
|
|
82
|
+
React.createElement(DataViewTable, { isTreeTable: true, "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, bodyStates: { loading: "Data is loading" }, rows: [] })));
|
|
83
|
+
expect(container).toMatchSnapshot();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -8,6 +8,8 @@ export interface DataViewToolbarProps extends PropsWithChildren {
|
|
|
8
8
|
bulkSelect?: React.ReactNode;
|
|
9
9
|
/** React component to display pagination */
|
|
10
10
|
pagination?: React.ReactNode;
|
|
11
|
+
/** React component to display actions */
|
|
12
|
+
actions?: React.ReactNode;
|
|
11
13
|
}
|
|
12
14
|
export declare const DataViewToolbar: React.FC<DataViewToolbarProps>;
|
|
13
15
|
export default DataViewToolbar;
|
|
@@ -12,10 +12,11 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
12
12
|
import React from 'react';
|
|
13
13
|
import { Toolbar, ToolbarContent, ToolbarItem, ToolbarItemVariant } from '@patternfly/react-core';
|
|
14
14
|
export const DataViewToolbar = (_a) => {
|
|
15
|
-
var { className, ouiaId = 'DataViewToolbar', bulkSelect, pagination, children } = _a, props = __rest(_a, ["className", "ouiaId", "bulkSelect", "pagination", "children"]);
|
|
15
|
+
var { className, ouiaId = 'DataViewToolbar', bulkSelect, actions, pagination, children } = _a, props = __rest(_a, ["className", "ouiaId", "bulkSelect", "actions", "pagination", "children"]);
|
|
16
16
|
return (React.createElement(Toolbar, Object.assign({ ouiaId: ouiaId, className: className }, props),
|
|
17
17
|
React.createElement(ToolbarContent, null,
|
|
18
18
|
bulkSelect && (React.createElement(ToolbarItem, { "data-ouia-component-id": `${ouiaId}-bulk-select` }, bulkSelect)),
|
|
19
|
+
actions && (React.createElement(ToolbarItem, null, actions)),
|
|
19
20
|
pagination && (React.createElement(ToolbarItem, { variant: ToolbarItemVariant.pagination, "data-ouia-component-id": `${ouiaId}-pagination` }, pagination)),
|
|
20
21
|
children)));
|
|
21
22
|
};
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
export declare enum PaginationParams {
|
|
2
|
-
PAGE = "page",
|
|
3
|
-
PER_PAGE = "perPage"
|
|
4
|
-
}
|
|
5
1
|
export interface UseDataViewPaginationProps {
|
|
6
2
|
/** Initial page */
|
|
7
3
|
page?: number;
|
|
@@ -11,12 +7,20 @@ export interface UseDataViewPaginationProps {
|
|
|
11
7
|
searchParams?: URLSearchParams;
|
|
12
8
|
/** Function to set search parameters */
|
|
13
9
|
setSearchParams?: (params: URLSearchParams) => void;
|
|
10
|
+
/** Custom URL parameter name for page */
|
|
11
|
+
pageParam?: string;
|
|
12
|
+
/** Custom URL parameter name for per page */
|
|
13
|
+
perPageParam?: string;
|
|
14
14
|
}
|
|
15
15
|
export interface DataViewPaginationProps extends UseDataViewPaginationProps {
|
|
16
16
|
/** Current page number */
|
|
17
17
|
page: number;
|
|
18
18
|
}
|
|
19
|
-
export declare
|
|
19
|
+
export declare enum PaginationParams {
|
|
20
|
+
PAGE = "page",
|
|
21
|
+
PER_PAGE = "perPage"
|
|
22
|
+
}
|
|
23
|
+
export declare const useDataViewPagination: ({ page, perPage, searchParams, setSearchParams, pageParam, perPageParam, }: UseDataViewPaginationProps) => {
|
|
20
24
|
onPerPageSelect: (_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined, newPerPage: number) => void;
|
|
21
25
|
onSetPage: (_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined, newPage: number) => void;
|
|
22
26
|
page: number;
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
2
|
export var PaginationParams;
|
|
3
3
|
(function (PaginationParams) {
|
|
4
4
|
PaginationParams["PAGE"] = "page";
|
|
5
5
|
PaginationParams["PER_PAGE"] = "perPage";
|
|
6
6
|
})(PaginationParams || (PaginationParams = {}));
|
|
7
|
-
export const useDataViewPagination = ({ page = 1, perPage, searchParams, setSearchParams, }) => {
|
|
7
|
+
export const useDataViewPagination = ({ page = 1, perPage, searchParams, setSearchParams, pageParam = PaginationParams.PAGE, perPageParam = PaginationParams.PER_PAGE, }) => {
|
|
8
8
|
const [state, setState] = useState({
|
|
9
|
-
page: parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(
|
|
10
|
-
perPage: parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(
|
|
9
|
+
page: parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(pageParam)) || `${page}`),
|
|
10
|
+
perPage: parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(perPageParam)) || `${perPage}`),
|
|
11
11
|
});
|
|
12
12
|
const updateSearchParams = (page, perPage) => {
|
|
13
13
|
if (searchParams && setSearchParams) {
|
|
14
14
|
const params = new URLSearchParams(searchParams);
|
|
15
|
-
params.set(
|
|
16
|
-
params.set(
|
|
15
|
+
params.set(pageParam, `${page}`);
|
|
16
|
+
params.set(perPageParam, `${perPage}`);
|
|
17
17
|
setSearchParams(params);
|
|
18
18
|
}
|
|
19
19
|
};
|
|
@@ -24,13 +24,13 @@ export const useDataViewPagination = ({ page = 1, perPage, searchParams, setSear
|
|
|
24
24
|
}, []);
|
|
25
25
|
useEffect(() => {
|
|
26
26
|
// Listen on URL params changes
|
|
27
|
-
const currentPage = parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(
|
|
28
|
-
const currentPerPage = parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(
|
|
27
|
+
const currentPage = parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(pageParam)) || `${state.page}`);
|
|
28
|
+
const currentPerPage = parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(perPageParam)) || `${state.perPage}`);
|
|
29
29
|
if (currentPage !== state.page || currentPerPage !== state.perPage) {
|
|
30
30
|
setState({ page: currentPage, perPage: currentPerPage });
|
|
31
31
|
}
|
|
32
32
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
33
|
-
}, [searchParams]);
|
|
33
|
+
}, [searchParams === null || searchParams === void 0 ? void 0 : searchParams.toString()]);
|
|
34
34
|
const onPerPageSelect = (_event, newPerPage) => {
|
|
35
35
|
updateSearchParams(1, newPerPage);
|
|
36
36
|
setState({ perPage: newPerPage, page: 1 });
|
|
@@ -44,56 +44,4 @@ describe('useDataViewPagination', () => {
|
|
|
44
44
|
perPage: 50
|
|
45
45
|
});
|
|
46
46
|
});
|
|
47
|
-
it('should read pagination state from URL', () => {
|
|
48
|
-
const mockSearchParams = new URLSearchParams('page=2&perPage=10');
|
|
49
|
-
const { result } = renderHook(() => useDataViewPagination({
|
|
50
|
-
searchParams: mockSearchParams,
|
|
51
|
-
setSearchParams: jest.fn(),
|
|
52
|
-
page: 1,
|
|
53
|
-
perPage: 5,
|
|
54
|
-
}));
|
|
55
|
-
expect(result.current).toEqual({
|
|
56
|
-
onPerPageSelect: expect.any(Function),
|
|
57
|
-
onSetPage: expect.any(Function),
|
|
58
|
-
page: 2,
|
|
59
|
-
perPage: 10,
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
it('should set pagination state in URL when page changes', () => {
|
|
63
|
-
const mockSetSearchParams = jest.fn();
|
|
64
|
-
const { result } = renderHook(() => useDataViewPagination({
|
|
65
|
-
searchParams: new URLSearchParams(),
|
|
66
|
-
setSearchParams: mockSetSearchParams,
|
|
67
|
-
page: 1,
|
|
68
|
-
perPage: 5,
|
|
69
|
-
}));
|
|
70
|
-
expect(mockSetSearchParams).toHaveBeenNthCalledWith(1, new URLSearchParams('page=1&perPage=5'));
|
|
71
|
-
act(() => {
|
|
72
|
-
result.current.onSetPage(undefined, 4);
|
|
73
|
-
});
|
|
74
|
-
expect(mockSetSearchParams).toHaveBeenNthCalledWith(2, new URLSearchParams('page=4&perPage=5'));
|
|
75
|
-
});
|
|
76
|
-
it('should set pagination state in URL when perPage changes', () => {
|
|
77
|
-
const mockSetSearchParams = jest.fn();
|
|
78
|
-
const { result } = renderHook(() => useDataViewPagination({
|
|
79
|
-
searchParams: new URLSearchParams('page=2'),
|
|
80
|
-
setSearchParams: mockSetSearchParams,
|
|
81
|
-
page: 1,
|
|
82
|
-
perPage: 5,
|
|
83
|
-
}));
|
|
84
|
-
act(() => {
|
|
85
|
-
result.current.onPerPageSelect(undefined, 20);
|
|
86
|
-
});
|
|
87
|
-
expect(mockSetSearchParams).toHaveBeenCalledWith(new URLSearchParams('page=1&perPage=20'));
|
|
88
|
-
});
|
|
89
|
-
it('should initialize URL with default values if not present', () => {
|
|
90
|
-
const mockSetSearchParams = jest.fn();
|
|
91
|
-
renderHook(() => useDataViewPagination({
|
|
92
|
-
searchParams: new URLSearchParams(),
|
|
93
|
-
setSearchParams: mockSetSearchParams,
|
|
94
|
-
page: 1,
|
|
95
|
-
perPage: 5,
|
|
96
|
-
}));
|
|
97
|
-
expect(mockSetSearchParams).toHaveBeenCalledWith(new URLSearchParams('page=1&perPage=5'));
|
|
98
|
-
});
|
|
99
47
|
});
|
|
@@ -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,25 @@
|
|
|
1
|
+
import React, { PropsWithChildren } from 'react';
|
|
2
|
+
import { DataViewState } from '../DataView';
|
|
3
|
+
export interface DataViewSelection {
|
|
4
|
+
/** Called when the selection of items changes */
|
|
5
|
+
onSelect: (isSelecting: boolean, items?: any[] | any) => void;
|
|
6
|
+
/** Checks if a specific item is currently selected */
|
|
7
|
+
isSelected: (item: any) => boolean;
|
|
8
|
+
/** Determines if selection is disabled for a given item */
|
|
9
|
+
isSelectDisabled?: (item: any) => boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface InternalContextProps {
|
|
12
|
+
/** Data selection props */
|
|
13
|
+
selection?: DataViewSelection;
|
|
14
|
+
/** Currently active state */
|
|
15
|
+
activeState?: DataViewState | string;
|
|
16
|
+
}
|
|
17
|
+
export interface InternalContextValue extends InternalContextProps {
|
|
18
|
+
/** Flag indicating if data view is selectable (auto-calculated) */
|
|
19
|
+
isSelectable: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare const InternalContext: React.Context<InternalContextValue>;
|
|
22
|
+
export type InternalProviderProps = PropsWithChildren<InternalContextProps>;
|
|
23
|
+
export declare const InternalContextProvider: React.FC<InternalProviderProps>;
|
|
24
|
+
export declare const useInternalContext: () => InternalContextValue;
|
|
25
|
+
export default InternalContext;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React, { createContext, useContext, useMemo } from 'react';
|
|
2
|
+
export const InternalContext = createContext({
|
|
3
|
+
selection: undefined,
|
|
4
|
+
activeState: undefined,
|
|
5
|
+
isSelectable: false,
|
|
6
|
+
});
|
|
7
|
+
export const InternalContextProvider = ({ children, selection, activeState }) => {
|
|
8
|
+
const isSelectable = useMemo(() => Boolean((selection === null || selection === void 0 ? void 0 : selection.onSelect) && (selection === null || selection === void 0 ? void 0 : selection.isSelected)), [selection === null || selection === void 0 ? void 0 : selection.onSelect, selection === null || selection === void 0 ? void 0 : selection.isSelected]);
|
|
9
|
+
return (React.createElement(InternalContext.Provider, { value: { selection, activeState, isSelectable } }, children));
|
|
10
|
+
};
|
|
11
|
+
export const useInternalContext = () => useContext(InternalContext);
|
|
12
|
+
export default InternalContext;
|