/* eslint-disable no-console */ import { useState } from 'react'; import { Table, Thead, Tr, Th, Tbody, Td, ExpandableRowContent, ActionsColumn, IAction } from '@patternfly/react-table'; interface Repository { name: string; branches: string; prs: string; nestedComponent?: React.ReactNode; link?: React.ReactNode; noPadding?: boolean; } interface NestedRepository { name: string; branches: string | null; prs: string | null; workspaces: string | null; lastCommit: string | null; } const NestedReposTable: React.FunctionComponent = () => { // In real usage, this data would come from some external source like an API via props. const prs: NestedRepository[] = [ { name: 'Repository 1', branches: '25', prs: '25', workspaces: '5', lastCommit: '2 days ago' }, { name: 'Repository 2', branches: '25', prs: '25', workspaces: '5', lastCommit: '2 days ago' }, { name: 'Repository 3', branches: '25', prs: '25', workspaces: '5', lastCommit: '2 days ago' }, { name: 'Repository 4', branches: '25', prs: '25', workspaces: '5', lastCommit: '2 days ago' } ]; const columnNames = { name: 'Repositories', branches: 'Branches', prs: 'Pull requests', workspaces: 'Workspaces', lastCommit: 'Last commit' }; return ( {prs.map((repo) => ( ))}
{columnNames.name} {columnNames.branches} {columnNames.prs} {columnNames.workspaces} {columnNames.lastCommit}
{repo.name} {repo.branches} {repo.prs} {repo.workspaces} {repo.lastCommit}
); }; export const TableExpandable: React.FunctionComponent = () => { // 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: '2', nestedComponent: , link: Link 1 }, { name: 'Node 2', branches: '3', prs: '4', link: Link 2 }, { name: 'Node 3', branches: '11', prs: '7', nestedComponent: (

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

), link: Link 3 }, { name: 'Node 4', branches: '11', prs: '7', nestedComponent: 'Expandable row content has no padding.', link: Link 4, noPadding: true } ]; const columnNames = { name: 'Repositories', branches: 'Branches', prs: 'Pull requests', link: 'Link', action: 'Action' }; // In this example, expanded rows are tracked by the repo names from each row. This could be any unique identifier. // This is to prevent state from being based on row order index in case we later add sorting. // Note that this behavior is very similar to selection state. const initialExpandedRepoNames = repositories.filter((repo) => !!repo.nestedComponent).map((repo) => repo.name); // Default to all expanded const [expandedRepoNames, setExpandedRepoNames] = useState(initialExpandedRepoNames); const setRepoExpanded = (repo: Repository, isExpanding = true) => setExpandedRepoNames((prevExpanded) => { const otherExpandedRepoNames = prevExpanded.filter((r) => r !== repo.name); return isExpanding ? [...otherExpandedRepoNames, repo.name] : otherExpandedRepoNames; }); const isRepoExpanded = (repo: Repository) => expandedRepoNames.includes(repo.name); 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}`) } ]; return ( {repositories.map((repo, rowIndex) => ( {repo.nestedComponent ? ( ) : null} ))}
{columnNames.name} {columnNames.branches} {columnNames.prs} {columnNames.link} {columnNames.action}
setRepoExpanded(repo, !isRepoExpanded(repo)), expandId: 'composable-nested-table-expandable-example' } : undefined } /> {repo.name} {repo.branches} {repo.prs} {repo.link}
{repo.nestedComponent}
); };