-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Subrow unselect doesn't affect parent checkbox #3965
Comments
Hello Jony, You are building the parent Checkbox with the wrong properties. You have to differentiate the selected rows from the "parent" selected rows. This image was taken from here: https://codesandbox.io/s/summer-lake-2qklsp?file=/src/App.js with the fix. Regards, EDIT: This won't work if you add more rows, I just duplicated the toggle all rows selected props to the "parent" row: |
Hi Nelson, thanks for quick answer! I updated my sandbox and also I added one more row. When I tried your fix my parent checkbox now selects all rows but it should select only subrows. Have you got any other idea how to fix it? |
Hello Jony, Also, expanding + row selection is not properly connected and you cannot do what you are trying to do, maybe Tan can help us out here. Good luck Jony! |
Feel free to open a discussion or use the Discord for more help! |
Alright, I create discussion here |
When following the reproduction steps, this same issue is visible in the "Expanding" CodeSandbox example from the guide. This issue seems to have the same/similar symptoms to these other issues: #4720 #4759 #4878 #4879
Could you please advise if we are missing something @tannerlinsley? |
See my comment on the above linked discussion that outlines a way I managed to implement this sort of "cascading" behaviour. |
I've fixed this in a local fork. The issue stems from row selection functioning independently of parent row selection. In other words, the current implementation allows a parent row to be selected independently of its child rows. This doesn't align with what I expect from nested checkbox behavior, namely:
The fixes below pertain to the row.toggleSelected = (value, opts) => {
const isSelected = row.getIsSelected()
table.setRowSelection((old) => {
value = typeof value !== "undefined" ? value : !isSelected
if (row.getCanSelect() && isSelected === value) {
return old
}
const selectedRowIds = {...old}
mutateRowIsSelected(
selectedRowIds,
row.id,
value,
opts?.selectChildren ?? true,
table,
)
// could also be enabled through a new config option.
return syncParentRowSelection(row, selectedRowIds, table)
})
} /**
* When child rows are updated, the parent state is not properly synced.
* Assumptions:
* When all children are selected, the parent should be selected.
* When only some children are selected, the parent should be indeterminate.
* When no children are selected, the parent should be unselected.
*/
function syncParentRowSelection<TData extends RowData>(
row: Row<TData>,
selection: RowSelectionState,
table: Table<TData>,
): RowSelectionState {
const parentRow = row.getParentRow()
if (!parentRow) {
return selection
}
const selected = isRowSelected(parentRow, selection)
const isAllSubRowsSelected =
isSubRowSelected(parentRow, selection, table) === "all"
if (isAllSubRowsSelected && !selected) {
mutateRowIsSelected(selection, parentRow.id, true, false, table)
} else if (!isAllSubRowsSelected && selected) {
mutateRowIsSelected(selection, parentRow.id, false, false, table)
}
return syncParentRowSelection(parentRow, selection, table)
} |
Describe the bug
If you unselect child row and you already has selected its parent row, parent row stay selected, but it should go to indeterminate state.
Your minimal, reproducible example
https://codesandbox.io/s/summer-lake-2qklsp?file=/src/App.js
Steps to reproduce
Expected behavior
How often does this bug happen?
Every time
Screenshots or Videos
Platform
I tried it on
But I don't think it is browser issue.
react-table version
7.7.12
TypeScript version
4.5.5
Additional context
No response
Terms & Code of Conduct
The text was updated successfully, but these errors were encountered: