Skip to content

Commit

Permalink
My attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Oct 3, 2024
1 parent 1c2ed21 commit 030759f
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/components/Grid/useDataGridSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,16 @@ export const useDataGridSource = <
const onSortModelChange: DataGridProps['onSortModelChange'] & {} =
useMemoizedFn((next) => {
let sortModel: ViewState['sortModel'];
const initialField = initialSort[0].field;

// Covers the case where the initial field is the same as the current sort field and initial sort is 'desc'
if (initialField === view.sortModel[0].field && next.length === 0) {
sortModel = [{ field: initialField, sort: 'asc' }];
if (next.length > 0) {
sortModel = [next[0]!];
} else {
sortModel = next.length === 0 ? initialSort : [next[0]!];
// If "un-sorting" revert to initial sort
sortModel = initialSort;
// If the prev sort field _is_ the initial sort field, be sure to flip order
const prev = view.sortModel[0];
if (prev.field === initialSort[0].field) {
sortModel[0].sort = prev.sort === 'asc' ? 'desc' : 'asc';
}
}

setView((prev) => ({
Expand Down

0 comments on commit 030759f

Please sign in to comment.