-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[DataGrid] Reset sortedRows state on prop change #599
Conversation
packages/grid/_modules_/grid/hooks/features/sorting/useSorting.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sortedRows
looks like an internal state that should be private. It's fully derived from the input rows and the sort configuration provided by users. Developers' changes to the state are overridden by the grid. So I don't think that any tests should assert it.
On a side note, I was surprised to see this state exist. I was assuming that we would have a "input row" state, as well as a "transformed row" state, the transformed one being computed from grouping, filtering, sorting, etc. It seems that the challenge is to make sure that each transformations is applied in the right order. I guess it can be resolved with a priority value.
Co-authored-by: Olivier Tassinari <olivier.tassinari@gmail.com>
Co-authored-by: Olivier Tassinari <olivier.tassinari@gmail.com>
Something is off, it jumps: It renders twice, shouldn't it render once? import * as React from 'react';
import { DataGrid } from '@material-ui/data-grid';
const TestCase = () => {
const [rows, setRows] = React.useState([
{
id: 0,
brand: 'Nike',
},
{
id: 1,
brand: 'Adidas',
},
{
id: 2,
brand: 'Puma',
},
]);
return (
<div style={{ width: 300, height: 300 }}>
<button onClick={() => {
setRows([
{
id: 3,
brand: 'Asics',
},
{
id: 4,
brand: 'RedBull',
},
{
id: 5,
brand: 'Hugo',
},
]);
}}>Swap</button>
<DataGrid
columns={[{ field: 'brand', width: 100 }]}
rows={rows}
sortModel={[
{
field: 'brand',
sort: 'asc',
},
]}
/>
</div>
);
};
export default TestCase; |
// When the rows prop change, we reset the sortedRows state. | ||
setGridState((state) => ({ | ||
...state, | ||
sorting: { ...state.sorting, sortedRows: rowsProp.map((row) => row.id) }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we sorted the rows directly, would it solve #599 (comment)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least we fixed the reported bug and we have a solid test in place, so why not defer a better solution to later. I suspect it will require a refactoring of the state. There is an issue with the control flow somewhere, it could be imperative calls that shouldn't be here, it could be missing dependencies, a desynchronization, no idea :).
I will fix it ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least we fixed the reported bug and we have a solid test in place, so why not defer a better solution to later. I suspect it will require a refactoring of the state. There is an issue with the control flow somewhere, it could be imperative calls that shouldn't be here, it could be missing dependencies, a desynchronization, no idea :).
Nice :) |
Fix #575
Fix #571