Skip to content

Commit 08adb72

Browse files
committed
feat: addSortBy toggle order
1 parent ee4dffa commit 08adb72

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/lib/plugins/addSortBy.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ export interface SortByConfig {
99
initialSortKeys?: SortKey[];
1010
disableMultiSort?: boolean;
1111
isMultiSortEvent?: (event: Event) => boolean;
12+
toggleOrder?: ('asc' | 'desc' | undefined)[];
1213
}
1314

15+
const DEFAULT_TOGGLE_ORDER: ('asc' | 'desc' | undefined)[] = ['asc', 'desc', undefined];
16+
1417
export interface SortByState<Item> {
1518
sortKeys: WritableSortKeys;
1619
preSortedRows: Readable<BodyRow<Item>[]>;
@@ -42,31 +45,34 @@ export interface SortKey {
4245

4346
export const createSortKeysStore = (initKeys: SortKey[]): WritableSortKeys => {
4447
const { subscribe, update, set } = writable(initKeys);
45-
const toggleId = (id: string, { multiSort = true }: ToggleOptions = {}) => {
48+
const toggleId = (
49+
id: string,
50+
{ multiSort = true, toggleOrder = DEFAULT_TOGGLE_ORDER }: ToggleOptions = {}
51+
) => {
4652
update(($sortKeys) => {
4753
const keyIdx = $sortKeys.findIndex((key) => key.id === id);
54+
const key = $sortKeys[keyIdx];
55+
const order = key?.order;
56+
const orderIdx = toggleOrder.findIndex((o) => o === order);
57+
const nextOrderIdx = (orderIdx + 1) % toggleOrder.length;
58+
const nextOrder = toggleOrder[nextOrderIdx];
4859
if (!multiSort) {
49-
if (keyIdx === -1) {
50-
return [{ id, order: 'asc' }];
51-
}
52-
const key = $sortKeys[keyIdx];
53-
if (key.order === 'asc') {
54-
return [{ id, order: 'desc' }];
60+
if (nextOrder === undefined) {
61+
return [];
5562
}
56-
return [];
63+
return [{ id, order: nextOrder }];
5764
}
58-
if (keyIdx === -1) {
59-
return [...$sortKeys, { id, order: 'asc' }];
65+
if (keyIdx === -1 && nextOrder !== undefined) {
66+
return [...$sortKeys, { id, order: nextOrder }];
6067
}
61-
const key = $sortKeys[keyIdx];
62-
if (key.order === 'asc') {
63-
return [
64-
...$sortKeys.slice(0, keyIdx),
65-
{ id, order: 'desc' },
66-
...$sortKeys.slice(keyIdx + 1),
67-
];
68+
if (nextOrder === undefined) {
69+
return [...$sortKeys.slice(0, keyIdx), ...$sortKeys.slice(keyIdx + 1)];
6870
}
69-
return [...$sortKeys.slice(0, keyIdx), ...$sortKeys.slice(keyIdx + 1)];
71+
return [
72+
...$sortKeys.slice(0, keyIdx),
73+
{ id, order: nextOrder },
74+
...$sortKeys.slice(keyIdx + 1),
75+
];
7076
});
7177
};
7278
const clearId = (id: string) => {
@@ -89,6 +95,7 @@ export const createSortKeysStore = (initKeys: SortKey[]): WritableSortKeys => {
8995

9096
interface ToggleOptions {
9197
multiSort?: boolean;
98+
toggleOrder?: ('asc' | 'desc' | undefined)[];
9299
}
93100

94101
export type WritableSortKeys = Writable<SortKey[]> & {
@@ -160,6 +167,7 @@ export const addSortBy =
160167
initialSortKeys = [],
161168
disableMultiSort = false,
162169
isMultiSortEvent = isShiftClick,
170+
toggleOrder,
163171
}: SortByConfig = {}): TablePlugin<Item, SortByState<Item>, SortByColumnOptions, SortByPropSet> =>
164172
({ columnOptions }) => {
165173
const disabledSortIds = Object.entries(columnOptions)
@@ -198,6 +206,7 @@ export const addSortBy =
198206
if (disabled) return;
199207
sortKeys.toggleId(cell.id, {
200208
multiSort: disableMultiSort ? false : isMultiSortEvent(event),
209+
toggleOrder,
201210
});
202211
};
203212
const clear = () => {

0 commit comments

Comments
 (0)