/* eslint-disable no-console */ import React from 'react'; import { Button, Divider, Drawer, DrawerContent, DrawerContentBody, DrawerPanelContent, DrawerHead, DrawerActions, DrawerCloseButton, DrawerPanelBody, Dropdown, DropdownList, Flex, FlexItem, Label, LabelGroup, MenuToggle, OverflowMenu, OverflowMenuContent, OverflowMenuControl, OverflowMenuGroup, OverflowMenuItem, PageSection, PageSectionVariants, Progress, ProgressSize, Tabs, Tab, TabContent, TabContentBody, TabTitleText, Title, Toolbar, ToolbarItem, ToolbarContent, ToolbarToggleGroup } from '@breakaway/preact-core'; import { Table, Thead, Tbody, Tr, Th, Td, IAction, ActionsColumn, CustomActionsToggleProps } from '@patternfly/react-table'; import EllipsisVIcon from '@patternfly/react-icons/dist/esm/icons/ellipsis-v-icon'; import DashboardWrapper from '@breakaway/preact-core/src/demos/examples/DashboardWrapper'; import CodeIcon from '@patternfly/react-icons/dist/esm/icons/code-icon'; import CodeBranchIcon from '@patternfly/react-icons/dist/esm/icons/code-branch-icon'; import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon'; import FilterIcon from '@patternfly/react-icons/dist/esm/icons/filter-icon'; import SortAmountDownIcon from '@patternfly/react-icons/dist/esm/icons/sort-amount-down-icon'; import { KeyTypes } from '../../../helpers'; interface Repository { name: string; branches: number | null; prs: number | null; workspaces: number; lastCommit: string; } export const TablesAndTabs = () => { // secondary tab properties const [secondaryActiveTabKey, setSecondaryActiveTabKey] = React.useState(10); const handleSecondaryTabClick = (tabIndex: number) => { setSecondaryActiveTabKey(tabIndex); }; // drawer properties const [isExpanded, setIsExpanded] = React.useState(true); // table properties // In real usage, this data would come from some external source like an API via props. const repositories: Repository[] = [ { name: 'Node 1', branches: 10, prs: 25, workspaces: 5, lastCommit: '2 days ago' }, { name: 'Node 2', branches: 8, prs: 30, workspaces: 2, lastCommit: '2 days ago' }, { name: 'Node 3', branches: 12, prs: 48, workspaces: 13, lastCommit: '30 days ago' }, { name: 'Node 4', branches: 3, prs: 8, workspaces: 20, lastCommit: '8 days ago' }, { name: 'Node 5', branches: 33, prs: 21, workspaces: 2, lastCommit: '26 days ago' } ]; const columnNames = { name: 'Repositories', branches: 'Branches', prs: 'Pull requests', workspaces: 'Workspaces', lastCommit: 'Last commit' }; const [selectedRepoName, setSelectedRepoName] = React.useState(repositories[0].name); const isRepoSelected = (repo: Repository) => repo.name === selectedRepoName; const setRepoSelected = (_event: React.FormEvent, repo: Repository) => { setSelectedRepoName(repo.name); setIsExpanded(true); }; const defaultActions: IAction[] = [ { title: 'Some action', onClick: (event) => { event.stopPropagation(); console.log('clicked on Some action'); } }, { title: Link action, onClick: (event) => { event.stopPropagation(); console.log('clicked on Link action'); } }, { isSeparator: true }, { title: 'Third action', onClick: (event) => { event.stopPropagation(); console.log('clicked on Third action'); } } ]; const firstActionRef = React.useRef(null); /** Handles when the user clicks on the custom action toggle, stops propagation to prevent the drawer from opening */ const handleActionsToggleClick = (event: React.MouseEvent, ActionsToggleProps: CustomActionsToggleProps) => { const { onToggle } = ActionsToggleProps; onToggle(event); event.stopPropagation(); }; /** Enables keyboard navigation of the custom actions toggle */ const handleActionsToggleKeyDown = (event: React.KeyboardEvent, ActionsToggleProps: CustomActionsToggleProps) => { const { onToggle } = ActionsToggleProps; const { Enter, Space, Escape, ArrowDown, ArrowUp } = KeyTypes; const shouldToggle = [Enter, Space, Escape].includes(event.key); const shouldFocus = [ArrowDown, ArrowUp, Enter, Space].includes(event.key); if (shouldToggle) { onToggle(event); } if (shouldFocus) { setTimeout(() => { firstActionRef.current?.focus(); }, 0); } }; const handleDrawerClose = () => { setIsExpanded(false); setSelectedRepoName(''); }; const customActionsToggle = (props: CustomActionsToggleProps, toggleName: string) => ( handleActionsToggleClick(event, props)} onKeyDown={(event: React.KeyboardEvent) => handleActionsToggleKeyDown(event, props)} variant="plain" aria-label={`${toggleName} actions`} aria-haspopup="menu" isExpanded={props.isOpen} ref={props.toggleRef} > ); const toolbar = ( } breakpoint="xl"> Name {}} toggle={(toggleRef) => ( {}} isExpanded={false} > )} isOpen={false} > {[]} ); const table = ( {repositories.map((repo, rowIndex) => ( ))}
{columnNames.name} {columnNames.branches} {columnNames.prs} {columnNames.workspaces} {columnNames.lastCommit}
setRepoSelected(event, repo), isSelected: isRepoSelected(repo), variant: 'radio' }} /> {repo.name}
{/** Preventing default behavior for demo purposes only */} event.preventDefault()} href="#"> siemur/test-space
{repo.branches} {repo.prs} {repo.workspaces} {repo.lastCommit} customActionsToggle(props, repo.name)} firstActionItemRef={firstActionRef} />
); const panelContent = ( {selectedRepoName} {/** Preventing default behavior for demo purposes only */} event.preventDefault()} href="#"> siemur/test-space handleSecondaryTabClick(Number(tabIndex))} isBox isFilled id="tabs-tables-secondary-tabs" > Overview} tabContentId={`tabContent${10}`} /> Activity} tabContentId={`tabContent${11}`} /> ); const tabContent = ( {table} ); return ( Nodes {toolbar} {tabContent} ); };