Debian Codegraph @

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

raw
1 import { Fragment, useState } from 'react';
2 import { ToggleGroup, ToggleGroupItem, ToggleGroupItemProps } from '@patternfly/react-core';
3 import { Table, Caption, Thead, Tr, Th, Tbody, Td } from '@patternfly/react-table';
4
5 interface Repository {
6 name: string;
7 branches: string | null;
8 prs: string | null;
9 workspaces: string;
10 lastCommit: string;
11 }
12
13 type ExampleType = 'compact' | 'compactBorderless';
14
15 export const TableBasic: 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: 'three', workspaces: 'four', lastCommit: 'five' },
19 { name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
20 { name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' }
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 // This state is just for the ToggleGroup in this example and isn't necessary for Table usage.
32 const [exampleChoice, setExampleChoice] = useState<ExampleType | ''>('');
33 const onExampleTypeChange: ToggleGroupItemProps['onChange'] = (event, _isSelected) => {
34 const id = event.currentTarget.id;
35 // Allow toggling off if clicking the already selected item
36 setExampleChoice(exampleChoice === id ? '' : (id as ExampleType));
37 };
38
39 return (
40 <Fragment>
41 <ToggleGroup aria-label="Default with single selectable">
42 <ToggleGroupItem
43 text="Compact"
44 buttonId="compact"
45 isSelected={exampleChoice === 'compact'}
46 onChange={onExampleTypeChange}
47 />
48 <ToggleGroupItem
49 text="Compact borderless"
50 buttonId="compactBorderless"
51 isSelected={exampleChoice === 'compactBorderless'}
52 onChange={onExampleTypeChange}
53 />
54 </ToggleGroup>
55 <Table
56 aria-label="Simple table"
57 variant={exampleChoice === 'compact' || exampleChoice === 'compactBorderless' ? 'compact' : undefined}
58 borders={exampleChoice !== 'compactBorderless'}
59 >
60 <Caption>Simple table using composable components</Caption>
61 <Thead>
62 <Tr>
63 <Th>{columnNames.name}</Th>
64 <Th>{columnNames.branches}</Th>
65 <Th>{columnNames.prs}</Th>
66 <Th>{columnNames.workspaces}</Th>
67 <Th>{columnNames.lastCommit}</Th>
68 </Tr>
69 </Thead>
70 <Tbody>
71 {repositories.map((repo) => (
72 <Tr key={repo.name}>
73 <Td dataLabel={columnNames.name}>{repo.name}</Td>
74 <Td dataLabel={columnNames.branches}>{repo.branches}</Td>
75 <Td dataLabel={columnNames.prs}>{repo.prs}</Td>
76 <Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td>
77 <Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td>
78 </Tr>
79 ))}
80 </Tbody>
81 </Table>
82 </Fragment>
83 );
84 };