22// SPDX-License-Identifier: Apache-2.0
33import { useRef } from "react" ;
44
5- import { AgColumn , ColumnState , GridApi } from "ag-grid-community" ;
5+ import { AgColumn , GridApi } from "ag-grid-community" ;
66
7- import { ColumnHeaderProps } from "./ColumnHeaderMenu " ;
7+ import { ColumnMenuProps , useColumnMenu } from "./ColumnMenu " ;
88
99const getIconForColumnType = ( type : string ) => {
1010 switch ( type . toLocaleLowerCase ( ) ) {
@@ -38,7 +38,7 @@ const ColumnHeader = ({
3838 column : AgColumn ;
3939 currentColumn : ( ) => AgColumn | undefined ;
4040 columnType : string ;
41- setColumnMenu : ( props : ColumnHeaderProps ) => void ;
41+ setColumnMenu : ( props : ColumnMenuProps ) => void ;
4242 theme : string ;
4343} ) => {
4444 const ref = useRef < HTMLButtonElement > ( undefined ! ) ;
@@ -48,94 +48,37 @@ const ColumnHeader = ({
4848 column . sort && currentSortedColumns . length > 1
4949 ? `${ column . sortIndex + 1 } `
5050 : "" ;
51+ const dropdownClassname =
52+ currentColumn ?. colId === column . colId ? "active dropdown" : "dropdown" ;
5153
52- const applyColumnState = ( state : ColumnState [ ] ) => {
53- api . applyColumnState ( { state, defaultState : { sort : null } } ) ;
54- api . ensureIndexVisible ( 0 ) ;
55- } ;
56-
54+ const getColumnMenu = useColumnMenu ( { api, theme } ) ;
5755 const displayColumnMenu = ( ) => {
5856 if ( currentColumn ) {
5957 return setColumnMenu ( undefined ) ;
6058 }
61- const { height, top, left } = ref . current . getBoundingClientRect ( ) ;
62- setColumnMenu ( {
63- left,
64- top : top + height ,
65- column,
66- theme,
67- hasSort : api . getColumnState ( ) . some ( ( c ) => c . sort ) ,
68- sortColumn : ( direction : "asc" | "desc" | null ) => {
69- const newColumnState = api . getColumnState ( ) . filter ( ( c ) => c . sort ) ;
70- const colIndex = newColumnState . findIndex (
71- ( c ) => c . colId === column . colId ,
72- ) ;
73- if ( colIndex === - 1 ) {
74- newColumnState . push ( {
75- colId : column . colId ,
76- sort : direction ,
77- sortIndex : newColumnState . length ,
78- } ) ;
79- } else {
80- newColumnState [ colIndex ] . sort = direction ;
81- }
82- applyColumnState ( newColumnState ) ;
83- } ,
84- removeAllSorting : ( ) =>
85- applyColumnState (
86- api
87- . getColumnState ( )
88- . filter ( ( c ) => c . sort )
89- . map ( ( c ) => ( { colId : c . colId , sort : null } ) ) ,
90- ) ,
91- removeFromSort : ( ) =>
92- applyColumnState (
93- api
94- . getColumnState ( )
95- . sort ( ( a , b ) => a . sortIndex - b . sortIndex )
96- . filter ( ( c ) => c . sort && c . colId !== column . colId )
97- // After we remove the column, lets reindex what's left
98- . map ( ( c , sortIndex ) => ( { ...c , sortIndex } ) ) ,
99- ) ,
100-
101- dismissMenu : ( ) => setColumnMenu ( undefined ) ,
102- } ) ;
59+ setColumnMenu (
60+ getColumnMenu ( column , ref . current . getBoundingClientRect ( ) , ( ) =>
61+ setColumnMenu ( undefined ) ,
62+ ) ,
63+ ) ;
10364 } ;
10465
10566 return (
10667 < div className = { `ag-cell-label-container ${ theme } ` } role = "presentation" >
107- < div
108- data-ref = "eLabel"
109- className = "ag-header-cell-label"
110- role = "presentation"
111- >
112- < span
113- className = { `header-icon ${ getIconForColumnType ( columnType ) } ` }
114- > </ span >
68+ < div className = "ag-header-cell-label" role = "presentation" >
69+ < span className = { `header-icon ${ getIconForColumnType ( columnType ) } ` } />
11570 < span className = "ag-header-cell-text" > { column . colId } </ span >
116-
11771 < span className = "sort-icon-wrapper" >
11872 { ! ! column . sort && (
11973 < >
120- < span
121- className = { `sort-icon ${ column . sort === "asc" ? "ascending" : "descending" } ` }
122- > </ span >
123- { ! ! columnNumber && (
124- < span className = "sort-number" > { columnNumber } </ span >
125- ) }
74+ < span className = { `icon ${ column . sort } ` } > </ span >
75+ { ! ! columnNumber && < span className = "number" > { columnNumber } </ span > }
12676 </ >
12777 ) }
12878 </ span >
129-
130- < div
131- className = {
132- currentColumn ?. colId === column . colId
133- ? "active dropdown"
134- : "dropdown"
135- }
136- >
79+ < div className = { dropdownClassname } >
13780 < button ref = { ref } type = "button" onClick = { displayColumnMenu } >
138- < span > </ span >
81+ < span / >
13982 </ button >
14083 </ div >
14184 </ div >
0 commit comments