Debian Codegraph @

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

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