diff --git a/src/components/category-selector.tsx b/src/components/category-selector.tsx index adb6582..8e6c5a6 100644 --- a/src/components/category-selector.tsx +++ b/src/components/category-selector.tsx @@ -1,7 +1,7 @@ import { ChevronDown } from 'lucide-react' import { CategoryIcon } from '@/app/groups/[groupId]/expenses/category-icon' -import { Button } from '@/components/ui/button' +import { Button, ButtonProps } from '@/components/ui/button' import { Command, CommandEmpty, @@ -9,13 +9,15 @@ import { CommandInput, CommandItem, } from '@/components/ui/command' +import { Drawer, DrawerContent, DrawerTrigger } from '@/components/ui/drawer' import { Popover, PopoverContent, PopoverTrigger, } from '@/components/ui/popover' +import { useMediaQuery } from '@/lib/hooks' import { Category } from '@prisma/client' -import { useState } from 'react' +import { forwardRef, useState } from 'react' type Props = { categories: Category[] @@ -30,7 +32,57 @@ export function CategorySelector({ }: Props) { const [open, setOpen] = useState(false) const [value, setValue] = useState(defaultValue) + const isDesktop = useMediaQuery('(min-width: 768px)') + const selectedCategory = + categories.find((category) => category.id === value) ?? categories[0] + + if (isDesktop) { + return ( + + + + + + { + setValue(id) + onValueChange(id) + setOpen(false) + }} + /> + + + ) + } + + return ( + + + + + + { + setValue(id) + onValueChange(id) + setOpen(false) + }} + /> + + + ) +} + +function CategoryCommand({ + categories, + onValueChange, +}: { + categories: Category[] + onValueChange: (categoryId: Category['id']) => void +}) { const categoriesByGroup = categories.reduce>( (acc, category) => ({ ...acc, @@ -39,56 +91,62 @@ export function CategorySelector({ {}, ) - 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} -
-
- ))} -
- ), - )} -
-
-
-
+ + + No category found. +
+ {Object.entries(categoriesByGroup).map( + ([group, groupCategories], index) => ( + + {groupCategories.map((category) => ( + { + const id = Number(currentValue.split(' ')[0]) + onValueChange(id) + }} + > + + + ))} + + ), + )} +
+
+ ) +} + +type CategoryButtonProps = { + category: Category + open: boolean +} +const CategoryButton = forwardRef( + ({ category, open, ...props }: ButtonProps & CategoryButtonProps, ref) => { + return ( + + ) + }, +) +CategoryButton.displayName = 'CategoryButton' + +function CategoryLabel({ category }: { category: Category }) { + return ( +
+ + {category.name} +
) }