@@ -13,7 +13,7 @@ import { FeatureModeConstant } from '../../../models/featureMode';
13
13
import { CellParams } from '../../../models/params/cellParams' ;
14
14
import { ColParams } from '../../../models/params/colParams' ;
15
15
import { SortModelParams } from '../../../models/params/sortModelParams' ;
16
- import { RowModel } from '../../../models/rows' ;
16
+ import { RowModel , RowsProp } from '../../../models/rows' ;
17
17
import { FieldComparatorList , SortItem , SortModel } from '../../../models/sortModel' ;
18
18
import { buildCellParams } from '../../../utils/paramsUtils' ;
19
19
import { isDesc , nextSortDirection } from '../../../utils/sortingUtils' ;
@@ -26,10 +26,10 @@ import { columnsSelector } from '../columns/columnsSelector';
26
26
import { GridState } from '../core/gridState' ;
27
27
import { useGridSelector } from '../core/useGridSelector' ;
28
28
import { useGridState } from '../core/useGridState' ;
29
- import { rowCountSelector , unorderedRowModelsSelector } from '../rows/rowsSelector' ;
29
+ import { rowCountSelector } from '../rows/rowsSelector' ;
30
30
import { SortingState } from './sortingState' ;
31
31
32
- export const useSorting = ( apiRef : ApiRef ) => {
32
+ export const useSorting = ( apiRef : ApiRef , rowsProp : RowsProp ) => {
33
33
const logger = useLogger ( 'useSorting' ) ;
34
34
const allowMultipleSorting = React . useRef < boolean > ( false ) ;
35
35
const comparatorList = React . useRef < FieldComparatorList > ( [ ] ) ;
@@ -38,7 +38,6 @@ export const useSorting = (apiRef: ApiRef) => {
38
38
const options = useGridSelector ( apiRef , optionsSelector ) ;
39
39
const columns = useGridSelector ( apiRef , columnsSelector ) ;
40
40
const rowCount = useGridSelector ( apiRef , rowCountSelector ) ;
41
- const unorderedRows = useGridSelector ( apiRef , unorderedRowModelsSelector ) ;
42
41
43
42
const getSortModelParams = React . useCallback (
44
43
( sortModel : SortModel ) : SortModelParams => ( {
@@ -127,13 +126,14 @@ export const useSorting = (apiRef: ApiRef) => {
127
126
) ;
128
127
129
128
const applySorting = React . useCallback ( ( ) => {
129
+ const rowModels = apiRef . current . getRowModels ( ) ;
130
130
const sortModel = apiRef . current . getState < GridState > ( ) . sorting . sortModel ;
131
131
logger . info ( 'Sorting rows with ' , sortModel ) ;
132
132
133
- let sorted = [ ...unorderedRows ] ;
133
+ const sorted = [ ...rowModels ] ;
134
134
if ( sortModel . length > 0 ) {
135
135
comparatorList . current = buildComparatorList ( sortModel ) ;
136
- sorted = sorted . sort ( comparatorListAggregate ) ;
136
+ sorted . sort ( comparatorListAggregate ) ;
137
137
}
138
138
139
139
setGridState ( ( oldState ) => {
@@ -143,15 +143,7 @@ export const useSorting = (apiRef: ApiRef) => {
143
143
} ;
144
144
} ) ;
145
145
forceUpdate ( ) ;
146
- } , [
147
- apiRef ,
148
- logger ,
149
- unorderedRows ,
150
- setGridState ,
151
- forceUpdate ,
152
- buildComparatorList ,
153
- comparatorListAggregate ,
154
- ] ) ;
146
+ } , [ apiRef , logger , setGridState , forceUpdate , buildComparatorList , comparatorListAggregate ] ) ;
155
147
156
148
const setSortModel = React . useCallback (
157
149
( sortModel : SortModel ) => {
@@ -236,6 +228,11 @@ export const useSorting = (apiRef: ApiRef) => {
236
228
const sortApi : SortApi = { getSortModel, setSortModel, onSortModelChange, applySorting } ;
237
229
useApiMethod ( apiRef , sortApi , 'SortApi' ) ;
238
230
231
+ React . useEffect ( ( ) => {
232
+ // When the rows prop change, we re apply the sorting.
233
+ apiRef . current . applySorting ( ) ;
234
+ } , [ apiRef , rowsProp ] ) ;
235
+
239
236
React . useEffect ( ( ) => {
240
237
if ( rowCount > 0 && options . sortingMode === FeatureModeConstant . client ) {
241
238
logger . debug ( 'row changed, applying sortModel' ) ;
@@ -268,7 +265,8 @@ export const useSorting = (apiRef: ApiRef) => {
268
265
269
266
React . useEffect ( ( ) => {
270
267
const sortModel = options . sortModel || [ ] ;
271
- if ( sortModel . length > 0 ) {
268
+ const oldSortModel = apiRef . current . state . sorting . sortModel ;
269
+ if ( sortModel . length > 0 && ! isEqual ( sortModel , oldSortModel ) ) {
272
270
// we use apiRef to avoid watching setSortModel as it will trigger an update on every state change
273
271
apiRef . current . setSortModel ( sortModel ) ;
274
272
}
0 commit comments