From 92156b29cbcc496af705982bdf18dd558e765acb Mon Sep 17 00:00:00 2001 From: Sebastien Castiel Date: Wed, 17 Jan 2024 12:07:03 -0500 Subject: Use combobox for category selector --- src/components/category-selector.tsx | 94 ++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/components/category-selector.tsx (limited to 'src/components/category-selector.tsx') diff --git a/src/components/category-selector.tsx b/src/components/category-selector.tsx new file mode 100644 index 0000000..adb6582 --- /dev/null +++ b/src/components/category-selector.tsx @@ -0,0 +1,94 @@ +import { ChevronDown } from 'lucide-react' + +import { CategoryIcon } from '@/app/groups/[groupId]/expenses/category-icon' +import { Button } from '@/components/ui/button' +import { + Command, + CommandEmpty, + CommandGroup, + CommandInput, + CommandItem, +} from '@/components/ui/command' +import { + Popover, + PopoverContent, + PopoverTrigger, +} from '@/components/ui/popover' +import { Category } from '@prisma/client' +import { useState } from 'react' + +type Props = { + categories: Category[] + onValueChange: (categoryId: Category['id']) => void + defaultValue: Category['id'] +} + +export function CategorySelector({ + categories, + onValueChange, + defaultValue, +}: Props) { + const [open, setOpen] = useState(false) + const [value, setValue] = useState(defaultValue) + + const categoriesByGroup = categories.reduce>( + (acc, category) => ({ + ...acc, + [category.grouping]: [...(acc[category.grouping] ?? []), category], + }), + {}, + ) + + const selectedCategory = + categories.find((category) => category.id === value) ?? categories[0] + + return ( + + + + + + + + No category found. +
+ {Object.entries(categoriesByGroup).map( + ([group, groupCategories], index) => ( + + {groupCategories.map((category) => ( + { + const id = Number(currentValue.split(' ')[0]) + setValue(id) + onValueChange(id) + setOpen(false) + }} + > +
+ + {category.name} +
+
+ ))} +
+ ), + )} +
+
+
+
+ ) +} -- cgit v1.3