Skip to content

Commit 5c73bf6

Browse files
authored
Merge pull request #82 from bryanmylee:feat/sort-toggle-order
Customize sort order
2 parents ee4dffa + 0a64a2a commit 5c73bf6

File tree

5 files changed

+39
-22
lines changed

5 files changed

+39
-22
lines changed

docs/src/routes/docs/[...3]plugins/[...2]add-sort-by.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ Takes an [Event](https://developer.mozilla.org/en-US/docs/Web/API/Event) and ret
4545

4646
_Defaults to multi-sort on shift-click_.
4747

48+
### `toggleOrder?: ()[]`
49+
50+
Allows customization of the toggling order. This cannot contain duplicate values. Set this to `['asc', 'desc']` to disable toggling to an unsorted column.
51+
52+
_Defaults to `['asc', 'desc', undefined]`_.
53+
4854
## Column Options
4955

5056
:::callout

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "svelte-headless-table",
33
"description": "Unopinionated and extensible data tables for Svelte",
4-
"version": "0.15.2",
4+
"version": "0.15.3",
55
"scripts": {
66
"dev": "vite dev",
77
"build": "vite build",

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 = () => {

src/routes/+page.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
group: addGroupBy({
4545
initialGroupByIds: [],
4646
}),
47-
sort: addSortBy(),
47+
sort: addSortBy({
48+
toggleOrder: ['asc', 'desc'],
49+
}),
4850
expand: addExpandedRows({
4951
initialExpandedIds: { 1: true },
5052
}),

0 commit comments

Comments
 (0)