| 1 |
import { useEffect, 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 TableSelectableIndeterminate: 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: 'b', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' }, |
| 18 |
{ name: 'c', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' }, |
| 19 |
{ name: 'd', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' }, |
| 20 |
{ name: 'e', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five' } |
| 21 |
]; |
| 22 |
|
| 23 |
const columnNames = { |
| 24 |
name: 'Repositories', |
| 25 |
branches: 'Branches', |
| 26 |
prs: 'Pull requests', |
| 27 |
workspaces: 'Workspaces', |
| 28 |
lastCommit: 'Last commit' |
| 29 |
}; |
| 30 |
|
| 31 |
const isRepoSelectable = (repo: Repository) => repo.name !== 'a'; // Arbitrary logic for this example |
| 32 |
const selectableRepos = repositories.filter(isRepoSelectable); |
| 33 |
|
| 34 |
// In this example, selected rows are tracked by the repo names from each row. This could be any unique identifier. |
| 35 |
// This is to prevent state from being based on row order index in case we later add sorting. |
| 36 |
const [selectedRepoNames, setSelectedRepoNames] = useState<string[]>([]); |
| 37 |
const setRepoSelected = (repo: Repository, isSelecting = true) => |
| 38 |
setSelectedRepoNames((prevSelected) => { |
| 39 |
const otherSelectedRepoNames = prevSelected.filter((r) => r !== repo.name); |
| 40 |
return isSelecting && isRepoSelectable(repo) ? [...otherSelectedRepoNames, repo.name] : otherSelectedRepoNames; |
| 41 |
}); |
| 42 |
const selectAllRepos = (isSelecting = true) => |
| 43 |
setSelectedRepoNames(isSelecting ? selectableRepos.map((r) => r.name) : []); |
| 44 |
const areAllReposSelected = selectedRepoNames.length === selectableRepos.length; |
| 45 |
const areSomeReposSelected = selectedRepoNames.length > 0 && selectedRepoNames.length < selectableRepos.length; |
| 46 |
const isRepoSelected = (repo: Repository) => selectedRepoNames.includes(repo.name); |
| 47 |
|
| 48 |
// To allow shift+click to select/deselect multiple rows |
| 49 |
const [recentSelectedRowIndex, setRecentSelectedRowIndex] = useState<number | null>(null); |
| 50 |
const [shifting, setShifting] = useState(false); |
| 51 |
|
| 52 |
const onSelectRepo = (repo: Repository, rowIndex: number, isSelecting: boolean) => { |
| 53 |
// If the user is shift + selecting the checkboxes, then all intermediate checkboxes should be selected |
| 54 |
if (shifting && recentSelectedRowIndex !== null) { |
| 55 |
const numberSelected = rowIndex - recentSelectedRowIndex; |
| 56 |
const intermediateIndexes = |
| 57 |
numberSelected > 0 |
| 58 |
? Array.from(new Array(numberSelected + 1), (_x, i) => i + recentSelectedRowIndex) |
| 59 |
: Array.from(new Array(Math.abs(numberSelected) + 1), (_x, i) => i + rowIndex); |
| 60 |
intermediateIndexes.forEach((index) => setRepoSelected(repositories[index], isSelecting)); |
| 61 |
} else { |
| 62 |
setRepoSelected(repo, isSelecting); |
| 63 |
} |
| 64 |
setRecentSelectedRowIndex(rowIndex); |
| 65 |
}; |
| 66 |
|
| 67 |
useEffect(() => { |
| 68 |
const onKeyDown = (e: KeyboardEvent) => { |
| 69 |
if (e.key === 'Shift') { |
| 70 |
setShifting(true); |
| 71 |
} |
| 72 |
}; |
| 73 |
const onKeyUp = (e: KeyboardEvent) => { |
| 74 |
if (e.key === 'Shift') { |
| 75 |
setShifting(false); |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
document.addEventListener('keydown', onKeyDown); |
| 80 |
document.addEventListener('keyup', onKeyUp); |
| 81 |
|
| 82 |
return () => { |
| 83 |
document.removeEventListener('keydown', onKeyDown); |
| 84 |
document.removeEventListener('keyup', onKeyUp); |
| 85 |
}; |
| 86 |
}, []); |
| 87 |
|
| 88 |
return ( |
| 89 |
<Table aria-label="Selectable table with indeterminate state"> |
| 90 |
<Thead> |
| 91 |
<Tr> |
| 92 |
<Th |
| 93 |
select={{ |
| 94 |
onSelect: (_event, isSelecting) => selectAllRepos(isSelecting), |
| 95 |
isSelected: areAllReposSelected, |
| 96 |
isIndeterminate: areSomeReposSelected |
| 97 |
}} |
| 98 |
aria-label="Row select" |
| 99 |
/> |
| 100 |
<Th>{columnNames.name}</Th> |
| 101 |
<Th>{columnNames.branches}</Th> |
| 102 |
<Th>{columnNames.prs}</Th> |
| 103 |
<Th>{columnNames.workspaces}</Th> |
| 104 |
<Th>{columnNames.lastCommit}</Th> |
| 105 |
</Tr> |
| 106 |
</Thead> |
| 107 |
<Tbody> |
| 108 |
{repositories.map((repo, rowIndex) => ( |
| 109 |
<Tr key={repo.name}> |
| 110 |
<Td |
| 111 |
select={{ |
| 112 |
rowIndex, |
| 113 |
onSelect: (_event, isSelecting) => onSelectRepo(repo, rowIndex, isSelecting), |
| 114 |
isSelected: isRepoSelected(repo), |
| 115 |
isDisabled: !isRepoSelectable(repo) |
| 116 |
}} |
| 117 |
/> |
| 118 |
<Td dataLabel={columnNames.name}>{repo.name}</Td> |
| 119 |
<Td dataLabel={columnNames.branches}>{repo.branches}</Td> |
| 120 |
<Td dataLabel={columnNames.prs}>{repo.prs}</Td> |
| 121 |
<Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td> |
| 122 |
<Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td> |
| 123 |
</Tr> |
| 124 |
))} |
| 125 |
</Tbody> |
| 126 |
</Table> |
| 127 |
); |
| 128 |
}; |