import { useState } from 'react'; import { ActionsColumn, Table, Thead, Tr, Th, Tbody, Td, ExpandableRowContent } from '@patternfly/react-table'; import { Button, Flex, FlexItem, MenuToggle, MenuToggleElement, Toolbar, ToolbarContent, ToolbarGroup, ToolbarItem, Pagination, PageSection, Select, SelectOption, PaginationVariant } from '@patternfly/react-core'; import RhUiCodeIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-code-icon'; import RhUiBranchFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-branch-fill-icon'; import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon'; import { DashboardWrapper } from '@patternfly/react-table/dist/esm/demos/DashboardWrapper'; import FilterIcon from '@patternfly/react-icons/dist/esm/icons/filter-icon'; export const TableCompoundExpansion: React.FunctionComponent = () => { // In real usage, this data would come from some external source like an API via props. const [isSelectOpen, setIsSelectOpen] = useState(false); const NestedItemsTable = () => { // In real usage, this data would come from some external source like an API via props. const items = [ { description: 'Item 1', date: 'May 9, 2018', status: 'Active' }, { description: 'Item 2', date: 'May 9, 2018', status: 'Warning' }, { description: 'Item 3', date: 'May 9, 2018', status: 'Active' }, { description: 'Item 4', date: 'May 9, 2018', status: 'Active' }, { description: 'Item 5', date: 'May 9, 2018', status: 'Active' } ]; const columnNames = { description: 'Description', date: 'Date', status: 'Status' }; return ( {items.map((item) => ( ))}
{columnNames.description} {columnNames.date} {columnNames.status}
{item.description} {item.date} {item.status}
); }; const renderPagination = (variant: 'top' | 'bottom' | PaginationVariant, isCompact: boolean) => ( ); const tableToolbar = ( {renderPagination('top', true)} ); const defaultActions = () => [ { title: 'Settings', // eslint-disable-next-line no-console onClick: () => console.log(`clicked on Settings`) }, { title: 'Help', // eslint-disable-next-line no-console onClick: () => console.log(`clicked on Help`) } ]; interface Repo { name: string; branches: number; prs: number; workspaces: number; lastCommit: string; } interface IDictionary { [Key: string]: T; } const repositories: Repo[] = [ { name: 'siemur/test-space', branches: 10, prs: 4, workspaces: 4, lastCommit: '20 minutes' }, { name: 'siemur/test-space-2', branches: 3, prs: 4, workspaces: 4, lastCommit: '20 minutes' } ]; const columnNames: IDictionary = { name: 'Repositories', branches: 'Branches', prs: 'Pull requests', workspaces: 'Workspaces', lastCommit: 'Last commit' }; // In this example, expanded cells are tracked by the repo and property names from each row. This could be any pair of unique identifiers. // This is to prevent state from being based on row and column order index in case we later add sorting and rearranging columns. // Note that this behavior is very similar to selection state. const [expandedCells, setExpandedCells] = useState>({ 'siemur/test-space': 'branches' // Default to the first cell of the first row being expanded }); const setCellExpanded = (repo: Repo, columnKey: string, isExpanding = true) => { const newExpandedCells: IDictionary = { ...expandedCells }; if (isExpanding) { newExpandedCells[repo.name] = columnKey; } else { delete newExpandedCells[repo.name]; } setExpandedCells(newExpandedCells); }; const compoundExpandParams = (repo: Repo, columnKey: string, rowIndex: number, columnIndex: number) => ({ isExpanded: expandedCells[repo.name] === columnKey, onToggle: () => setCellExpanded(repo, columnKey, expandedCells[repo.name] !== columnKey), expandId: 'compound-expandable-demo', rowIndex, columnIndex }); return ( {tableToolbar} {repositories.map((repo, rowIndex) => { const expandedCellKey = expandedCells[repo.name]; const isRowExpanded = !!expandedCellKey; return ( ); })}
{columnNames.name} {columnNames.branches} {columnNames.prs} {columnNames.workspaces} {columnNames.lastCommit}
{repo.name} {repo.branches} {repo.prs} {' '} {repo.workspaces} {repo.lastCommit} Open in GitHub
Expanded content for {repo.name}: prs goes here!
Expanded content for {repo.name}: workspaces goes here!
{renderPagination('bottom', false)}
); };