|
1 | 1 | <script setup lang="ts"> |
2 | | -import { computed, resolveComponent } from 'vue'; |
| 2 | +import { computed, ref } from 'vue'; |
| 3 | +import { onClickOutside } from '@vueuse/core'; |
| 4 | +
|
| 5 | +import { useColumnDragDrop } from '@/composables/useColumnDragDrop'; |
3 | 6 |
|
4 | 7 | import type { ColumnVisibilityTableInstance } from '@/composables/usePersistentColumnVisibility'; |
5 | 8 |
|
6 | 9 | interface Props { |
7 | 10 | table: ColumnVisibilityTableInstance | null; |
| 11 | + columnOrder?: string[]; |
8 | 12 | } |
9 | 13 |
|
10 | 14 | const props = defineProps<Props>(); |
11 | 15 |
|
12 | 16 | const emit = defineEmits<{ |
13 | 17 | (e: 'change'): void; |
| 18 | + (e: 'update:columnOrder', value: string[]): void; |
14 | 19 | }>(); |
15 | 20 |
|
16 | | -const UDropdownMenu = resolveComponent('UDropdownMenu'); |
17 | | -const UButton = resolveComponent('UButton'); |
| 21 | +const isOpen = ref(false); |
| 22 | +const dropdownRef = ref<HTMLElement | null>(null); |
| 23 | +
|
| 24 | +onClickOutside(dropdownRef, () => { |
| 25 | + isOpen.value = false; |
| 26 | +}); |
| 27 | +
|
| 28 | +const columnOrderState = ref<string[]>([]); |
18 | 29 |
|
19 | | -const items = computed(() => { |
20 | | - if (!props.table?.tableApi) return [[]]; |
| 30 | +const orderedColumns = computed(() => { |
| 31 | + if (!props.table?.tableApi) return []; |
21 | 32 |
|
22 | 33 | const availableColumns = props.table.tableApi.getAllColumns().filter((column) => column.getCanHide()); |
| 34 | + const columnIds = availableColumns.map((col) => col.id); |
| 35 | +
|
| 36 | + const order = props.columnOrder && props.columnOrder.length > 0 ? props.columnOrder : columnIds; |
| 37 | + columnOrderState.value = order; |
| 38 | +
|
| 39 | + const columnMap = new Map(availableColumns.map((col) => [col.id, col])); |
| 40 | +
|
| 41 | + const ordered = order |
| 42 | + .map((id) => columnMap.get(id)) |
| 43 | + .filter((col): col is NonNullable<typeof col> => col !== undefined); |
| 44 | +
|
| 45 | + const missing = availableColumns.filter((col) => !order.includes(col.id)); |
23 | 46 |
|
24 | | - const list = availableColumns.map((column) => { |
25 | | - return { |
26 | | - label: column.id, |
27 | | - type: 'checkbox' as const, |
28 | | - checked: column.getIsVisible(), |
29 | | - onUpdateChecked(checked: boolean) { |
30 | | - props.table?.tableApi?.getColumn?.(column.id)?.toggleVisibility(!!checked); |
31 | | - emit('change'); |
32 | | - }, |
33 | | - onSelect(e: Event) { |
34 | | - e.preventDefault(); |
35 | | - }, |
36 | | - }; |
37 | | - }); |
38 | | -
|
39 | | - return [list]; |
| 47 | + return [...ordered, ...missing]; |
40 | 48 | }); |
| 49 | +
|
| 50 | +const { |
| 51 | + draggingColumnId, |
| 52 | + dragOverColumnId, |
| 53 | + handleDragStart, |
| 54 | + handleDragEnd, |
| 55 | + handleDragOver, |
| 56 | + handleDrop, |
| 57 | +} = useColumnDragDrop({ |
| 58 | + columnOrder: columnOrderState, |
| 59 | + onReorder: (newOrder) => { |
| 60 | + emit('update:columnOrder', newOrder); |
| 61 | + emit('change'); |
| 62 | + }, |
| 63 | +}); |
| 64 | +
|
| 65 | +function toggleColumnVisibility(columnId: string, checked: boolean | 'indeterminate') { |
| 66 | + if (checked === 'indeterminate') return; |
| 67 | + props.table?.tableApi?.getColumn?.(columnId)?.toggleVisibility(checked); |
| 68 | + emit('change'); |
| 69 | +} |
| 70 | +
|
| 71 | +function toggleDropdown() { |
| 72 | + isOpen.value = !isOpen.value; |
| 73 | +} |
41 | 74 | </script> |
42 | 75 |
|
43 | 76 | <template> |
44 | | - <UDropdownMenu :items="items" size="md" :ui="{ content: 'z-40' }"> |
45 | | - <UButton color="neutral" variant="outline" size="md" trailing-icon="i-lucide-chevron-down"> |
| 77 | + <div ref="dropdownRef" class="relative"> |
| 78 | + <UButton |
| 79 | + color="neutral" |
| 80 | + variant="outline" |
| 81 | + size="md" |
| 82 | + trailing-icon="i-lucide-chevron-down" |
| 83 | + @click="toggleDropdown" |
| 84 | + > |
46 | 85 | Columns |
47 | 86 | </UButton> |
48 | | - </UDropdownMenu> |
| 87 | + |
| 88 | + <Transition |
| 89 | + enter-active-class="transition ease-out duration-100" |
| 90 | + enter-from-class="transform opacity-0 scale-95" |
| 91 | + enter-to-class="transform opacity-100 scale-100" |
| 92 | + leave-active-class="transition ease-in duration-75" |
| 93 | + leave-from-class="transform opacity-100 scale-100" |
| 94 | + leave-to-class="transform opacity-0 scale-95" |
| 95 | + > |
| 96 | + <div |
| 97 | + v-if="isOpen" |
| 98 | + class="ring-opacity-5 absolute left-0 z-10 mt-2 min-w-[220px] origin-top-right overflow-y-auto rounded-md bg-white shadow-lg ring-1 ring-black focus:outline-none dark:bg-gray-800" |
| 99 | + > |
| 100 | + <div class="py-1"> |
| 101 | + <div |
| 102 | + v-for="column in orderedColumns" |
| 103 | + :key="column.id" |
| 104 | + :draggable="true" |
| 105 | + :class="[ |
| 106 | + 'flex cursor-move items-center gap-2 px-3 py-2 transition-colors select-none hover:bg-gray-50 dark:hover:bg-gray-700', |
| 107 | + draggingColumnId === column.id && 'opacity-50', |
| 108 | + dragOverColumnId === column.id && 'bg-primary-50 dark:bg-primary-900/20', |
| 109 | + ]" |
| 110 | + @dragstart="(e: DragEvent) => handleDragStart(e, column.id)" |
| 111 | + @dragend="handleDragEnd" |
| 112 | + @dragover="(e: DragEvent) => handleDragOver(e, column.id)" |
| 113 | + @drop="(e: DragEvent) => handleDrop(e, column.id)" |
| 114 | + > |
| 115 | + <UIcon name="i-lucide-grip-vertical" class="h-4 w-4 text-gray-400" /> |
| 116 | + <UCheckbox |
| 117 | + :model-value="column.getIsVisible()" |
| 118 | + @update:model-value=" |
| 119 | + (checked: boolean | 'indeterminate') => toggleColumnVisibility(column.id, checked) |
| 120 | + " |
| 121 | + @click.stop |
| 122 | + /> |
| 123 | + <span class="flex-1 text-sm">{{ column.id }}</span> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + </Transition> |
| 128 | + </div> |
49 | 129 | </template> |
0 commit comments