11// Copyright © 2025, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
22// SPDX-License-Identifier: Apache-2.0
3-
43import { useRef } from "react" ;
54
6- import { AgColumn , GridApi } from "ag-grid-community" ;
5+ import { AgColumn , ColumnState , GridApi } from "ag-grid-community" ;
76
87import { ColumnHeaderProps } from "./ColumnHeaderMenu" ;
98
@@ -50,6 +49,11 @@ const ColumnHeader = ({
5049 ? `${ column . sortIndex + 1 } `
5150 : "" ;
5251
52+ const applyColumnState = ( state : ColumnState [ ] ) => {
53+ api . applyColumnState ( { state, defaultState : { sort : null } } ) ;
54+ api . ensureIndexVisible ( 0 ) ;
55+ } ;
56+
5357 const displayColumnMenu = ( ) => {
5458 if ( currentColumn ) {
5559 return setColumnMenu ( undefined ) ;
@@ -75,29 +79,25 @@ const ColumnHeader = ({
7579 } else {
7680 newColumnState [ colIndex ] . sort = direction ;
7781 }
78- api . applyColumnState ( {
79- state : newColumnState ,
80- defaultState : { sort : null } ,
81- } ) ;
82+ applyColumnState ( newColumnState ) ;
8283 } ,
8384 removeAllSorting : ( ) =>
84- api . applyColumnState ( {
85- state : api
85+ applyColumnState (
86+ api
8687 . getColumnState ( )
8788 . filter ( ( c ) => c . sort )
8889 . map ( ( c ) => ( { colId : c . colId , sort : null } ) ) ,
89- defaultState : { sort : null } ,
90- } ) ,
90+ ) ,
9191 removeFromSort : ( ) =>
92- api . applyColumnState ( {
93- state : api
92+ applyColumnState (
93+ api
9494 . getColumnState ( )
9595 . sort ( ( a , b ) => a . sortIndex - b . sortIndex )
9696 . filter ( ( c ) => c . sort && c . colId !== column . colId )
9797 // After we remove the column, lets reindex what's left
9898 . map ( ( c , sortIndex ) => ( { ...c , sortIndex } ) ) ,
99- defaultState : { sort : null } ,
100- } ) ,
99+ ) ,
100+
101101 dismissMenu : ( ) => setColumnMenu ( undefined ) ,
102102 } ) ;
103103 } ;
0 commit comments