Debian Codegraph @

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

raw
1 /* eslint-disable no-console */
2 import { Fragment, useState } from 'react';
3 import { Button, MenuToggle, ToggleGroup, ToggleGroupItem, ToggleGroupItemProps } from '@patternfly/react-core';
4 import {
5 Table,
6 TableText,
7 Thead,
8 Tr,
9 Th,
10 Tbody,
11 Td,
12 CustomActionsToggleProps,
13 ActionsColumn,
14 IAction
15 } from '@patternfly/react-table';
16
17 interface Repository {
18 name: string;
19 branches: string;
20 prs: string;
21 workspaces: string;
22 lastCommit: string;
23 singleAction: string;
24 }
25
26 type ExampleType = 'customToggle';
27
28 export const TableActions: React.FunctionComponent = () => {
29 // In real usage, this data would come from some external source like an API via props.
30 const repositories: Repository[] = [
31 { name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five', singleAction: 'Start' },
32 { name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five', singleAction: '' },
33 { name: 'p', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five', singleAction: 'Start' },
34 { name: '4', branches: '2', prs: 'b', workspaces: 'four', lastCommit: 'five', singleAction: 'Start' },
35 { name: '5', branches: '2', prs: 'b', workspaces: 'four', lastCommit: 'five', singleAction: 'Start' }
36 ];
37
38 const columnNames = {
39 name: 'Repositories',
40 branches: 'Branches',
41 prs: 'Pull requests',
42 workspaces: 'Workspaces',
43 lastCommit: 'Last commit',
44 singleAction: 'Single action'
45 };
46
47 // This state is just for the ToggleGroup in this example and isn't necessary for Table usage.
48 const [exampleChoice, setExampleChoice] = useState<ExampleType | ''>('');
49 const onExampleTypeChange: ToggleGroupItemProps['onChange'] = (event, _isSelected) => {
50 const id = event.currentTarget.id;
51 // Allow toggling off if clicking the already selected item
52 setExampleChoice(exampleChoice === id ? '' : (id as ExampleType));
53 };
54
55 const customActionsToggle = (props: CustomActionsToggleProps) => (
56 <MenuToggle ref={props.toggleRef} onClick={props.onToggle} isDisabled={props.isDisabled}>
57 Actions
58 </MenuToggle>
59 );
60
61 const defaultActions = (repo: Repository): IAction[] => [
62 {
63 title: 'Some action',
64 onClick: () => console.log(`clicked on Some action, on row ${repo.name}`)
65 },
66 {
67 title: <a href="https://www.patternfly.org">Link action</a>
68 },
69 {
70 isSeparator: true
71 },
72 {
73 title: 'Third action',
74 onClick: () => console.log(`clicked on Third action, on row ${repo.name}`)
75 }
76 ];
77
78 const lastRowActions = (repo: Repository): IAction[] => [
79 {
80 title: 'Some action',
81 onClick: () => console.log(`clicked on Some action, on row ${repo.name}`)
82 },
83 {
84 title: <div>Another action</div>,
85 onClick: () => console.log(`clicked on Another action, on row ${repo.name}`)
86 },
87 {
88 isSeparator: true
89 },
90 {
91 title: 'Third action',
92 onClick: () => console.log(`clicked on Third action, on row ${repo.name}`)
93 }
94 ];
95
96 return (
97 <Fragment>
98 <ToggleGroup aria-label="Default uses kebab toggle">
99 <ToggleGroupItem
100 text="Custom actions toggle"
101 buttonId="customToggle"
102 isSelected={exampleChoice === 'customToggle'}
103 onChange={onExampleTypeChange}
104 />
105 </ToggleGroup>
106 <Table aria-label="Actions table">
107 <Thead>
108 <Tr>
109 <Th>{columnNames.name}</Th>
110 <Th>{columnNames.branches}</Th>
111 <Th>{columnNames.prs}</Th>
112 <Th>{columnNames.workspaces}</Th>
113 <Th>{columnNames.lastCommit}</Th>
114 <Th screenReaderText="Primary action" />
115 <Th screenReaderText="Secondary action" />
116 </Tr>
117 </Thead>
118 <Tbody>
119 {repositories.map((repo) => {
120 // Arbitrary logic to determine which rows get which actions in this example
121 let rowActions: IAction[] | null = defaultActions(repo);
122 if (repo.name === 'a') {
123 rowActions = null;
124 }
125 if (repo.name === '5') {
126 rowActions = lastRowActions(repo);
127 }
128 let singleActionButton: React.ReactNode = null;
129 if (repo.singleAction !== '') {
130 singleActionButton = (
131 <TableText>
132 <Button variant="secondary">{repo.singleAction}</Button>
133 </TableText>
134 );
135 }
136
137 return (
138 <Tr key={repo.name}>
139 <Td dataLabel={columnNames.name}>{repo.name}</Td>
140 <Td dataLabel={columnNames.branches}>{repo.branches}</Td>
141 <Td dataLabel={columnNames.prs}>{repo.prs}</Td>
142 <Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td>
143 <Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td>
144 <Td dataLabel={columnNames.singleAction} modifier="fitContent" hasAction>
145 {singleActionButton}
146 </Td>
147 <Td isActionCell>
148 {rowActions ? (
149 <ActionsColumn
150 items={rowActions}
151 isDisabled={repo.name === '4'} // Also arbitrary for the example
152 actionsToggle={exampleChoice === 'customToggle' ? customActionsToggle : undefined}
153 />
154 ) : null}
155 </Td>
156 </Tr>
157 );
158 })}
159 </Tbody>
160 </Table>
161 </Fragment>
162 );
163 };