@patternfly/react-data-view 5.1.0 → 5.1.2
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 +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 +13 -0
- package/dist/cjs/DataViewTableBasic/DataViewTableBasic.js +49 -0
- package/dist/cjs/DataViewTableBasic/DataViewTableBasic.test.d.ts +1 -0
- package/dist/cjs/DataViewTableBasic/DataViewTableBasic.test.js +27 -0
- package/dist/cjs/DataViewTableBasic/index.d.ts +2 -0
- package/dist/cjs/DataViewTableBasic/index.js +23 -0
- package/dist/cjs/DataViewTableHeader/DataViewTableHeader.d.ts +13 -0
- package/dist/cjs/DataViewTableHeader/DataViewTableHeader.js +57 -0
- package/dist/cjs/DataViewTableHeader/DataViewTableHeader.test.d.ts +1 -0
- package/dist/cjs/DataViewTableHeader/DataViewTableHeader.test.js +36 -0
- package/dist/cjs/DataViewTableHeader/index.d.ts +2 -0
- package/dist/cjs/DataViewTableHeader/index.js +23 -0
- package/dist/cjs/DataViewTableTree/DataViewTableTree.d.ts +19 -0
- package/dist/cjs/DataViewTableTree/DataViewTableTree.js +120 -0
- package/dist/cjs/DataViewTableTree/DataViewTableTree.test.d.ts +1 -0
- package/dist/cjs/DataViewTableTree/DataViewTableTree.test.js +75 -0
- package/dist/cjs/DataViewTableTree/index.d.ts +2 -0
- package/dist/cjs/DataViewTableTree/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 +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/DataViewTableHeader/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 +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 +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 +13 -0
- package/dist/esm/DataViewTableBasic/DataViewTableBasic.js +42 -0
- package/dist/esm/DataViewTableBasic/DataViewTableBasic.test.d.ts +1 -0
- package/dist/esm/DataViewTableBasic/DataViewTableBasic.test.js +22 -0
- package/dist/esm/DataViewTableBasic/index.d.ts +2 -0
- package/dist/esm/DataViewTableBasic/index.js +2 -0
- package/dist/esm/DataViewTableHeader/DataViewTableHeader.d.ts +13 -0
- package/dist/esm/DataViewTableHeader/DataViewTableHeader.js +30 -0
- package/dist/esm/DataViewTableHeader/DataViewTableHeader.test.d.ts +1 -0
- package/dist/esm/DataViewTableHeader/DataViewTableHeader.test.js +31 -0
- package/dist/esm/DataViewTableHeader/index.d.ts +2 -0
- package/dist/esm/DataViewTableHeader/index.js +2 -0
- package/dist/esm/DataViewTableTree/DataViewTableTree.d.ts +19 -0
- package/dist/esm/DataViewTableTree/DataViewTableTree.js +93 -0
- package/dist/esm/DataViewTableTree/DataViewTableTree.test.d.ts +1 -0
- package/dist/esm/DataViewTableTree/DataViewTableTree.test.js +70 -0
- package/dist/esm/DataViewTableTree/index.d.ts +2 -0
- package/dist/esm/DataViewTableTree/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 +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/package.json +1 -1
- package/patternfly-docs/content/extensions/data-view/examples/Components/Components.md +45 -3
- package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableExample.tsx +47 -0
- package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableTreeExample.tsx +69 -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 +15 -32
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/SelectionExample.tsx +15 -46
- package/patternfly-docs/content/extensions/data-view/examples/Layout/Layout.md +1 -0
- package/patternfly-docs/content/extensions/data-view/examples/Layout/PredefinedLayoutExample.tsx +20 -51
- 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 +80 -0
- package/src/DataViewTable/DataViewTable.tsx +37 -0
- package/src/DataViewTable/__snapshots__/DataViewTable.test.tsx.snap +1010 -0
- package/src/DataViewTable/index.ts +2 -0
- package/src/DataViewTableBasic/DataViewTableBasic.test.tsx +37 -0
- package/src/DataViewTableBasic/DataViewTableBasic.tsx +71 -0
- package/src/DataViewTableBasic/__snapshots__/DataViewTableBasic.test.tsx.snap +324 -0
- package/src/DataViewTableBasic/index.ts +2 -0
- package/src/DataViewTableHeader/DataViewTableHeader.test.tsx +52 -0
- package/src/DataViewTableHeader/DataViewTableHeader.tsx +53 -0
- package/src/DataViewTableHeader/__snapshots__/DataViewTableHeader.test.tsx.snap +227 -0
- package/src/DataViewTableHeader/index.ts +2 -0
- package/src/DataViewTableTree/DataViewTableTree.test.tsx +85 -0
- package/src/DataViewTableTree/DataViewTableTree.tsx +144 -0
- package/src/DataViewTableTree/__snapshots__/DataViewTableTree.test.tsx.snap +937 -0
- package/src/DataViewTableTree/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 +89 -0
- package/src/InternalContext/InternalContext.tsx +35 -0
- package/src/InternalContext/index.ts +2 -0
- package/src/index.ts +18 -0
|
@@ -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,41 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
import { TdProps, ThProps, TrProps } from '@patternfly/react-table';
|
|
3
|
+
import { DataViewTableTreeProps } from '../DataViewTableTree';
|
|
4
|
+
import { DataViewTableBasicProps } from '../DataViewTableBasic';
|
|
5
|
+
export type DataViewTh = ReactNode | {
|
|
6
|
+
cell: ReactNode;
|
|
7
|
+
props?: ThProps;
|
|
8
|
+
};
|
|
9
|
+
export declare const isDataViewThObject: (value: DataViewTh) => value is {
|
|
10
|
+
cell: ReactNode;
|
|
11
|
+
props?: ThProps | undefined;
|
|
12
|
+
};
|
|
13
|
+
export interface DataViewTrObject {
|
|
14
|
+
row: DataViewTd[];
|
|
15
|
+
id?: string;
|
|
16
|
+
props?: TrProps;
|
|
17
|
+
}
|
|
18
|
+
export type DataViewTd = ReactNode | {
|
|
19
|
+
cell: ReactNode;
|
|
20
|
+
props?: TdProps;
|
|
21
|
+
};
|
|
22
|
+
export type DataViewTr = DataViewTd[] | DataViewTrObject;
|
|
23
|
+
export declare const isDataViewTdObject: (value: DataViewTd) => value is {
|
|
24
|
+
cell: ReactNode;
|
|
25
|
+
props?: TdProps | undefined;
|
|
26
|
+
};
|
|
27
|
+
export declare const isDataViewTrObject: (value: DataViewTr) => value is {
|
|
28
|
+
row: DataViewTd[];
|
|
29
|
+
id?: string | undefined;
|
|
30
|
+
};
|
|
31
|
+
export interface DataViewTrTree extends DataViewTrObject {
|
|
32
|
+
id: string;
|
|
33
|
+
children?: DataViewTrTree[];
|
|
34
|
+
}
|
|
35
|
+
export type DataViewTableProps = ({
|
|
36
|
+
isTreeTable: true;
|
|
37
|
+
} & DataViewTableTreeProps) | ({
|
|
38
|
+
isTreeTable?: false;
|
|
39
|
+
} & DataViewTableBasicProps);
|
|
40
|
+
export declare const DataViewTable: React.FC<DataViewTableProps>;
|
|
41
|
+
export default DataViewTable;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { DataViewTableTree } from '../DataViewTableTree';
|
|
3
|
+
import { DataViewTableBasic } from '../DataViewTableBasic';
|
|
4
|
+
export const isDataViewThObject = (value) => value != null && typeof value === 'object' && 'cell' in value;
|
|
5
|
+
export const isDataViewTdObject = (value) => value != null && typeof value === 'object' && 'cell' in value;
|
|
6
|
+
export const isDataViewTrObject = (value) => value != null && typeof value === 'object' && 'row' in value;
|
|
7
|
+
export const DataViewTable = (props) => (props.isTreeTable ? React.createElement(DataViewTableTree, Object.assign({}, props)) : React.createElement(DataViewTableBasic, Object.assign({}, props)));
|
|
8
|
+
export default DataViewTable;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
import { DataViewTable } from './DataViewTable';
|
|
4
|
+
const repositories = [
|
|
5
|
+
{ name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' },
|
|
6
|
+
{ name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
|
|
7
|
+
{ name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' },
|
|
8
|
+
{ name: 'Repository four', branches: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' },
|
|
9
|
+
{ name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' },
|
|
10
|
+
{ name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' }
|
|
11
|
+
];
|
|
12
|
+
const rows = repositories.map(repo => ({ row: Object.values(repo) }));
|
|
13
|
+
const repositoriesTree = [
|
|
14
|
+
{
|
|
15
|
+
name: 'Repository one',
|
|
16
|
+
branches: 'Branch one',
|
|
17
|
+
prs: 'Pull request one',
|
|
18
|
+
workspaces: 'Workspace one',
|
|
19
|
+
lastCommit: 'Timestamp one',
|
|
20
|
+
children: [
|
|
21
|
+
{ name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
|
|
22
|
+
{ name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' },
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: 'Repository four',
|
|
27
|
+
branches: 'Branch four',
|
|
28
|
+
prs: 'Pull request four',
|
|
29
|
+
workspaces: 'Workspace four',
|
|
30
|
+
lastCommit: 'Timestamp four',
|
|
31
|
+
children: [{ name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' }]
|
|
32
|
+
},
|
|
33
|
+
{ name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' }
|
|
34
|
+
];
|
|
35
|
+
const buildRows = (repositories) => repositories.map((repo) => (Object.assign({ row: [repo.name, repo.branches, repo.prs, repo.workspaces, repo.lastCommit], id: repo.name }, (repo.children
|
|
36
|
+
? {
|
|
37
|
+
children: buildRows(repo.children) // build rows for children
|
|
38
|
+
}
|
|
39
|
+
: {}))));
|
|
40
|
+
const treeRows = buildRows(repositoriesTree);
|
|
41
|
+
const columns = ['Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit'];
|
|
42
|
+
const ouiaId = 'TableExample';
|
|
43
|
+
describe('DataViewTable component', () => {
|
|
44
|
+
test('should render a basic table correctly', () => {
|
|
45
|
+
const { container } = render(React.createElement(DataViewTable, { "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, rows: rows }));
|
|
46
|
+
expect(container).toMatchSnapshot();
|
|
47
|
+
});
|
|
48
|
+
test('should render a tree table correctly', () => {
|
|
49
|
+
const { container } = render(React.createElement(DataViewTable, { isTreeTable: true, "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, rows: treeRows }));
|
|
50
|
+
expect(container).toMatchSnapshot();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { TableProps } from '@patternfly/react-table';
|
|
3
|
+
import { DataViewTh, DataViewTr } from '../DataViewTable';
|
|
4
|
+
export interface DataViewTableBasicProps extends Omit<TableProps, 'onSelect' | 'rows'> {
|
|
5
|
+
/** Columns definition */
|
|
6
|
+
columns: DataViewTh[];
|
|
7
|
+
/** Current page rows */
|
|
8
|
+
rows: DataViewTr[];
|
|
9
|
+
/** Custom OUIA ID */
|
|
10
|
+
ouiaId?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare const DataViewTableBasic: React.FC<DataViewTableBasicProps>;
|
|
13
|
+
export default DataViewTableBasic;
|
|
@@ -0,0 +1,42 @@
|
|
|
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, Tr, } from '@patternfly/react-table';
|
|
14
|
+
import { useInternalContext } from '../InternalContext';
|
|
15
|
+
import { DataViewTableHeader } from '../DataViewTableHeader';
|
|
16
|
+
import { isDataViewTdObject, isDataViewTrObject } from '../DataViewTable';
|
|
17
|
+
export const DataViewTableBasic = (_a) => {
|
|
18
|
+
var { columns, rows, ouiaId = 'DataViewTableBasic' } = _a, props = __rest(_a, ["columns", "rows", "ouiaId"]);
|
|
19
|
+
const { selection } = useInternalContext();
|
|
20
|
+
const { onSelect, isSelected, isSelectDisabled } = selection !== null && selection !== void 0 ? selection : {};
|
|
21
|
+
return (React.createElement(Table, Object.assign({ "aria-label": "Data table", ouiaId: ouiaId }, props),
|
|
22
|
+
React.createElement(DataViewTableHeader, { columns: columns, ouiaId: ouiaId }),
|
|
23
|
+
React.createElement(Tbody, null, rows.map((row, rowIndex) => {
|
|
24
|
+
var _a;
|
|
25
|
+
const rowIsObject = isDataViewTrObject(row);
|
|
26
|
+
return (React.createElement(Tr, Object.assign({ key: rowIndex, ouiaId: `${ouiaId}-tr-${rowIndex}` }, (rowIsObject && ((_a = row === null || row === void 0 ? void 0 : row.props) !== null && _a !== void 0 ? _a : {}))),
|
|
27
|
+
onSelect && isSelected && (React.createElement(Td, { key: `select-${rowIndex}`, select: {
|
|
28
|
+
rowIndex,
|
|
29
|
+
onSelect: (_event, isSelecting) => {
|
|
30
|
+
onSelect === null || onSelect === void 0 ? void 0 : onSelect(isSelecting, rowIsObject ? row : [row]);
|
|
31
|
+
},
|
|
32
|
+
isSelected: (isSelected === null || isSelected === void 0 ? void 0 : isSelected(row)) || false,
|
|
33
|
+
isDisabled: (isSelectDisabled === null || isSelectDisabled === void 0 ? void 0 : isSelectDisabled(row)) || false,
|
|
34
|
+
} })),
|
|
35
|
+
(rowIsObject ? row.row : row).map((cell, colIndex) => {
|
|
36
|
+
var _a;
|
|
37
|
+
const cellIsObject = isDataViewTdObject(cell);
|
|
38
|
+
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));
|
|
39
|
+
})));
|
|
40
|
+
}))));
|
|
41
|
+
};
|
|
42
|
+
export default DataViewTableBasic;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
import { DataViewTableBasic } from './DataViewTableBasic';
|
|
4
|
+
const repositories = [
|
|
5
|
+
{ name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' },
|
|
6
|
+
{ name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
|
|
7
|
+
{ name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' },
|
|
8
|
+
{ name: 'Repository four', branches: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' },
|
|
9
|
+
{ name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' },
|
|
10
|
+
{ name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' }
|
|
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(DataViewTableBasic, { "aria-label": 'Repositories table', ouiaId: ouiaId, columns: columns, rows: rows }));
|
|
20
|
+
expect(container).toMatchSnapshot();
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { TheadProps } from '@patternfly/react-table';
|
|
3
|
+
import { DataViewTh } from '../DataViewTable';
|
|
4
|
+
export interface DataViewTableHeaderProps 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 DataViewTableHeader: React.FC<DataViewTableHeaderProps>;
|
|
13
|
+
export default DataViewTableHeader;
|
|
@@ -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 DataViewTableHeader = (_a) => {
|
|
17
|
+
var { isTreeTable = false, columns, ouiaId = 'DataViewTableHeader' } = _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 header 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 DataViewTableHeader;
|
|
@@ -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 { DataViewTableHeader } from './DataViewTableHeader';
|
|
5
|
+
import { DataView } from '../DataView';
|
|
6
|
+
const columns = ['Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit'];
|
|
7
|
+
const ouiaId = 'HeaderExample';
|
|
8
|
+
describe('DataViewTableHeader 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(DataViewTableHeader, { 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(DataViewTableHeader, { 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(DataViewTableHeader, { columns: columns, ouiaId: ouiaId }))));
|
|
29
|
+
expect(container).toMatchSnapshot();
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { TableProps } from '@patternfly/react-table';
|
|
3
|
+
import { DataViewTh, DataViewTrTree } from '../DataViewTable';
|
|
4
|
+
export interface DataViewTableTreeProps extends Omit<TableProps, 'onSelect' | 'rows'> {
|
|
5
|
+
/** Columns definition */
|
|
6
|
+
columns: DataViewTh[];
|
|
7
|
+
/** Current page rows */
|
|
8
|
+
rows: DataViewTrTree[];
|
|
9
|
+
/** Optional icon for the leaf rows */
|
|
10
|
+
leafIcon?: React.ReactNode;
|
|
11
|
+
/** Optional icon for the expanded parent rows */
|
|
12
|
+
expandedIcon?: React.ReactNode;
|
|
13
|
+
/** Optional icon for the collapsed parent rows */
|
|
14
|
+
collapsedIcon?: React.ReactNode;
|
|
15
|
+
/** Custom OUIA ID */
|
|
16
|
+
ouiaId?: string;
|
|
17
|
+
}
|
|
18
|
+
export declare const DataViewTableTree: React.FC<DataViewTableTreeProps>;
|
|
19
|
+
export default DataViewTableTree;
|
|
@@ -0,0 +1,93 @@
|
|
|
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 { DataViewTableHeader } from '../DataViewTableHeader';
|
|
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, leafIcon = null, expandedIcon = null, collapsedIcon = null, ouiaId = 'DataViewTableTree' } = _a, props = __rest(_a, ["columns", "rows", "leafIcon", "expandedIcon", "collapsedIcon", "ouiaId"]);
|
|
33
|
+
const { selection } = 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 nodes = useMemo(() => {
|
|
38
|
+
const renderRows = ([node, ...remainingNodes], level = 1, posinset = 1, rowIndex = 0, isHidden = false) => {
|
|
39
|
+
var _a, _b, _c, _d;
|
|
40
|
+
if (!node) {
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
const isExpanded = expandedNodeIds.includes(node.id);
|
|
44
|
+
const isDetailsExpanded = expandedDetailsNodeNames.includes(node.id);
|
|
45
|
+
const isChecked = isSelected && isNodeChecked(node, isSelected);
|
|
46
|
+
let icon = leafIcon;
|
|
47
|
+
if (node.children) {
|
|
48
|
+
icon = isExpanded ? expandedIcon : collapsedIcon;
|
|
49
|
+
}
|
|
50
|
+
const treeRow = {
|
|
51
|
+
onCollapse: () => setExpandedNodeIds(prevExpanded => {
|
|
52
|
+
const otherExpandedNodeIds = prevExpanded.filter(id => id !== node.id);
|
|
53
|
+
return isExpanded ? otherExpandedNodeIds : [...otherExpandedNodeIds, node.id];
|
|
54
|
+
}),
|
|
55
|
+
onToggleRowDetails: () => setExpandedDetailsNodeIds(prevDetailsExpanded => {
|
|
56
|
+
const otherDetailsExpandedNodeIds = prevDetailsExpanded.filter(id => id !== node.id);
|
|
57
|
+
return isDetailsExpanded ? otherDetailsExpandedNodeIds : [...otherDetailsExpandedNodeIds, node.id];
|
|
58
|
+
}),
|
|
59
|
+
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)),
|
|
60
|
+
rowIndex,
|
|
61
|
+
props: {
|
|
62
|
+
isExpanded,
|
|
63
|
+
isDetailsExpanded,
|
|
64
|
+
isHidden,
|
|
65
|
+
'aria-level': level,
|
|
66
|
+
'aria-posinset': posinset,
|
|
67
|
+
'aria-setsize': (_b = (_a = node.children) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0,
|
|
68
|
+
isChecked,
|
|
69
|
+
ouiaId: `${ouiaId}-tree-toggle-${node.id}`,
|
|
70
|
+
checkboxId: `checkbox_id_${(_c = node.id) === null || _c === void 0 ? void 0 : _c.toLowerCase().replace(/\s+/g, '_')}`,
|
|
71
|
+
icon,
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
const childRows = ((_d = node.children) === null || _d === void 0 ? void 0 : _d.length)
|
|
75
|
+
? renderRows(node.children, level + 1, 1, rowIndex + 1, !isExpanded || isHidden)
|
|
76
|
+
: [];
|
|
77
|
+
return [
|
|
78
|
+
React.createElement(TreeRowWrapper, { key: node.id, row: { props: treeRow.props } }, node.row.map((cell, colIndex) => {
|
|
79
|
+
var _a;
|
|
80
|
+
const cellIsObject = isDataViewTdObject(cell);
|
|
81
|
+
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));
|
|
82
|
+
})),
|
|
83
|
+
...childRows,
|
|
84
|
+
...renderRows(remainingNodes, level, posinset + 1, rowIndex + 1 + childRows.length, isHidden),
|
|
85
|
+
];
|
|
86
|
+
};
|
|
87
|
+
return renderRows(rows);
|
|
88
|
+
}, [rows, expandedNodeIds, expandedDetailsNodeNames, leafIcon, expandedIcon, collapsedIcon, isSelected, onSelect, isSelectDisabled, ouiaId]);
|
|
89
|
+
return (React.createElement(Table, Object.assign({ isTreeTable: true, "aria-label": "Data table", ouiaId: ouiaId }, props),
|
|
90
|
+
React.createElement(DataViewTableHeader, { isTreeTable: true, columns: columns, ouiaId: ouiaId }),
|
|
91
|
+
React.createElement(Tbody, null, nodes)));
|
|
92
|
+
};
|
|
93
|
+
export default DataViewTableTree;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
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
|
+
});
|
|
@@ -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,
|