/* eslint-disable no-console */ import { Fragment, useState } from 'react'; import { Button, MenuToggle, ToggleGroup, ToggleGroupItem, ToggleGroupItemProps } from '@patternfly/react-core'; import { Table, TableText, Thead, Tr, Th, Tbody, Td, CustomActionsToggleProps, ActionsColumn, IAction } from '@patternfly/react-table'; interface Repository { name: string; branches: string; prs: string; workspaces: string; lastCommit: string; singleAction: string; } type ExampleType = 'customToggle'; export const TableActions: React.FunctionComponent = () => { // In real usage, this data would come from some external source like an API via props. const repositories: Repository[] = [ { name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five', singleAction: 'Start' }, { name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five', singleAction: '' }, { name: 'p', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five', singleAction: 'Start' }, { name: '4', branches: '2', prs: 'b', workspaces: 'four', lastCommit: 'five', singleAction: 'Start' }, { name: '5', branches: '2', prs: 'b', workspaces: 'four', lastCommit: 'five', singleAction: 'Start' } ]; const columnNames = { name: 'Repositories', branches: 'Branches', prs: 'Pull requests', workspaces: 'Workspaces', lastCommit: 'Last commit', singleAction: 'Single action' }; // This state is just for the ToggleGroup in this example and isn't necessary for Table usage. const [exampleChoice, setExampleChoice] = useState(''); const onExampleTypeChange: ToggleGroupItemProps['onChange'] = (event, _isSelected) => { const id = event.currentTarget.id; // Allow toggling off if clicking the already selected item setExampleChoice(exampleChoice === id ? '' : (id as ExampleType)); }; const customActionsToggle = (props: CustomActionsToggleProps) => ( Actions ); const defaultActions = (repo: Repository): IAction[] => [ { title: 'Some action', onClick: () => console.log(`clicked on Some action, on row ${repo.name}`) }, { title: Link action }, { isSeparator: true }, { title: 'Third action', onClick: () => console.log(`clicked on Third action, on row ${repo.name}`) } ]; const lastRowActions = (repo: Repository): IAction[] => [ { title: 'Some action', onClick: () => console.log(`clicked on Some action, on row ${repo.name}`) }, { title:
Another action
, onClick: () => console.log(`clicked on Another action, on row ${repo.name}`) }, { isSeparator: true }, { title: 'Third action', onClick: () => console.log(`clicked on Third action, on row ${repo.name}`) } ]; return ( {repositories.map((repo) => { // Arbitrary logic to determine which rows get which actions in this example let rowActions: IAction[] | null = defaultActions(repo); if (repo.name === 'a') { rowActions = null; } if (repo.name === '5') { rowActions = lastRowActions(repo); } let singleActionButton: React.ReactNode = null; if (repo.singleAction !== '') { singleActionButton = ( ); } return ( ); })}
{columnNames.name} {columnNames.branches} {columnNames.prs} {columnNames.workspaces} {columnNames.lastCommit}
{repo.name} {repo.branches} {repo.prs} {repo.workspaces} {repo.lastCommit} {singleActionButton} {rowActions ? ( ) : null}
); };