-
Notifications
You must be signed in to change notification settings - Fork 383
feat(Table): add indeterminate checkbox state support for select-all header #12411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thatblindgeye
merged 2 commits into
patternfly:main
from
rhamilto:feat/table-indeterminate-checkbox
May 8, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
packages/react-table/src/components/Table/examples/TableSelectableIndeterminate.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { useEffect, 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 TableSelectableIndeterminate: 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: 'b', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' }, | ||
| { name: 'c', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' }, | ||
| { name: 'd', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' }, | ||
| { name: 'e', 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 | ||
| const selectableRepos = repositories.filter(isRepoSelectable); | ||
|
|
||
| // 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 [selectedRepoNames, setSelectedRepoNames] = useState<string[]>([]); | ||
| const setRepoSelected = (repo: Repository, isSelecting = true) => | ||
| setSelectedRepoNames((prevSelected) => { | ||
| const otherSelectedRepoNames = prevSelected.filter((r) => r !== repo.name); | ||
| return isSelecting && isRepoSelectable(repo) ? [...otherSelectedRepoNames, repo.name] : otherSelectedRepoNames; | ||
| }); | ||
| const selectAllRepos = (isSelecting = true) => | ||
| setSelectedRepoNames(isSelecting ? selectableRepos.map((r) => r.name) : []); | ||
| const areAllReposSelected = selectedRepoNames.length === selectableRepos.length; | ||
| const areSomeReposSelected = selectedRepoNames.length > 0 && selectedRepoNames.length < selectableRepos.length; | ||
| const isRepoSelected = (repo: Repository) => selectedRepoNames.includes(repo.name); | ||
|
|
||
| // To allow shift+click to select/deselect multiple rows | ||
| const [recentSelectedRowIndex, setRecentSelectedRowIndex] = useState<number | null>(null); | ||
| const [shifting, setShifting] = useState(false); | ||
|
|
||
| const onSelectRepo = (repo: Repository, rowIndex: number, isSelecting: boolean) => { | ||
| // If the user is shift + selecting the checkboxes, then all intermediate checkboxes should be selected | ||
| if (shifting && recentSelectedRowIndex !== null) { | ||
| const numberSelected = rowIndex - recentSelectedRowIndex; | ||
| const intermediateIndexes = | ||
| numberSelected > 0 | ||
| ? Array.from(new Array(numberSelected + 1), (_x, i) => i + recentSelectedRowIndex) | ||
| : Array.from(new Array(Math.abs(numberSelected) + 1), (_x, i) => i + rowIndex); | ||
| intermediateIndexes.forEach((index) => setRepoSelected(repositories[index], isSelecting)); | ||
| } else { | ||
| setRepoSelected(repo, isSelecting); | ||
| } | ||
| setRecentSelectedRowIndex(rowIndex); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| const onKeyDown = (e: KeyboardEvent) => { | ||
| if (e.key === 'Shift') { | ||
| setShifting(true); | ||
| } | ||
| }; | ||
| const onKeyUp = (e: KeyboardEvent) => { | ||
| if (e.key === 'Shift') { | ||
| setShifting(false); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener('keydown', onKeyDown); | ||
| document.addEventListener('keyup', onKeyUp); | ||
|
|
||
| return () => { | ||
| document.removeEventListener('keydown', onKeyDown); | ||
| document.removeEventListener('keyup', onKeyUp); | ||
| }; | ||
| }, []); | ||
|
|
||
| return ( | ||
| <Table aria-label="Selectable table with indeterminate state"> | ||
| <Thead> | ||
| <Tr> | ||
| <Th | ||
| select={{ | ||
| onSelect: (_event, isSelecting) => selectAllRepos(isSelecting), | ||
| isSelected: areAllReposSelected, | ||
| isIndeterminate: areSomeReposSelected | ||
| }} | ||
| aria-label="Row select" | ||
| /> | ||
| <Th>{columnNames.name}</Th> | ||
| <Th>{columnNames.branches}</Th> | ||
| <Th>{columnNames.prs}</Th> | ||
| <Th>{columnNames.workspaces}</Th> | ||
| <Th>{columnNames.lastCommit}</Th> | ||
| </Tr> | ||
| </Thead> | ||
| <Tbody> | ||
| {repositories.map((repo, rowIndex) => ( | ||
| <Tr key={repo.name}> | ||
| <Td | ||
| select={{ | ||
| rowIndex, | ||
| onSelect: (_event, isSelecting) => onSelectRepo(repo, rowIndex, isSelecting), | ||
| isSelected: isRepoSelected(repo), | ||
| isDisabled: !isRepoSelectable(repo) | ||
| }} | ||
| /> | ||
| <Td dataLabel={columnNames.name}>{repo.name}</Td> | ||
| <Td dataLabel={columnNames.branches}>{repo.branches}</Td> | ||
| <Td dataLabel={columnNames.prs}>{repo.prs}</Td> | ||
| <Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td> | ||
| <Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td> | ||
| </Tr> | ||
| ))} | ||
| </Tbody> | ||
| </Table> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.