From 030759f1aaeb80c77350d305178076e82406498f Mon Sep 17 00:00:00 2001 From: Carson Full Date: Thu, 3 Oct 2024 10:06:46 -0500 Subject: [PATCH] My attempt --- src/components/Grid/useDataGridSource.tsx | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/components/Grid/useDataGridSource.tsx b/src/components/Grid/useDataGridSource.tsx index b53891c2c..b21677672 100644 --- a/src/components/Grid/useDataGridSource.tsx +++ b/src/components/Grid/useDataGridSource.tsx @@ -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) => ({