In a React-Admin Datagrid component, I want to set all of the list items to be individually selectable (isRowSelectable) based on all the rows in aggregate.
That is, all items should be selectable (if the status property is the same for all of them); OR all items should NOT be selectable (if the status property is NOT the same for all).
The code below does that, but has one problem. When I try to select all items using the checkbox at the top of the Datagrid, I get an error (Invalid hook call ... handleSelectAll DatagridHeader.tsx:65
).
I don’t get that error when I select the list items individually.
const checkAllStatuses = () => {
const { data } = useListContext();
const uniqueStatuses = [...new Set(data.map((item) => item.status))];
return uniqueStatusStates.length === 1;
};
const EmployeeList = (props) => {
return (
<List title="Employees">
<Datagrid isRowSelectable={checkAllStatuses}>
...
</Datagrid>
</List>
);
};
How can I prevent the "Invalid hook call…" error when I select all the items collectively using the button at the top?
Alternatively, is there a way to disable or hide the "select all" checkbox in the Datagrid header – but only when the status property is not the same for all items?
2
Answers
From what I understood, you want to
Which translates to:
And then call the code below (step 3) and the
setCurrentStatus
function (step 4) when the user interacts with the checkbox or as an effect for when the selected ids change.And don’t forget to call
setCurrentStatus(undefined)
when you remove all selections too to re-enable the row selection for all rows.Possible implementation: