Debian Codegraph @

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

raw
1 import { 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 | null;
7 prs: string | null;
8 workspaces: string;
9 lastCommit: string;
10 }
11
12 export const TableClickable: 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: 'three', workspaces: 'four', lastCommit: 'five' },
16 { name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
17 { name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' }
18 ];
19
20 const columnNames = {
21 name: 'Repositories',
22 branches: 'Branches',
23 prs: 'Pull requests',
24 workspaces: 'Workspaces',
25 lastCommit: 'Last commit'
26 };
27
28 // In this example, selected rows are tracked by the repo names from each row. This could be any unique identifier.
29 // This is to prevent state from being based on row order index in case we later add sorting.
30 const [selectedRepoName, setSelectedRepoName] = useState('');
31
32 return (
33 <Table aria-label="Clickable table">
34 <Thead>
35 <Tr>
36 <Th>{columnNames.name}</Th>
37 <Th>{columnNames.branches}</Th>
38 <Th>{columnNames.prs}</Th>
39 <Th>{columnNames.workspaces}</Th>
40 <Th>{columnNames.lastCommit}</Th>
41 </Tr>
42 </Thead>
43 <Tbody>
44 {repositories.map((repo) => (
45 <Tr
46 key={repo.name}
47 onRowClick={() => setSelectedRepoName(repo.name)}
48 isSelectable
49 isClickable
50 isRowSelected={selectedRepoName === repo.name}
51 >
52 <Td dataLabel={columnNames.name}>{repo.name}</Td>
53 <Td dataLabel={columnNames.branches}>{repo.branches}</Td>
54 <Td dataLabel={columnNames.prs}>{repo.prs}</Td>
55 <Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td>
56 <Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td>
57 </Tr>
58 ))}
59 </Tbody>
60 </Table>
61 );
62 };