@@ -41,6 +41,60 @@ const ColumnHeader = ({
4141} ) => {
4242 const ref = useRef < HTMLButtonElement > ( undefined ! ) ;
4343 const currentColumn = getCurrentColumn ( ) ;
44+ const currentSortedColumns = api . getColumnState ( ) . filter ( ( c ) => c . sort ) ;
45+ const columnNumber = column . sort
46+ ? currentSortedColumns . length > 1
47+ ? currentSortedColumns . findIndex ( ( c ) => c . colId === column . colId ) + 1
48+ : ""
49+ : "" ;
50+
51+ const displayColumnMenu = ( ) => {
52+ if ( currentColumn ) {
53+ return setColumnMenu ( undefined ) ;
54+ }
55+ const { height, top, left } = ref . current . getBoundingClientRect ( ) ;
56+ setColumnMenu ( {
57+ left,
58+ top : top + height ,
59+ column,
60+ theme,
61+ hasSort : api . getColumnState ( ) . some ( ( c ) => c . sort ) ,
62+ sortColumn : ( direction : "asc" | "desc" | null ) => {
63+ const newColumnState = api . getColumnState ( ) . filter ( ( c ) => c . sort ) ;
64+ const colIndex = newColumnState . findIndex (
65+ ( c ) => c . colId === column . colId ,
66+ ) ;
67+ if ( colIndex === - 1 ) {
68+ newColumnState . push ( {
69+ colId : column . colId ,
70+ sort : direction ,
71+ } ) ;
72+ } else {
73+ newColumnState [ colIndex ] . sort = direction ;
74+ }
75+ api . applyColumnState ( {
76+ state : newColumnState ,
77+ defaultState : { sort : null } ,
78+ } ) ;
79+ } ,
80+ removeAllSorting : ( ) =>
81+ api . applyColumnState ( {
82+ state : api
83+ . getColumnState ( )
84+ . filter ( ( c ) => c . sort )
85+ . map ( ( c ) => ( { colId : c . colId , sort : null } ) ) ,
86+ defaultState : { sort : null } ,
87+ } ) ,
88+ removeFromSort : ( ) =>
89+ api . applyColumnState ( {
90+ state : api
91+ . getColumnState ( )
92+ . filter ( ( c ) => c . sort && c . colId !== column . colId ) ,
93+ defaultState : { sort : null } ,
94+ } ) ,
95+ dismissMenu : ( ) => setColumnMenu ( undefined ) ,
96+ } ) ;
97+ } ;
4498
4599 return (
46100 < div className = { `ag-cell-label-container ${ theme } ` } role = "presentation" >
@@ -53,41 +107,28 @@ const ColumnHeader = ({
53107 className = { `header-icon ${ getIconForColumnType ( columnType ) } ` }
54108 > </ span >
55109 < span className = "ag-header-cell-text" > { column . colId } </ span >
56- { column . sort === "asc" && (
57- < span className = "sort-icon-wrapper" >
58- < span className = "sort-icon ascending" > </ span >
59- </ span >
60- ) }
61- { column . sort === "desc" && (
62- < span className = "sort-icon-wrapper" >
63- < span className = "sort-icon descending" > </ span >
64- </ span >
65- ) }
66- < div className = "dropdown" >
67- < button
68- ref = { ref }
69- type = "button"
70- className = { currentColumn ?. colId === column . colId ? "active" : "" }
71- onClick = { ( ) => {
72- if ( currentColumn ) {
73- return setColumnMenu ( undefined ) ;
74- }
75- const { height, top, left } = ref . current . getBoundingClientRect ( ) ;
76- setColumnMenu ( {
77- left,
78- top : top + height ,
79- column,
80- theme,
81- sortColumn : ( direction : "asc" | "desc" | null ) => {
82- api . applyColumnState ( {
83- state : [ { colId : column . colId , sort : direction } ] ,
84- defaultState : { sort : null } ,
85- } ) ;
86- } ,
87- dismissMenu : ( ) => setColumnMenu ( undefined ) ,
88- } ) ;
89- } }
90- >
110+
111+ < span className = "sort-icon-wrapper" >
112+ { ! ! column . sort && (
113+ < >
114+ < span
115+ className = { `sort-icon ${ column . sort === "asc" ? "ascending" : "descending" } ` }
116+ > </ span >
117+ { ! ! columnNumber && (
118+ < span className = "sort-number" > { columnNumber } </ span >
119+ ) }
120+ </ >
121+ ) }
122+ </ span >
123+
124+ < div
125+ className = {
126+ currentColumn ?. colId === column . colId
127+ ? "active dropdown"
128+ : "dropdown"
129+ }
130+ >
131+ < button ref = { ref } type = "button" onClick = { displayColumnMenu } >
91132 < span > </ span >
92133 </ button >
93134 </ div >
0 commit comments