|
| 1 | +import * as React from 'react'; |
| 2 | +import { Column, Table } from '@tanstack/react-table'; |
| 3 | +import { Check, PlusCircle } from 'lucide-react'; |
| 4 | + |
| 5 | +import { cn } from '@/lib/utils'; |
| 6 | +import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; |
| 7 | +import { Button } from '@/components/ui/button'; |
| 8 | +import { Separator } from '@/components/ui/separator'; |
| 9 | +import { Badge } from '@/components/ui/badge'; |
| 10 | +import { |
| 11 | + Command, |
| 12 | + CommandEmpty, |
| 13 | + CommandGroup, |
| 14 | + CommandInput, |
| 15 | + CommandItem, |
| 16 | + CommandList, |
| 17 | + CommandSeparator, |
| 18 | +} from './ui/command'; |
| 19 | + |
| 20 | +interface TableFilterProps<TData, TValue> { |
| 21 | + table: Table<TData>; |
| 22 | + column?: Column<TData, TValue>; |
| 23 | + title?: string; |
| 24 | + options: { |
| 25 | + label: string; |
| 26 | + value: string; |
| 27 | + icon?: React.ComponentType<{ className?: string }>; |
| 28 | + }[]; |
| 29 | + disabled?: boolean; |
| 30 | +} |
| 31 | + |
| 32 | +export function TableFilter<TData, TValue>({ |
| 33 | + table, |
| 34 | + column, |
| 35 | + title, |
| 36 | + options, |
| 37 | + disabled = false, |
| 38 | +}: Readonly<TableFilterProps<TData, TValue>>) { |
| 39 | + const facets = column?.getFacetedUniqueValues(); |
| 40 | + const selectedValues = new Set(column?.getFilterValue() as string[]); |
| 41 | + |
| 42 | + const isDropdownVariant = true; |
| 43 | + |
| 44 | + if (isDropdownVariant) { |
| 45 | + return ( |
| 46 | + <Popover> |
| 47 | + <PopoverTrigger asChild> |
| 48 | + <Button disabled={disabled} variant="outline" size="sm" className="h-8 border-dashed"> |
| 49 | + <PlusCircle /> |
| 50 | + {title} |
| 51 | + {selectedValues?.size > 0 && ( |
| 52 | + <> |
| 53 | + <Separator orientation="vertical" className="mx-2 h-4" /> |
| 54 | + <Badge variant="secondary" className="rounded-sm px-1 font-normal lg:hidden"> |
| 55 | + {selectedValues.size} |
| 56 | + </Badge> |
| 57 | + <div className="hidden space-x-1 lg:flex"> |
| 58 | + {selectedValues.size > 2 ? ( |
| 59 | + <Badge variant="secondary" className="rounded-sm px-1 font-normal"> |
| 60 | + {selectedValues.size} Selected |
| 61 | + </Badge> |
| 62 | + ) : ( |
| 63 | + options |
| 64 | + .filter((option) => selectedValues.has(option.value)) |
| 65 | + .map((option) => ( |
| 66 | + <Badge |
| 67 | + variant="secondary" |
| 68 | + key={option.value} |
| 69 | + className="rounded-sm px-1 font-normal" |
| 70 | + > |
| 71 | + {option.label} |
| 72 | + </Badge> |
| 73 | + )) |
| 74 | + )} |
| 75 | + </div> |
| 76 | + </> |
| 77 | + )} |
| 78 | + </Button> |
| 79 | + </PopoverTrigger> |
| 80 | + <PopoverContent className="p-0"> |
| 81 | + <Command> |
| 82 | + <CommandInput placeholder={title} /> |
| 83 | + <CommandList> |
| 84 | + <CommandEmpty> |
| 85 | + <span className="italic">No results found</span> |
| 86 | + </CommandEmpty> |
| 87 | + <CommandGroup> |
| 88 | + {options.map((option) => { |
| 89 | + const isSelected = selectedValues.has(option.value); |
| 90 | + return ( |
| 91 | + <CommandItem |
| 92 | + key={option.value} |
| 93 | + onSelect={() => { |
| 94 | + if (isSelected) { |
| 95 | + selectedValues.delete(option.value); |
| 96 | + } else { |
| 97 | + selectedValues.add(option.value); |
| 98 | + } |
| 99 | + const filterValues = Array.from(selectedValues); |
| 100 | + column?.setFilterValue(filterValues.length ? filterValues : undefined); |
| 101 | + table.resetRowSelection(); |
| 102 | + }} |
| 103 | + > |
| 104 | + <div |
| 105 | + className={cn( |
| 106 | + 'border-primary mr-2 flex h-4 w-4 items-center justify-center rounded-sm border', |
| 107 | + isSelected |
| 108 | + ? 'bg-primary text-primary-foreground' |
| 109 | + : 'opacity-50 [&_svg]:invisible', |
| 110 | + )} |
| 111 | + > |
| 112 | + <Check /> |
| 113 | + </div> |
| 114 | + {option.icon && ( |
| 115 | + <option.icon className="text-muted-foreground mr-2 h-4 w-4" /> |
| 116 | + )} |
| 117 | + <span>{option.label}</span> |
| 118 | + {facets?.get(option.value) && ( |
| 119 | + <span className="ml-auto flex h-4 w-4 items-center justify-center font-mono text-xs"> |
| 120 | + {facets.get(option.value)} |
| 121 | + </span> |
| 122 | + )} |
| 123 | + </CommandItem> |
| 124 | + ); |
| 125 | + })} |
| 126 | + </CommandGroup> |
| 127 | + {selectedValues.size > 0 && ( |
| 128 | + <> |
| 129 | + <CommandSeparator /> |
| 130 | + <CommandGroup> |
| 131 | + <CommandItem |
| 132 | + onSelect={() => column?.setFilterValue(undefined)} |
| 133 | + className="justify-center text-center" |
| 134 | + > |
| 135 | + Clear filters |
| 136 | + </CommandItem> |
| 137 | + </CommandGroup> |
| 138 | + </> |
| 139 | + )} |
| 140 | + </CommandList> |
| 141 | + </Command> |
| 142 | + </PopoverContent> |
| 143 | + </Popover> |
| 144 | + ); |
| 145 | + } |
| 146 | +} |
0 commit comments