|
| 1 | +<script lang="ts"> |
| 2 | + import { Search, X, SlidersHorizontal } from 'lucide-svelte'; |
| 3 | + import { Input } from '$lib/components/ui/input'; |
| 4 | + import { Button } from '$lib/components/ui/button'; |
| 5 | + import * as DropdownMenu from '$lib/components/ui/dropdown-menu'; |
| 6 | + import { FilterDialog } from '$lib/components/ui/filter-dialog'; |
| 7 | + import type { FilterState, FilterCategory } from '$lib/components/ui/filter-dialog'; |
| 8 | + import type { ColumnDef, VisibilityState } from '@tanstack/table-core'; |
| 9 | +
|
| 10 | + type ColumnWithAccessor = ColumnDef<unknown, unknown> & { accessorKey?: string }; |
| 11 | +
|
| 12 | + interface FilterBarProps { |
| 13 | + filterCategories?: FilterCategory[]; |
| 14 | + filters?: FilterState; |
| 15 | + onFilterChange?: (filters: FilterState) => void; |
| 16 | + search?: string; |
| 17 | + onSearchChange?: (search: string) => void; |
| 18 | + columns?: ColumnWithAccessor[]; |
| 19 | + columnVisibility?: VisibilityState; |
| 20 | + onColumnVisibilityChange?: (visibility: VisibilityState) => void; |
| 21 | + } |
| 22 | +
|
| 23 | + let { |
| 24 | + filterCategories = [], |
| 25 | + filters = {}, |
| 26 | + onFilterChange, |
| 27 | + search, |
| 28 | + onSearchChange, |
| 29 | + columns = [], |
| 30 | + columnVisibility = {}, |
| 31 | + onColumnVisibilityChange |
| 32 | + }: FilterBarProps = $props(); |
| 33 | +
|
| 34 | + function handleSearchInput(e: Event) { |
| 35 | + onSearchChange?.((e.target as HTMLInputElement).value); |
| 36 | + } |
| 37 | +
|
| 38 | + function clearSearch() { |
| 39 | + onSearchChange?.(''); |
| 40 | + } |
| 41 | +
|
| 42 | + function toggleColumn(columnId: string, checked: boolean) { |
| 43 | + onColumnVisibilityChange?.({ |
| 44 | + ...columnVisibility, |
| 45 | + [columnId]: checked |
| 46 | + }); |
| 47 | + } |
| 48 | +</script> |
| 49 | + |
| 50 | +<div class="flex items-center gap-2 mb-6 flex-wrap"> |
| 51 | + <!-- Filter Dialog --> |
| 52 | + <FilterDialog |
| 53 | + categories={filterCategories} |
| 54 | + {filters} |
| 55 | + onFilterChange={(newFilters: FilterState) => onFilterChange?.(newFilters)} |
| 56 | + /> |
| 57 | + |
| 58 | + <!-- Search (optional) --> |
| 59 | + {#if onSearchChange !== undefined} |
| 60 | + <div class="relative w-64"> |
| 61 | + <Search class="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" /> |
| 62 | + <Input |
| 63 | + type="text" |
| 64 | + placeholder="Search..." |
| 65 | + value={search ?? ''} |
| 66 | + oninput={handleSearchInput} |
| 67 | + class="h-8 pl-9 pr-8 text-sm" |
| 68 | + /> |
| 69 | + {#if search} |
| 70 | + <button |
| 71 | + type="button" |
| 72 | + class="absolute right-2 top-1/2 -translate-y-1/2 p-0.5 hover:bg-muted rounded" |
| 73 | + onclick={clearSearch} |
| 74 | + > |
| 75 | + <X class="h-3 w-3 text-muted-foreground" /> |
| 76 | + </button> |
| 77 | + {/if} |
| 78 | + </div> |
| 79 | + {/if} |
| 80 | + |
| 81 | + <!-- Spacer --> |
| 82 | + <div class="flex-1"></div> |
| 83 | + |
| 84 | + <!-- Column Visibility --> |
| 85 | + <DropdownMenu.Root> |
| 86 | + <DropdownMenu.Trigger> |
| 87 | + {#snippet child({ props }: { props: Record<string, unknown> })} |
| 88 | + <Button variant="ghost" size="icon" class="h-8 w-8" {...props}> |
| 89 | + <SlidersHorizontal class="h-4 w-4" /> |
| 90 | + </Button> |
| 91 | + {/snippet} |
| 92 | + </DropdownMenu.Trigger> |
| 93 | + <DropdownMenu.Content align="end"> |
| 94 | + <DropdownMenu.Label>Toggle Columns</DropdownMenu.Label> |
| 95 | + <DropdownMenu.Separator /> |
| 96 | + {#each columns as column} |
| 97 | + {#if (column.accessorKey || column.id) && column.header} |
| 98 | + {@const columnId = (column.id || column.accessorKey) as string} |
| 99 | + {@const isVisible = columnVisibility[columnId] !== false} |
| 100 | + <DropdownMenu.CheckboxItem |
| 101 | + checked={isVisible} |
| 102 | + onCheckedChange={(checked: boolean) => toggleColumn(columnId, checked)} |
| 103 | + > |
| 104 | + {column.header} |
| 105 | + </DropdownMenu.CheckboxItem> |
| 106 | + {/if} |
| 107 | + {/each} |
| 108 | + </DropdownMenu.Content> |
| 109 | + </DropdownMenu.Root> |
| 110 | +</div> |
0 commit comments