1- import React , { useState , useEffect } from "react" ;
1+ import React , { useState , useEffect , Fragment } from "react" ;
22import "./EnhancedTable.scss" ;
33import { headerStyles , inputStyles , sortableHeaderStyles , tableWrapperStyles } from "./TableStyles" ;
44
@@ -58,102 +58,78 @@ export const SearchFilter = ({ value, onChange, placeholder, inputClassName = ""
5858/**
5959 * Renders a consistent table header from react-table's headerGroups
6060 */
61- export const renderTableHeader = ( headerGroups ) => {
62- return (
63- < >
64- { headerGroups . map ( ( headerGroup , index ) => (
65- < tr { ...headerGroup . getHeaderGroupProps ( ) } key = { `header-row-${ headerGroup . id || index } ` } >
66- { headerGroup . headers . map ( ( column ) => {
67- const headerProps = column . getHeaderProps ( ) ;
68- const sortByProps = column . getSortByToggleProps ? column . getSortByToggleProps ( ) : { } ;
61+ export const renderTableHeader = ( headerGroups , props ) => {
62+ const handleSortChange = ( columnId ) => {
63+ if ( ! props . updateCriteria ) return ;
64+
65+ const currentSort = props . criteria ?. sortCriteria ;
66+ let newSortCriteria ;
67+
68+ if ( ! currentSort || currentSort . sortBy !== columnId ) {
69+ newSortCriteria = { sortBy : columnId , direction : "ASC" } ;
70+ } else if ( currentSort . direction === "ASC" ) {
71+ newSortCriteria = { sortBy : columnId , direction : "DESC" } ;
72+ } else {
73+ newSortCriteria = { sortBy : "name" , direction : "DESC" } ;
74+ }
6975
70- // Make sure to prevent click event conflicts
71- const onHeaderClick = ( e ) => {
72- if ( column . getSortByToggleProps && ! column . disableSortBy ) {
73- sortByProps . onClick ( e ) ;
74- }
75- } ;
76+ props . updateCriteria ( { sortCriteria : newSortCriteria } ) ;
77+ } ;
78+ const currentSort = props . criteria ?. sortCriteria ;
7679
77- return (
80+ return (
81+ < >
82+ { headerGroups . map ( ( headerGroup , headerGroupIndex ) => (
83+ < Fragment key = { `header-group-${ headerGroupIndex } ` } >
84+ < tr { ...headerGroup . getHeaderGroupProps ( ) } >
85+ { headerGroup . headers . map ( ( column , columnIndex ) => (
7886 < th
79- key = { `header-cell-${ column . id } ` }
80- className = { `${ headerStyles } ${ ! column . disableSortBy ? sortableHeaderStyles : "" } ` }
81- { ...headerProps }
82- onClick = { onHeaderClick }
87+ { ...column . getHeaderProps ( ) }
88+ className = { `mr-px-2 mr-text-left mr-border-gray-600 mr-relative ${ column . canResize ? "mr-border-r mr-border-gray-500" : "" } ` }
89+ key = { `header-${ column . id } -${ columnIndex } ` }
8390 style = { {
84- ...headerProps . style ,
91+ ...column . getHeaderProps ( ) . style ,
8592 width : column . width ,
8693 minWidth : column . minWidth ,
87- position : "relative " ,
94+ overflow : "hidden " ,
8895 } }
8996 >
90- < div className = "mr-header-content" >
91- < div className = "mr-flex mr-items-center mr-justify-between" >
92- < div
93- className = "mr-flex mr-items-center mr-whitespace-nowrap"
94- style = { {
95- overflow : "hidden" ,
96- textOverflow : "ellipsis" ,
97- } }
97+ < div
98+ className = "mr-relative mr-overflow-hidden"
99+ style = { { paddingRight : ! column . disableSortBy ? "24px" : "8px" } }
100+ >
101+ < div className = "mr-truncate" > { column . render ( "Header" ) } </ div >
102+ { ! column . disableSortBy && (
103+ < button
104+ className = "mr-absolute mr-right-0 mr-top-0 mr-bottom-0 mr-w-6 mr-h-full mr-flex mr-items-center mr-justify-center mr-text-gray-400 hover:mr-text-white mr-cursor-pointer mr-text-xs mr-z-20"
105+ onClick = { ( ) => handleSortChange ( column . id ) }
106+ title = { `Sort by ${ column . Header || column . id } ` }
98107 >
99- < span > { column . render ( "Header" ) } </ span >
100- { ! column . disableSortBy && (
101- < span className = "mr-ml-1 mr-opacity-70" >
102- { column . isSorted ? (
103- column . isSortedDesc ? (
104- " ▼"
105- ) : (
106- " ▲"
107- )
108- ) : (
109- < span className = "mr-text-xs mr-opacity-50 mr-inline-block" > ↕</ span >
110- ) }
111- </ span >
112- ) }
113- </ div >
114- </ div >
115- { ! column . disableResizing && (
116- < div
117- className = { `mr-resizer` }
118- { ...column . getResizerProps ?. ( ) }
119- onClick = { ( e ) => {
120- e . stopPropagation ( ) ;
121- } }
122- />
108+ { ! currentSort || currentSort . sortBy !== column . id
109+ ? "↕"
110+ : currentSort . direction === "DESC"
111+ ? "▼"
112+ : "▲" }
113+ </ button >
123114 ) }
124115 </ div >
116+ { column . canResize && (
117+ < div
118+ { ...column . getResizerProps ( ) }
119+ className = "mr-absolute mr-right-0 mr-top-0 mr-w-1 mr-h-full mr-bg-gray-400 mr-cursor-col-resize hover:mr-bg-blue-400 hover:mr-scale-x-3 mr-transition-all mr-z-10"
120+ />
121+ ) }
125122 </ th >
126- ) ;
127- } ) }
128- </ tr >
129- ) ) }
130-
131- { /* Add a separate row for filters */ }
132- { headerGroups . map ( ( headerGroup , index ) => (
133- < tr key = { `filter-row-${ headerGroup . id } -${ index } ` } >
134- { headerGroup . headers . map ( ( column ) => (
135- < td
136- key = { `filter-cell-${ column . id } ` }
137- style = { {
138- width : column . width ,
139- minWidth : column . minWidth ,
140- } }
141- >
142- { column . canFilter && (
143- < div
144- className = "mr-header-filter mr-mr-2"
145- onClick = { ( e ) => e . stopPropagation ( ) }
146- style = { {
147- overflow : "hidden" ,
148- maxWidth : "100%" ,
149- } }
150- >
151- { column . render ( "Filter" ) }
152- </ div >
153- ) }
154- </ td >
155- ) ) }
156- </ tr >
123+ ) ) }
124+ </ tr >
125+ < tr >
126+ { headerGroup . headers . map ( ( column , columnIndex ) => (
127+ < th key = { `filter-${ column . id } -${ columnIndex } ` } className = "mr-px-2" >
128+ < div > { column . Filter ? column . render ( "Filter" ) : null } </ div >
129+ </ th >
130+ ) ) }
131+ </ tr >
132+ </ Fragment >
157133 ) ) }
158134 </ >
159135 ) ;
@@ -177,30 +153,20 @@ export const TableWrapper = ({ children, className = "" }) => (
177153 * @param {Object } options - Additional styling options
178154 * @returns {JSX.Element } - The rendered table cell
179155 */
180- export const renderTableCell = ( cell , options = { } ) => {
156+ export const renderTableCell = ( cell , row , cellIndex ) => {
181157 return (
182158 < td
159+ key = { `cell-${ row . original . id } -${ cell . column . id } -${ cellIndex } ` }
183160 { ...cell . getCellProps ( ) }
161+ className = "mr-px-2"
184162 style = { {
185163 ...cell . getCellProps ( ) . style ,
186- whiteSpace : "nowrap !important" ,
187- overflow : "hidden !important" ,
188- textOverflow : "ellipsis !important" ,
189- minWidth : cell . column . minWidth ,
190- ...options ,
164+ overflow : "hidden" ,
165+ textOverflow : "ellipsis" ,
166+ whiteSpace : "nowrap" ,
191167 } }
192168 >
193- < div
194- style = { {
195- overflow : "hidden !important" ,
196- textOverflow : "ellipsis !important" ,
197- whiteSpace : "nowrap !important" ,
198- width : "100%" ,
199- display : "block" ,
200- } }
201- >
202- { cell . render ( "Cell" ) }
203- </ div >
169+ { cell . render ( "Cell" ) }
204170 </ td >
205171 ) ;
206172} ;
0 commit comments