@@ -7,6 +7,7 @@ import { Pagination } from "./Pagination";
77import type { PaginationProps } from "./Pagination" ;
88import { TableHeader } from "./TableHeader" ;
99import { TableRow } from "./TableRow" ;
10+ import { SortType } from '../constants/sort' ;
1011import type {
1112 Column ,
1213 DataItem ,
@@ -30,6 +31,9 @@ export interface MultiLevelTableProps {
3031 childrenKey ?: string ;
3132 pageSize ?: number ;
3233 renderCustomPagination ?: ( props ?: PaginationProps ) => React . ReactNode ;
34+ sortable ?: boolean ;
35+ ascendingIcon ?: React . ReactNode ;
36+ descendingIcon ?: React . ReactNode ;
3337}
3438
3539/**
@@ -44,6 +48,9 @@ export const MultiLevelTable: React.FC<MultiLevelTableProps> = ({
4448 childrenKey = "children" ,
4549 pageSize = 10 ,
4650 renderCustomPagination = null ,
51+ sortable = false ,
52+ ascendingIcon,
53+ descendingIcon,
4754} ) => {
4855 const [ filterInput , setFilterInput ] = useState ( "" ) ;
4956 const [ expandedRows , setExpandedRows ] = useState < Set < string | number > > (
@@ -80,6 +87,9 @@ export const MultiLevelTable: React.FC<MultiLevelTableProps> = ({
8087 return columns . map ( ( col ) => ( {
8188 Header : col . title ,
8289 accessor : col . key ,
90+ disableSortBy : sortable ? col . sortable === false : true ,
91+ sortType : col . customSortFn ? SortType . Custom : SortType . Basic ,
92+ sortFn : col . customSortFn ,
8393 Cell : ( { row, value } : { row : Row < DataItem > ; value : unknown } ) => {
8494 const item = row . original ;
8595
@@ -102,7 +112,7 @@ export const MultiLevelTable: React.FC<MultiLevelTableProps> = ({
102112 )
103113 : undefined ,
104114 } ) ) ;
105- } , [ columns , filterInput ] ) ;
115+ } , [ columns , filterInput , sortable ] ) ;
106116
107117 // Initialize table with react-table hooks
108118 const {
@@ -125,9 +135,20 @@ export const MultiLevelTable: React.FC<MultiLevelTableProps> = ({
125135 columns : tableColumns ,
126136 data,
127137 initialState : { pageSize } as TableStateWithPagination < DataItem > ,
138+ // @ts -expect-error - sortTypes is not included in the type definition but is supported by react-table
139+ sortTypes : {
140+ custom : ( rowA : Row < DataItem > , rowB : Row < DataItem > , columnId : string ) => {
141+ const column = columns . find ( col => col . key === columnId ) ;
142+
143+ if ( column ?. customSortFn )
144+ return column . customSortFn ( rowA . original , rowB . original , columnId ) ;
145+
146+ return 0 ;
147+ } ,
148+ } ,
128149 } ,
129150 useFilters ,
130- useSortBy ,
151+ ... ( sortable ? [ useSortBy ] : [ ] ) ,
131152 usePagination
132153 ) as TableInstanceWithHooks < DataItem > ;
133154
@@ -179,7 +200,12 @@ export const MultiLevelTable: React.FC<MultiLevelTableProps> = ({
179200 return (
180201 < div >
181202 < table { ...getTableProps ( ) } className = "table-container" >
182- < TableHeader headerGroups = { headerGroups } />
203+ < TableHeader
204+ headerGroups = { headerGroups }
205+ sortable = { sortable }
206+ ascendingIcon = { ascendingIcon }
207+ descendingIcon = { descendingIcon }
208+ />
183209 < tbody { ...getTableBodyProps ( ) } >
184210 { page . map ( ( row ) => {
185211 prepareRow ( row ) ;
0 commit comments