| 1 |
import { useState } from 'react'; |
| 2 |
import { headerCol } from '@patternfly/react-table'; |
| 3 |
import { Table, TableHeader, TableBody, TableProps } from '@patternfly/react-table/deprecated'; |
| 4 |
|
| 5 |
interface Repository { |
| 6 |
name: string; |
| 7 |
branches: string; |
| 8 |
prs: string; |
| 9 |
workspaces: string; |
| 10 |
lastCommit: string; |
| 11 |
} |
| 12 |
|
| 13 |
export const LegacyTableSelectableRadio: React.FunctionComponent = () => { |
| 14 |
// In real usage, this data would come from some external source like an API via props. |
| 15 |
const repositories: Repository[] = [ |
| 16 |
{ name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five' }, |
| 17 |
{ name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' }, |
| 18 |
{ name: 'p', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five' } |
| 19 |
]; |
| 20 |
|
| 21 |
const isRepoSelectable = (repo: Repository) => repo.name !== 'a'; // Arbitrary logic for this example |
| 22 |
|
| 23 |
// In this example, selected rows are tracked by the repo names from each row. This could be any unique identifier. |
| 24 |
// This is to prevent state from being based on row order index in case we later add sorting. |
| 25 |
const [selectedRepoName, setSelectedRepoName] = useState<string | null>(null); |
| 26 |
|
| 27 |
const columns: TableProps['cells'] = [ |
| 28 |
{ title: 'Repositories', cellTransforms: [headerCol('selectable-radio')] }, |
| 29 |
'Branches', |
| 30 |
{ title: 'Pull requests' }, |
| 31 |
'Workspaces', |
| 32 |
'Last commit' |
| 33 |
]; |
| 34 |
const rows: TableProps['rows'] = repositories.map((repo) => ({ |
| 35 |
cells: [repo.name, repo.branches, repo.prs, repo.workspaces, repo.lastCommit], |
| 36 |
selected: selectedRepoName === repo.name, |
| 37 |
disableSelection: !isRepoSelectable(repo) |
| 38 |
})); |
| 39 |
|
| 40 |
return ( |
| 41 |
<Table |
| 42 |
onSelect={(_event, _isSelecting, rowIndex) => { |
| 43 |
const repo = repositories[rowIndex]; |
| 44 |
setSelectedRepoName(repo.name); |
| 45 |
}} |
| 46 |
selectVariant="radio" |
| 47 |
aria-label="Selectable Table with Radio Buttons" |
| 48 |
cells={columns} |
| 49 |
rows={rows} |
| 50 |
> |
| 51 |
<TableHeader /> |
| 52 |
<TableBody /> |
| 53 |
</Table> |
| 54 |
); |
| 55 |
}; |