| 1 |
import { useState } from 'react'; |
| 2 |
import { Table, Thead, Tr, Th, Tbody, Td } from '@patternfly/react-table'; |
| 3 |
|
| 4 |
interface Repository { |
| 5 |
name: string; |
| 6 |
branches: string; |
| 7 |
prs: string; |
| 8 |
workspaces: string; |
| 9 |
lastCommit: string; |
| 10 |
} |
| 11 |
|
| 12 |
export const TableSelectableRadio: React.FunctionComponent = () => { |
| 13 |
// In real usage, this data would come from some external source like an API via props. |
| 14 |
const repositories: Repository[] = [ |
| 15 |
{ name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five' }, |
| 16 |
{ name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' }, |
| 17 |
{ name: 'p', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five' } |
| 18 |
]; |
| 19 |
|
| 20 |
const columnNames = { |
| 21 |
name: 'Repositories', |
| 22 |
branches: 'Branches', |
| 23 |
prs: 'Pull requests', |
| 24 |
workspaces: 'Workspaces', |
| 25 |
lastCommit: 'Last commit' |
| 26 |
}; |
| 27 |
|
| 28 |
const isRepoSelectable = (repo: Repository) => repo.name !== 'a'; // Arbitrary logic for this example |
| 29 |
|
| 30 |
// In this example, selected rows are tracked by the repo names from each row. This could be any unique identifier. |
| 31 |
// This is to prevent state from being based on row order index in case we later add sorting. |
| 32 |
const [selectedRepoName, setSelectedRepoName] = useState<string | null>(null); |
| 33 |
|
| 34 |
return ( |
| 35 |
<Table aria-label="Selectable table"> |
| 36 |
<Thead> |
| 37 |
<Tr> |
| 38 |
<Th screenReaderText="Row select" /> |
| 39 |
<Th>{columnNames.name}</Th> |
| 40 |
<Th>{columnNames.branches}</Th> |
| 41 |
<Th>{columnNames.prs}</Th> |
| 42 |
<Th>{columnNames.workspaces}</Th> |
| 43 |
<Th>{columnNames.lastCommit}</Th> |
| 44 |
</Tr> |
| 45 |
</Thead> |
| 46 |
<Tbody> |
| 47 |
{repositories.map((repo, rowIndex) => ( |
| 48 |
<Tr key={repo.name}> |
| 49 |
<Td |
| 50 |
select={{ |
| 51 |
rowIndex, |
| 52 |
onSelect: () => setSelectedRepoName(repo.name), |
| 53 |
isSelected: selectedRepoName === repo.name, |
| 54 |
isDisabled: !isRepoSelectable(repo), |
| 55 |
variant: 'radio' |
| 56 |
}} |
| 57 |
/> |
| 58 |
<Td dataLabel={columnNames.name}>{repo.name}</Td> |
| 59 |
<Td dataLabel={columnNames.branches}>{repo.branches}</Td> |
| 60 |
<Td dataLabel={columnNames.prs}>{repo.prs}</Td> |
| 61 |
<Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td> |
| 62 |
<Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td> |
| 63 |
</Tr> |
| 64 |
))} |
| 65 |
</Tbody> |
| 66 |
</Table> |
| 67 |
); |
| 68 |
}; |