Skip to content

Commit 896390f

Browse files
committed
Fix sorting
1 parent 85431c4 commit 896390f

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

src/lib/components/Table/ColumnUtils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import { escapeHtml } from '$lib/utils/encoding';
1616

1717
function CreateSortHeader<TData>(name: string): StringOrTemplateHeader<TData, unknown> {
1818
return ({ column }) =>
19-
renderComponent(DataTableSortButton, {
19+
renderComponent(DataTableSortButton<TData>, {
2020
name,
21-
onclick: () => column.toggleSorting(column.getIsSorted() === 'asc'),
21+
column,
2222
});
2323
}
2424

@@ -61,14 +61,16 @@ export function CreateSortableColumnDef<
6161
renderer: (content: TData[TKey]) => TableCell,
6262
sortFunct?: 'auto' | ((a: TData[TKey], b: TData[TKey]) => number) | BuiltInSortingFn
6363
): ColumnDef<TData> {
64-
let sortingFn: SortingFnOption<TData> | undefined = undefined;
64+
let sortingFn: SortingFnOption<TData>;
6565
if (sortFunct) {
6666
if (typeof sortFunct === 'string') {
6767
sortingFn = sortFunct;
6868
} else {
6969
sortingFn = (row_a, row_b) =>
7070
sortFunct(row_a.getValue(accessorKey), row_b.getValue(accessorKey));
7171
}
72+
} else {
73+
sortingFn = 'auto';
7274
}
7375

7476
return {
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
<script lang="ts">
1+
<script lang="ts" generics="TData">
22
import { Button } from '$lib/components/ui/button';
3+
import type { Column } from '@tanstack/table-core';
34
import type { ComponentProps } from 'svelte';
45
5-
import ArrowUpDown from '@lucide/svelte/icons/arrow-up-down';
6+
import { ArrowDown, ArrowUp, ArrowUpDown } from '@lucide/svelte';
67
7-
type Props = ComponentProps<typeof Button> & { name: string };
8+
type Props = ComponentProps<typeof Button> & { name: string; column: Column<TData, unknown> };
89
9-
let { variant = 'ghost', name, ...restProps }: Props = $props();
10+
let { variant = 'ghost', name, column, ...restProps }: Props = $props();
11+
12+
let direction = $derived(column.getIsSorted());
1013
</script>
1114

12-
<Button {variant} {...restProps}>
15+
<Button {variant} {...restProps} onclick={() => column.toggleSorting(direction === 'asc')}>
1316
{name}
14-
<ArrowUpDown class="ml-2 size-4" />
17+
{#if direction === 'asc'}
18+
<ArrowUp class="ml-2 size-4" />
19+
{:else if direction === 'desc'}
20+
<ArrowDown class="ml-2 size-4" />
21+
{:else}
22+
<ArrowUpDown class="ml-2 size-4" />
23+
{/if}
1524
</Button>

0 commit comments

Comments
 (0)