import { useState } from 'react'; import { Table, Thead, Tr, Th, Tbody, Td } from '@patternfly/react-table'; interface Repository { name: string; branches: string; prs: string; workspaces: string; lastCommit: string; } export const TableSelectableRadio: 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' }, { name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' }, { name: 'p', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five' } ]; const columnNames = { name: 'Repositories', branches: 'Branches', prs: 'Pull requests', workspaces: 'Workspaces', lastCommit: 'Last commit' }; const isRepoSelectable = (repo: Repository) => repo.name !== 'a'; // Arbitrary logic for this example // In this example, selected 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. const [selectedRepoName, setSelectedRepoName] = useState(null); return ( {repositories.map((repo, rowIndex) => ( ))}
{columnNames.name} {columnNames.branches} {columnNames.prs} {columnNames.workspaces} {columnNames.lastCommit}
setSelectedRepoName(repo.name), isSelected: selectedRepoName === repo.name, isDisabled: !isRepoSelectable(repo), variant: 'radio' }} /> {repo.name} {repo.branches} {repo.prs} {repo.workspaces} {repo.lastCommit}
); };