Debian Codegraph @

node/@patternfly/react-table/src/deprecated/components/Table/examples/LegacyTableSelectable.tsx

raw
1 import { useEffect, useState } from 'react';
2 import { headerCol } from '@patternfly/react-table';
3 import { Table, TableHeader, TableBody, TableProps } from '@patternfly/react-table/deprecated';
4 import { Checkbox } from '@patternfly/react-core';
5 import spacing from '@patternfly/react-styles/css/utilities/Spacing/spacing';
6
7 interface Repository {
8 name: string;
9 branches: string;
10 prs: string;
11 workspaces: string;
12 lastCommit: string;
13 }
14
15 export const LegacyTableSelectable: React.FunctionComponent = () => {
16 // In real usage, this data would come from some external source like an API via props.
17 const repositories: Repository[] = [
18 { name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five' },
19 { name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' },
20 { name: 'b', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' },
21 { name: 'c', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' },
22 { name: 'd', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' },
23 { name: 'e', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five' }
24 ];
25
26 const isRepoSelectable = (repo: Repository) => repo.name !== 'a'; // Arbitrary logic for this example
27 const selectableRepos = repositories.filter(isRepoSelectable);
28
29 // In this example, selected rows are tracked by the repo names from each row. This could be any unique identifier.
30 // This is to prevent state from being based on row order index in case we later add sorting.
31 const [selectedRepoNames, setSelectedRepoNames] = useState<string[]>([]);
32 const setRepoSelected = (repo: Repository, isSelecting = true) =>
33 setSelectedRepoNames((prevSelected) => {
34 const otherSelectedRepoNames = prevSelected.filter((r) => r !== repo.name);
35 return isSelecting && isRepoSelectable(repo) ? [...otherSelectedRepoNames, repo.name] : otherSelectedRepoNames;
36 });
37 const selectAllRepos = (isSelecting = true) =>
38 setSelectedRepoNames(isSelecting ? selectableRepos.map((r) => r.name) : []);
39 const isRepoSelected = (repo: Repository) => selectedRepoNames.includes(repo.name);
40
41 const [canSelectAll, setCanSelectAll] = useState(true);
42
43 // To allow shift+click to select/deselect multiple rows
44 const [recentSelectedRowIndex, setRecentSelectedRowIndex] = useState<number | null>(null);
45 const [shifting, setShifting] = useState(false);
46
47 const onSelectRepo = (repo: Repository, rowIndex: number, isSelecting: boolean) => {
48 // If the user is shift + selecting the checkboxes, then all intermediate checkboxes should be selected
49 if (shifting && recentSelectedRowIndex !== null) {
50 const numberSelected = rowIndex - recentSelectedRowIndex;
51 const intermediateIndexes =
52 numberSelected > 0
53 ? Array.from(new Array(numberSelected + 1), (_x, i) => i + recentSelectedRowIndex)
54 : Array.from(new Array(Math.abs(numberSelected) + 1), (_x, i) => i + rowIndex);
55 intermediateIndexes.forEach((index) => setRepoSelected(repositories[index], isSelecting));
56 } else {
57 setRepoSelected(repo, isSelecting);
58 }
59 setRecentSelectedRowIndex(rowIndex);
60 };
61
62 useEffect(() => {
63 const onKeyDown = (e: KeyboardEvent) => {
64 if (e.key === 'Shift') {
65 setShifting(true);
66 }
67 };
68 const onKeyUp = (e: KeyboardEvent) => {
69 if (e.key === 'Shift') {
70 setShifting(false);
71 }
72 };
73
74 document.addEventListener('keydown', onKeyDown);
75 document.addEventListener('keyup', onKeyUp);
76
77 return () => {
78 document.removeEventListener('keydown', onKeyDown);
79 document.removeEventListener('keyup', onKeyUp);
80 };
81 }, []);
82
83 const columns: TableProps['cells'] = [
84 { title: 'Repositories', cellTransforms: [headerCol()] },
85 'Branches',
86 { title: 'Pull requests' },
87 'Workspaces',
88 'Last commit'
89 ];
90 const rows: TableProps['rows'] = repositories.map((repo) => ({
91 cells: [repo.name, repo.branches, repo.prs, repo.workspaces, repo.lastCommit],
92 selected: isRepoSelected(repo),
93 disableSelection: !isRepoSelectable(repo)
94 }));
95
96 return (
97 <div>
98 <Checkbox
99 label="Can select all"
100 className={spacing.mbLg}
101 isChecked={canSelectAll}
102 onChange={(_event, checked) => setCanSelectAll(checked)}
103 aria-label="toggle select all checkbox"
104 id="toggle-select-all"
105 name="toggle-select-all"
106 />
107 <Table
108 onSelect={(_event, isSelecting, rowIndex) => {
109 if (rowIndex === -1) {
110 selectAllRepos(isSelecting);
111 } else {
112 const repo = repositories[rowIndex];
113 onSelectRepo(repo, rowIndex, isSelecting);
114 }
115 }}
116 canSelectAll={canSelectAll}
117 aria-label="Selectable Table"
118 cells={columns}
119 rows={rows}
120 >
121 <TableHeader />
122 <TableBody />
123 </Table>
124 </div>
125 );
126 };