Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | 467x 1637x 103x 36x 1x 1x 1x 1x 1x 1x 89x 336x 4x | import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { ArrowUpDown, Check, ChevronDown, X } from "lucide-react";
import React, { useEffect, useRef, useState } from "react";
export interface FilterOption<T extends string = string> {
value: T;
label: string;
/** Matching-item count, rendered alongside the label where the control
* supports it (`SegmentedFilter`). Omit where the number is meaningless. */
count?: number;
}
// ─── Single-select filter ─────────────────────────────────────────────────────
export interface FilterSelectProps<T extends string = string> {
value: T;
onValueChange: (value: T) => void;
placeholder: string;
options: FilterOption<T>[];
/** Highlights the trigger when the filter is narrowing results. */
active?: boolean;
}
export function FilterSelect<T extends string = string>({
value,
onValueChange,
placeholder,
options,
active,
}: FilterSelectProps<T>) {
return (
<Select value={value} onValueChange={onValueChange}>
<SelectTrigger
size="sm"
className={`text-xs h-8 gap-1.5 ${
active
? "border-gray-900 dark:border-gray-100 text-gray-900 dark:text-gray-100 font-medium"
: "text-gray-500 dark:text-gray-400"
}`}
>
<SelectValue placeholder={placeholder} />
</SelectTrigger>
<SelectContent>
{options.map((o) => (
<SelectItem key={o.value} value={o.value} className="text-xs">
{o.label}
</SelectItem>
))}
</SelectContent>
</Select>
);
}
// ─── Sort select ──────────────────────────────────────────────────────────────
export interface SortSelectProps<T extends string = string> {
value: T;
onChange: (value: T) => void;
options: FilterOption<T>[];
/** The default sort key — the trigger is only highlighted away from it. */
defaultValue?: T;
}
export function SortSelect<T extends string = string>({
value,
onChange,
options,
defaultValue,
}: SortSelectProps<T>) {
return (
<div className="flex items-center gap-1.5">
<ArrowUpDown className="size-3.5 text-gray-400 shrink-0" />
<FilterSelect
value={value}
onValueChange={onChange}
placeholder="Sort by"
options={options}
active={defaultValue !== undefined && value !== defaultValue}
/>
</div>
);
}
// ─── Multi-select filter dropdown ─────────────────────────────────────────────
export interface MultiSelectFilterProps {
label: string;
icon?: React.ReactNode;
options: string[];
selected: string[];
onChange: (selected: string[]) => void;
capitalizeOptions?: boolean;
}
export const MultiSelectFilter: React.FC<MultiSelectFilterProps> = ({
label,
icon,
options,
selected,
onChange,
capitalizeOptions,
}) => {
const [open, setOpen] = useState(false);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
Eif (!open) return;
const handler = (e: MouseEvent) => {
if (ref.current && !ref.current.contains(e.target as Node))
setOpen(false);
};
document.addEventListener("mousedown", handler);
return () => document.removeEventListener("mousedown", handler);
}, [open]);
const toggle = (val: string) =>
onChange(
selected.includes(val)
? selected.filter((v) => v !== val)
: [...selected, val],
);
return (
<div ref={ref} className="relative">
<button
type="button"
onClick={() => setOpen((v) => !v)}
className={`flex items-center gap-1.5 px-3 py-2 rounded-lg text-xs font-medium border transition-colors ${
selected.length > 0
? "border-gray-900 dark:border-gray-200 bg-gray-900 dark:bg-gray-100 text-white dark:text-gray-900"
: "border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700"
}`}
>
{icon}
{label}
{selected.length > 0 && (
<span className="flex items-center justify-center size-4 rounded-full bg-white/20 dark:bg-gray-900/20 text-[10px] font-semibold tabular-nums">
{selected.length}
</span>
)}
<ChevronDown
className={`size-3 shrink-0 transition-transform ${open ? "rotate-180" : ""}`}
/>
</button>
{open && (
<div className="absolute left-0 top-full mt-1.5 z-50 min-w-44 rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-lg overflow-hidden">
<div className="flex items-center justify-between px-3 py-2 border-b border-gray-100 dark:border-gray-700">
<span className="text-[10px] font-semibold uppercase tracking-wider text-gray-400 dark:text-gray-500">
{label}
</span>
{selected.length > 0 && (
<button
type="button"
onClick={() => onChange([])}
className="flex items-center gap-0.5 text-[10px] text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 transition-colors"
>
<X className="size-2.5" />
Clear
</button>
)}
</div>
<div className="max-h-56 overflow-y-auto py-1">
{options.map((opt) => {
const active = selected.includes(opt);
return (
<button
type="button"
key={opt}
onClick={() => toggle(opt)}
className="flex items-center gap-2 w-full px-3 py-1.5 text-xs text-left hover:bg-gray-50 dark:hover:bg-gray-700/60 transition-colors"
>
<span
className={`flex items-center justify-center size-3.5 rounded border transition-colors shrink-0 ${
active
? "bg-gray-900 dark:bg-gray-100 border-gray-900 dark:border-gray-100"
: "border-gray-300 dark:border-gray-600"
}`}
>
{active && (
<Check className="size-2.5 text-white dark:text-gray-900" />
)}
</span>
<span
className={`text-gray-700 dark:text-gray-300 ${capitalizeOptions ? "capitalize" : ""}`}
>
{opt}
</span>
</button>
);
})}
</div>
</div>
)}
</div>
);
};
// ─── Segmented filter (e.g. All / System / Custom) ───────────────────────────
export interface SegmentedFilterProps<T extends string = string> {
value: T;
onChange: (value: T) => void;
options: FilterOption<T>[];
}
export function SegmentedFilter<T extends string = string>({
value,
onChange,
options,
}: SegmentedFilterProps<T>) {
return (
<div className="flex items-center rounded-lg border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 p-0.5 gap-0.5">
{options.map((opt) => (
<button
key={opt.value}
onClick={() => onChange(opt.value)}
className={`px-2.5 py-1 rounded-md text-xs font-medium transition-colors ${
value === opt.value
? "bg-gray-900 dark:bg-gray-100 text-white dark:text-gray-900"
: "text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200"
}`}
>
{opt.label}
{opt.count !== undefined && (
<span
className={`ml-1 tabular-nums ${
value === opt.value
? "text-white/60 dark:text-gray-900/60"
: "text-gray-400 dark:text-gray-500"
}`}
>
{opt.count}
</span>
)}
</button>
))}
</div>
);
}
|