|
| 1 | +import React, { useContext, useEffect, useState, useRef } from 'react'; |
| 2 | +import { Drawer, DrawerActions, DrawerCloseButton, DrawerContent, DrawerContentBody, DrawerHead, DrawerPanelContent, Title, Text } from '@patternfly/react-core'; |
| 3 | +import { Table, Tbody, Th, Thead, Tr, Td } from '@patternfly/react-table'; |
| 4 | +import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView'; |
| 5 | +import DataViewContext, { DataViewProvider, EventTypes } from '@patternfly/react-data-view/dist/dynamic/DataViewContext'; |
| 6 | + |
| 7 | +interface Repository { |
| 8 | + name: string; |
| 9 | + branches: string | null; |
| 10 | + prs: string | null; |
| 11 | + workspaces: string; |
| 12 | + lastCommit: string; |
| 13 | +} |
| 14 | + |
| 15 | +const repositories: Repository[] = [ |
| 16 | + { name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' }, |
| 17 | + { name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' }, |
| 18 | + { name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' }, |
| 19 | + { name: 'one - 4', branches: 'two - 4', prs: 'null', workspaces: 'four - 4', lastCommit: 'five - 4' }, |
| 20 | + { name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' }, |
| 21 | + { name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' } |
| 22 | +]; |
| 23 | + |
| 24 | +const cols: Record<keyof Repository, string> = { |
| 25 | + name: 'Repositories', |
| 26 | + branches: 'Branches', |
| 27 | + prs: 'Pull requests', |
| 28 | + workspaces: 'Workspaces', |
| 29 | + lastCommit: 'Last commit' |
| 30 | +}; |
| 31 | + |
| 32 | +const ouiaId = 'ContextExample'; |
| 33 | + |
| 34 | +interface RepositoryDetailProps { |
| 35 | + selectedRepo?: Repository; |
| 36 | + setSelectedRepo: React.Dispatch<React.SetStateAction<Repository | undefined>>; |
| 37 | +} |
| 38 | + |
| 39 | +const RepositoryDetail: React.FunctionComponent<RepositoryDetailProps> = ({ selectedRepo, setSelectedRepo }) => { |
| 40 | + const context = useContext(DataViewContext); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + const unsubscribe = context.subscribe(EventTypes.rowClick, (repo: Repository) => { |
| 44 | + setSelectedRepo(repo); |
| 45 | + }); |
| 46 | + |
| 47 | + return () => unsubscribe(); |
| 48 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 49 | + }, []); |
| 50 | + |
| 51 | + return ( |
| 52 | + <DrawerPanelContent> |
| 53 | + <DrawerHead> |
| 54 | + <Title className="pf-v5-u-mb-md" headingLevel="h2"> |
| 55 | + Detail of repository {selectedRepo?.name} |
| 56 | + </Title> |
| 57 | + <Text>Branches: {selectedRepo?.branches}</Text> |
| 58 | + <Text>Pull requests: {selectedRepo?.prs}</Text> |
| 59 | + <Text>Workspaces: {selectedRepo?.workspaces}</Text> |
| 60 | + <Text>Last commit: {selectedRepo?.lastCommit}</Text> |
| 61 | + <DrawerActions> |
| 62 | + <DrawerCloseButton onClick={() => setSelectedRepo(undefined)} /> |
| 63 | + </DrawerActions> |
| 64 | + </DrawerHead> |
| 65 | + </DrawerPanelContent> |
| 66 | + ); |
| 67 | +}; |
| 68 | + |
| 69 | +interface RepositoriesTableProps { |
| 70 | + selectedRepo?: Repository; |
| 71 | +} |
| 72 | + |
| 73 | +const RepositoriesTable: React.FunctionComponent<RepositoriesTableProps> = ({ selectedRepo = undefined }) => { |
| 74 | + const { trigger } = useContext(DataViewContext); |
| 75 | + |
| 76 | + const handleRowClick = (repo: Repository | undefined) => { |
| 77 | + trigger(EventTypes.rowClick, repo); |
| 78 | + }; |
| 79 | + |
| 80 | + return ( |
| 81 | + <DataView> |
| 82 | + <Table aria-label="Repositories table" ouiaId={ouiaId}> |
| 83 | + <Thead data-ouia-component-id={`${ouiaId}-thead`}> |
| 84 | + <Tr ouiaId={`${ouiaId}-tr-head`}> |
| 85 | + {Object.values(cols).map((column, index) => ( |
| 86 | + <Th key={index} data-ouia-component-id={`${ouiaId}-th-${index}`}> |
| 87 | + {column} |
| 88 | + </Th> |
| 89 | + ))} |
| 90 | + </Tr> |
| 91 | + </Thead> |
| 92 | + <Tbody> |
| 93 | + {repositories.map((repo, rowIndex) => ( |
| 94 | + <Tr |
| 95 | + isClickable |
| 96 | + key={repo.name} |
| 97 | + ouiaId={`${ouiaId}-tr-${rowIndex}`} |
| 98 | + onRowClick={() => handleRowClick(selectedRepo?.name === repo.name ? undefined : repo)} |
| 99 | + isRowSelected={selectedRepo?.name === repo.name} |
| 100 | + > |
| 101 | + {Object.keys(cols).map((column, colIndex) => ( |
| 102 | + <Td key={colIndex} data-ouia-component-id={`${ouiaId}-td-${rowIndex}-${colIndex}`}> |
| 103 | + {repo[column as keyof Repository]} |
| 104 | + </Td> |
| 105 | + ))} |
| 106 | + </Tr> |
| 107 | + ))} |
| 108 | + </Tbody> |
| 109 | + </Table> |
| 110 | + </DataView> |
| 111 | + ); |
| 112 | +}; |
| 113 | + |
| 114 | +export const BasicExample: React.FunctionComponent = () => { |
| 115 | + const [ selectedRepo, setSelectedRepo ] = useState<Repository>(); |
| 116 | + const drawerRef = useRef<HTMLDivElement>(null); |
| 117 | + |
| 118 | + return ( |
| 119 | + <DataViewProvider> |
| 120 | + <Drawer isExpanded={Boolean(selectedRepo)} onExpand={() => drawerRef.current?.focus()}> |
| 121 | + <DrawerContent |
| 122 | + panelContent={<RepositoryDetail selectedRepo={selectedRepo} setSelectedRepo={setSelectedRepo} />} |
| 123 | + > |
| 124 | + <DrawerContentBody> |
| 125 | + <RepositoriesTable selectedRepo={selectedRepo} /> |
| 126 | + </DrawerContentBody> |
| 127 | + </DrawerContent> |
| 128 | + </Drawer> |
| 129 | + </DataViewProvider> |
| 130 | + ); |
| 131 | +}; |
0 commit comments