@@ -10,12 +10,13 @@ import { type ReactNode, useEffect, useState } from 'react';
1010
1111import { SELECTION_COLUMN_ID , type UseTableDataProps } from '#Table/types.js' ;
1212import { useThemeContext } from '#ThemeContext/index.js' ;
13+ import type { ColumnSortingInterface } from '#types.js' ;
1314import { emptyObj } from '#utils/noops.js' ;
1415
1516import { makeTableColumns } from './columns.js' ;
1617import { useTableContext } from './context.js' ;
1718
18- export const getSingleValue = ( data : Record < string , any > | ReactNode ) : ReactNode => {
19+ export const getSingleValue = ( data : Record < string , unknown > | ReactNode ) : ReactNode => {
1920 if ( typeof data === 'object' && data ) {
2021 return getSingleValue ( Object . values ( data ) [ 0 ] ) ;
2122 } else {
@@ -25,9 +26,26 @@ export const getSingleValue = (data: Record<string, any> | ReactNode): ReactNode
2526
2627const defaultSelectionColumnWidth = 28 ;
2728
29+ const sanitizeCustomSortingDesc = ( sortingArray ) => {
30+ return ( sortingArray || [ ] ) . map ( ( field ) => ( { ...field , desc : field . desc || false } ) ) ;
31+ } ;
32+
33+ type SortingConverter = < T extends 'ReactTable' | 'Arranger' > (
34+ sortingArray : T extends 'ReactTable' ? ColumnSortingInterface [ ] : SortingState ,
35+ direction : T ,
36+ ) => T extends 'ReactTable' ? SortingState : ColumnSortingInterface [ ] ;
37+
38+ const convertSorting : SortingConverter = ( sortingArray , direction ) => {
39+ return sanitizeCustomSortingDesc ( sortingArray ) . map ( ( field ) => ( {
40+ desc : field . desc ,
41+ ...( direction === 'ReactTable' ? { id : field . fieldName } : { fieldName : field . id } ) ,
42+ } ) ) as any ; // because TS goes stupid with array methods
43+ } ;
44+
2845export const useTableData = ( {
2946 columnTypes : customColumnTypes ,
3047 defaultColumnWidth : customColumnWidth ,
48+ defaultSorting : customDefaultSorting ,
3149 disableColumnResizing : customDisableColumnResizing ,
3250 disableRowSelection : customDisableRowSelection ,
3351 disableRowSorting : customDisableRowSorting ,
@@ -48,6 +66,7 @@ export const useTableData = ({
4866 visibleColumnsDict,
4967 } = useTableContext ( {
5068 callerName : 'Table - useTableData' ,
69+ defaultSorting : sanitizeCustomSortingDesc ( customDefaultSorting ) ,
5170 } ) ;
5271
5372 const {
@@ -68,21 +87,20 @@ export const useTableData = ({
6887 const defaultColumnWidth = customColumnWidth || themeColumnWidth ;
6988 const [ tableColumns , setTableColumns ] = useState < ColumnDef < unknown , string > [ ] > ( [ ] ) ;
7089
90+ const initialSortingForReactTable = convertSorting ( defaultSorting , 'ReactTable' ) ;
7191 const [ reactTableSorting , setReactTableSorting ] = useState < SortingState > ( [ ] ) ;
7292
7393 const onSortingChange : OnChangeFn < SortingState > = ( handleSorting ) => {
7494 if ( typeof handleSorting === 'function' ) {
7595 const newReactTableSorting = handleSorting ( reactTableSorting ) ;
7696
97+ const isValidNewReactTableSorting = Array . isArray ( newReactTableSorting ) && newReactTableSorting . length ;
98+
7799 // update the data context for other Arranger components
78- setSorting (
79- newReactTableSorting . length
80- ? newReactTableSorting . map ( ( { id, desc } ) => ( { fieldName : id , desc } ) )
81- : defaultSorting ,
82- ) ;
100+ setSorting ( isValidNewReactTableSorting ? convertSorting ( newReactTableSorting , 'Arranger' ) : defaultSorting ) ;
83101
84102 // update react-table's internal state
85- setReactTableSorting ( newReactTableSorting ) ;
103+ setReactTableSorting ( isValidNewReactTableSorting ? newReactTableSorting : initialSortingForReactTable ) ;
86104 } else {
87105 console . info ( 'react-table is doing something unexpected with the sorting' , handleSorting ) ;
88106 }
@@ -106,8 +124,7 @@ export const useTableData = ({
106124 state : {
107125 ...( allowRowSelection && { rowSelection : selectedRowsDict } ) ,
108126 ...( allowRowSorting && {
109- // sorting: sorting.map(({ desc, fieldName }) => ({ desc, id: fieldName })),
110- sorting : reactTableSorting ,
127+ sorting : reactTableSorting . length ? reactTableSorting : initialSortingForReactTable ,
111128 } ) ,
112129 } ,
113130 } ) ;
0 commit comments