@@ -4,7 +4,7 @@ import { useCallback, useContext, useEffect, useMemo } from 'preact/hooks';
4
4
import { useArrowKeyNavigation } from '../../hooks/use-arrow-key-navigation' ;
5
5
import { useStableCallback } from '../../hooks/use-stable-callback' ;
6
6
import { useSyncedRef } from '../../hooks/use-synced-ref' ;
7
- import type { CompositeProps , Order } from '../../types' ;
7
+ import type { CompositeProps , Order , OrderDirection } from '../../types' ;
8
8
import { downcastRef } from '../../util/typing' ;
9
9
import { ArrowDownIcon , ArrowUpIcon , SpinnerSpokesIcon } from '../icons' ;
10
10
import { Button } from '../input' ;
@@ -67,6 +67,11 @@ type ComponentProps<Row> = Pick<
67
67
/** Current sort order */
68
68
order ?: Order < keyof Row > ;
69
69
70
+ /**
71
+ * The initial order direction to set when a column becomes the ordered one.
72
+ */
73
+ initialColumnsOrderDir ?: Partial < Record < keyof Row , OrderDirection > > ;
74
+
70
75
/**
71
76
* Callback invoked when user clicks a column header to change the sort order.
72
77
* When a header is clicked, if that's not the active order, it is set with
@@ -98,9 +103,13 @@ function defaultRenderItem<Row>(r: Row, field: keyof Row): ComponentChildren {
98
103
function calculateNewOrder < T extends string | number | symbol > (
99
104
newField : T ,
100
105
prevOrder ?: Order < T > ,
106
+ initialColumnsOrderDir ?: Partial < Record < T , OrderDirection > > ,
101
107
) : Order < T > {
102
108
if ( newField !== prevOrder ?. field ) {
103
- return { field : newField , direction : 'ascending' } ;
109
+ return {
110
+ field : newField ,
111
+ direction : initialColumnsOrderDir ?. [ newField ] ?? 'ascending' ,
112
+ } ;
104
113
}
105
114
106
115
const newDirection =
@@ -151,6 +160,7 @@ export default function DataTable<Row>({
151
160
order,
152
161
onOrderChange,
153
162
orderableColumns = [ ] ,
163
+ initialColumnsOrderDir,
154
164
155
165
// Forwarded to Table
156
166
title,
@@ -164,10 +174,14 @@ export default function DataTable<Row>({
164
174
const scrollContext = useContext ( ScrollContext ) ;
165
175
const updateOrder = useCallback (
166
176
( newField : keyof Row ) => {
167
- const newOrder = calculateNewOrder ( newField , order ) ;
177
+ const newOrder = calculateNewOrder (
178
+ newField ,
179
+ order ,
180
+ initialColumnsOrderDir ,
181
+ ) ;
168
182
onOrderChange ?.( newOrder ) ;
169
183
} ,
170
- [ onOrderChange , order ] ,
184
+ [ initialColumnsOrderDir , onOrderChange , order ] ,
171
185
) ;
172
186
173
187
const noContent = loading || ( ! rows . length && emptyMessage ) ;
0 commit comments