-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathfilter-popover.tsx
More file actions
88 lines (81 loc) · 2.98 KB
/
filter-popover.tsx
File metadata and controls
88 lines (81 loc) · 2.98 KB
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
import { ListFilter } from 'lucide-react';
import { useId } from 'react';
import { Button } from '@/components/ui/button';
import { Checkbox } from '@/components/ui/checkbox';
import { Label } from '@/components/ui/label';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
const CATEGORIES = [
{ label: 'Stacks.js', value: 'stacks.js' },
{ label: 'Clarity', value: 'clarity' },
{ label: 'Bitcoin', value: 'bitcoin' },
{ label: 'API', value: 'api' },
];
interface FilterPopoverProps {
selectedCategories: string[];
onCategoriesChange: (categories: string[]) => void;
}
function FilterPopover({ selectedCategories, onCategoriesChange }: FilterPopoverProps) {
const id = useId();
const handleCheckboxChange = (category: string, checked: boolean) => {
if (checked) {
onCategoriesChange([...selectedCategories, category]);
} else {
onCategoriesChange(selectedCategories.filter((c) => c !== category));
}
};
const handleClear = () => {
onCategoriesChange([]);
};
return (
<div className="flex flex-col gap-4">
<Popover>
<PopoverTrigger asChild>
<Button
size="icon"
aria-label="Filter by category"
className="border border-input bg-background hover:bg-accent hover:text-accent-foreground"
>
<ListFilter size={16} strokeWidth={2} aria-hidden="true" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-48 p-3">
<div className="space-y-3">
<div className="text-sm font-medium text-muted-foreground">Filter by category</div>
<form className="space-y-3" onSubmit={(e) => e.preventDefault()}>
{CATEGORIES.map((category) => (
<div key={category.value} className="flex items-center gap-2">
<Checkbox
id={`${id}-${category.value}`}
checked={selectedCategories.includes(category.value)}
onCheckedChange={(checked) =>
handleCheckboxChange(category.value, checked as boolean)
}
/>
<Label htmlFor={`${id}-${category.value}`} className="font-normal">
{category.label}
</Label>
</div>
))}
<div
role="separator"
aria-orientation="horizontal"
className="-mx-3 my-1 h-px bg-border"
/>
<div className="flex justify-between gap-2">
<Button
type="button"
size="sm"
className="h-7 px-2 border border-input bg-background hover:bg-accent hover:text-accent-foreground"
onClick={handleClear}
>
Clear
</Button>
</div>
</form>
</div>
</PopoverContent>
</Popover>
</div>
);
}
export { FilterPopover };