Debian Codegraph @

node/@patternfly/react-core/src/demos/examples/Tabs/TabsAndTable.tsx

raw
1 import { Fragment, useRef, useState } from 'react';
2 /* eslint-disable no-console */
3 import {
4 Button,
5 Divider,
6 Drawer,
7 DrawerContent,
8 DrawerContentBody,
9 DrawerPanelContent,
10 DrawerHead,
11 DrawerActions,
12 DrawerCloseButton,
13 DrawerPanelBody,
14 Dropdown,
15 DropdownList,
16 Flex,
17 FlexItem,
18 Label,
19 LabelGroup,
20 MenuToggle,
21 OverflowMenu,
22 OverflowMenuContent,
23 OverflowMenuControl,
24 OverflowMenuGroup,
25 OverflowMenuItem,
26 PageSection,
27 Progress,
28 ProgressSize,
29 Tabs,
30 Tab,
31 TabContent,
32 TabContentBody,
33 TabTitleText,
34 Title,
35 Toolbar,
36 ToolbarItem,
37 ToolbarContent,
38 ToolbarToggleGroup
39 } from '@patternfly/react-core';
40 import {
41 Table,
42 Thead,
43 Tbody,
44 Tr,
45 Th,
46 Td,
47 IAction,
48 ActionsColumn,
49 CustomActionsToggleProps
50 } from '@patternfly/react-table';
51 import RhUiCodeIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-code-icon';
52 import RhUiEllipsisVerticalFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-ellipsis-vertical-fill-icon';
53 import RhUiBranchFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-branch-fill-icon';
54 import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon';
55 import FilterIcon from '@patternfly/react-icons/dist/esm/icons/filter-icon';
56 import RhMicronsSortDownLargeToSmallIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-sort-down-large-to-small-icon';
57 import { KeyTypes } from '../../../helpers';
58 import { DashboardWrapper } from '@patternfly/react-core/src/demos/DashboardWrapper';
59
60 interface Repository {
61 name: string;
62 branches: number | null;
63 prs: number | null;
64 workspaces: number;
65 lastCommit: string;
66 }
67
68 export const TablesAndTabs = () => {
69 // secondary tab properties
70 const [secondaryActiveTabKey, setSecondaryActiveTabKey] = useState<number>(10);
71 const handleSecondaryTabClick = (tabIndex: number) => {
72 setSecondaryActiveTabKey(tabIndex);
73 };
74
75 // drawer properties
76 const [isExpanded, setIsExpanded] = useState<boolean>(true);
77
78 // table properties
79 // In real usage, this data would come from some external source like an API via props.
80 const repositories: Repository[] = [
81 { name: 'Node 1', branches: 10, prs: 25, workspaces: 5, lastCommit: '2 days ago' },
82 { name: 'Node 2', branches: 8, prs: 30, workspaces: 2, lastCommit: '2 days ago' },
83 { name: 'Node 3', branches: 12, prs: 48, workspaces: 13, lastCommit: '30 days ago' },
84 { name: 'Node 4', branches: 3, prs: 8, workspaces: 20, lastCommit: '8 days ago' },
85 { name: 'Node 5', branches: 33, prs: 21, workspaces: 2, lastCommit: '26 days ago' }
86 ];
87
88 const columnNames = {
89 name: 'Repositories',
90 branches: 'Branches',
91 prs: 'Pull requests',
92 workspaces: 'Workspaces',
93 lastCommit: 'Last commit'
94 };
95
96 const [selectedRepoName, setSelectedRepoName] = useState<string>(repositories[0].name);
97 const isRepoSelected = (repo: Repository) => repo.name === selectedRepoName;
98 const setRepoSelected = (_event: React.FormEvent<HTMLInputElement>, repo: Repository) => {
99 setSelectedRepoName(repo.name);
100 setIsExpanded(true);
101 };
102
103 const defaultActions: IAction[] = [
104 {
105 title: 'Some action',
106 onClick: (event) => {
107 event.stopPropagation();
108 console.log('clicked on Some action');
109 }
110 },
111 {
112 title: <a href="https://www.patternfly.org">Link action</a>,
113 onClick: (event) => {
114 event.stopPropagation();
115 console.log('clicked on Link action');
116 }
117 },
118 {
119 isSeparator: true
120 },
121 {
122 title: 'Third action',
123 onClick: (event) => {
124 event.stopPropagation();
125 console.log('clicked on Third action');
126 }
127 }
128 ];
129
130 const firstActionRef = useRef<HTMLButtonElement>(null);
131
132 /** Handles when the user clicks on the custom action toggle, stops propagation to prevent the drawer from opening */
133 const handleActionsToggleClick = (event: React.MouseEvent, ActionsToggleProps: CustomActionsToggleProps) => {
134 const { onToggle } = ActionsToggleProps;
135
136 onToggle(event);
137 event.stopPropagation();
138 };
139
140 /** Enables keyboard navigation of the custom actions toggle */
141 const handleActionsToggleKeyDown = (event: React.KeyboardEvent, ActionsToggleProps: CustomActionsToggleProps) => {
142 const { onToggle } = ActionsToggleProps;
143 const { Enter, Space, Escape, ArrowDown, ArrowUp } = KeyTypes;
144
145 const shouldToggle = [Enter, Space, Escape].includes(event.key);
146 const shouldFocus = [ArrowDown, ArrowUp, Enter, Space].includes(event.key);
147
148 if (shouldToggle) {
149 onToggle(event);
150 }
151
152 if (shouldFocus) {
153 setTimeout(() => {
154 firstActionRef.current?.focus();
155 }, 0);
156 }
157 };
158
159 const handleDrawerClose = () => {
160 setIsExpanded(false);
161 setSelectedRepoName('');
162 };
163
164 const customActionsToggle = (props: CustomActionsToggleProps, toggleName: string) => (
165 <MenuToggle
166 isDisabled={props.isDisabled}
167 onClick={(event: React.MouseEvent) => handleActionsToggleClick(event, props)}
168 onKeyDown={(event: React.KeyboardEvent) => handleActionsToggleKeyDown(event, props)}
169 variant="plain"
170 aria-label={`${toggleName} actions`}
171 aria-haspopup="menu"
172 isExpanded={props.isOpen}
173 ref={props.toggleRef}
174 icon={<RhUiEllipsisVerticalFillIcon />}
175 />
176 );
177
178 const toolbar = (
179 <Toolbar id="page-layout-table-column-management-action-toolbar-top">
180 <ToolbarContent>
181 <ToolbarToggleGroup toggleIcon={<FilterIcon />} breakpoint="xl">
182 <ToolbarItem>
183 <MenuToggle>Name</MenuToggle>
184 </ToolbarItem>
185 </ToolbarToggleGroup>
186 <ToolbarItem>
187 <MenuToggle variant="plain" aria-label="Sort columns" icon={<RhMicronsSortDownLargeToSmallIcon />} />
188 </ToolbarItem>
189 <OverflowMenu breakpoint="md">
190 <OverflowMenuContent>
191 <OverflowMenuGroup groupType="button" isPersistent>
192 <OverflowMenuItem isPersistent>
193 <Button variant="primary">Generate</Button>
194 </OverflowMenuItem>
195 <OverflowMenuItem isPersistent>
196 <Button variant="secondary">Deploy</Button>
197 </OverflowMenuItem>
198 </OverflowMenuGroup>
199 </OverflowMenuContent>
200 <OverflowMenuControl hasAdditionalOptions>
201 <Dropdown
202 onSelect={() => {}}
203 toggle={(toggleRef) => (
204 <MenuToggle
205 ref={toggleRef}
206 aria-label="overflow menu"
207 variant="plain"
208 onClick={() => {}}
209 isExpanded={false}
210 icon={<RhUiEllipsisVerticalFillIcon />}
211 />
212 )}
213 isOpen={false}
214 >
215 <DropdownList>{[]}</DropdownList>
216 </Dropdown>
217 </OverflowMenuControl>
218 </OverflowMenu>
219 </ToolbarContent>
220 </Toolbar>
221 );
222
223 const table = (
224 <Table aria-label="`Composable` table">
225 <Thead noWrap>
226 <Tr>
227 <Th screenReaderText="Row select" />
228 <Th>{columnNames.name}</Th>
229 <Th>{columnNames.branches}</Th>
230 <Th>{columnNames.prs}</Th>
231 <Th>{columnNames.workspaces}</Th>
232 <Th>{columnNames.lastCommit}</Th>
233 </Tr>
234 </Thead>
235 <Tbody>
236 {repositories.map((repo, rowIndex) => (
237 <Tr key={repo.name}>
238 <Td
239 key={`${rowIndex}_0`}
240 select={{
241 rowIndex,
242 onSelect: (event) => setRepoSelected(event, repo),
243 isSelected: isRepoSelected(repo),
244 variant: 'radio'
245 }}
246 />
247 <Td dataLabel={columnNames.name}>
248 {repo.name}
249 <div>
250 {/** Preventing default behavior for demo purposes only */}
251 <a onClick={(event) => event.preventDefault()} href="#">
252 siemur/test-space
253 </a>
254 </div>
255 </Td>
256 <Td dataLabel={columnNames.branches}>
257 <Flex>
258 <FlexItem>{repo.branches}</FlexItem>
259 <FlexItem>
260 <RhUiBranchFillIcon key="icon" />
261 </FlexItem>
262 </Flex>
263 </Td>
264 <Td dataLabel={columnNames.prs}>
265 <Flex>
266 <FlexItem>{repo.prs}</FlexItem>
267 <FlexItem>
268 <RhUiCodeIcon key="icon" />
269 </FlexItem>
270 </Flex>
271 </Td>
272 <Td dataLabel={columnNames.workspaces}>
273 <Flex>
274 <FlexItem>{repo.workspaces}</FlexItem>
275 <FlexItem>
276 <CubeIcon key="icon" />
277 </FlexItem>
278 </Flex>
279 </Td>
280 <Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td>
281 <Td key={`${rowIndex}_5`}>
282 <ActionsColumn
283 items={defaultActions}
284 actionsToggle={(props: CustomActionsToggleProps) => customActionsToggle(props, repo.name)}
285 firstActionItemRef={firstActionRef}
286 />
287 </Td>
288 </Tr>
289 ))}
290 </Tbody>
291 </Table>
292 );
293
294 const panelContent = (
295 <DrawerPanelContent widths={{ default: 'width_33', xl: 'width_33' }}>
296 <DrawerHead>
297 <DrawerActions>
298 <DrawerCloseButton onClick={handleDrawerClose} />
299 </DrawerActions>
300 <Flex spaceItems={{ default: 'spaceItemsSm' }} direction={{ default: 'column' }}>
301 <FlexItem>
302 <Title headingLevel="h2" size="lg">
303 {selectedRepoName}
304 </Title>
305 </FlexItem>
306 <FlexItem>
307 {/** Preventing default behavior for demo purposes only */}
308 <a onClick={(event) => event.preventDefault()} href="#">
309 siemur/test-space
310 </a>
311 </FlexItem>
312 </Flex>
313 </DrawerHead>
314 <DrawerPanelBody hasNoPadding>
315 <Tabs
316 activeKey={secondaryActiveTabKey}
317 onSelect={(_event, tabIndex) => handleSecondaryTabClick(Number(tabIndex))}
318 isBox
319 isFilled
320 id="tabs-tables-secondary-tabs"
321 >
322 <Tab eventKey={10} title={<TabTitleText>Overview</TabTitleText>} tabContentId={`tabContent${10}`} />
323 <Tab eventKey={11} title={<TabTitleText>Activity</TabTitleText>} tabContentId={`tabContent${11}`} />
324 </Tabs>
325 </DrawerPanelBody>
326 <DrawerPanelBody>
327 <TabContent
328 key={10}
329 eventKey={10}
330 id={`tabContent${10}`}
331 activeKey={secondaryActiveTabKey}
332 hidden={10 !== secondaryActiveTabKey}
333 >
334 <TabContentBody>
335 <Flex direction={{ default: 'column' }} spaceItems={{ default: 'spaceItemsLg' }}>
336 <FlexItem>
337 <p>
338 The content of the drawer really is up to you. It could have form fields, definition lists, text
339 lists, labels, charts, progress bars, etc. Spacing recommendation is 24px margins. You can put tabs in
340 here, and can also make the drawer scrollable.
341 </p>
342 </FlexItem>
343 <FlexItem>
344 <Progress value={33} title="Capacity" size={ProgressSize.sm} />
345 </FlexItem>
346 <FlexItem>
347 <Progress value={66} title="Modules" size={ProgressSize.sm} />
348 </FlexItem>
349 <Flex direction={{ default: 'column' }}>
350 <FlexItem>
351 <Title headingLevel="h3">Tags</Title>
352 </FlexItem>
353 <FlexItem>
354 <LabelGroup>
355 {[1, 2, 3, 4, 5].map((labelNumber) => (
356 <Label variant="outline" key={`label-${labelNumber}`}>{`Tag ${labelNumber}`}</Label>
357 ))}
358 </LabelGroup>
359 </FlexItem>
360 </Flex>
361 </Flex>
362 </TabContentBody>
363 </TabContent>
364 <TabContent
365 key={11}
366 eventKey={11}
367 id={`tabContent${11}`}
368 activeKey={secondaryActiveTabKey}
369 hidden={11 !== secondaryActiveTabKey}
370 >
371 <TabContentBody>Activity panel</TabContentBody>
372 </TabContent>
373 </DrawerPanelBody>
374 </DrawerPanelContent>
375 );
376
377 const tabContent = (
378 <Drawer isExpanded={isExpanded} isInline>
379 <DrawerContent panelContent={panelContent}>
380 <DrawerContentBody>{table}</DrawerContentBody>
381 </DrawerContent>
382 </Drawer>
383 );
384
385 return (
386 <DashboardWrapper>
387 <Fragment>
388 <PageSection aria-labelledby="nodes">
389 <Title headingLevel="h1" size="2xl" id="nodes">
390 Nodes
391 </Title>
392 </PageSection>
393 <PageSection padding={{ default: 'noPadding' }} aria-label="Nodes toolbar and content">
394 {toolbar}
395 <Divider />
396 {tabContent}
397 </PageSection>
398 </Fragment>
399 </DashboardWrapper>
400 );
401 };