|
| 1 | +import { Circle } from 'lucide-react'; |
| 2 | +import { AnimatePresence, motion, type Transition } from 'motion/react'; |
| 3 | +import { RadioGroup as RadioGroupPrimitive } from 'radix-ui'; |
| 4 | +import type { ComponentProps, HTMLAttributes } from 'react'; |
| 5 | + |
| 6 | +import { Card, CardContent, CardDescription, CardHeader, type CardProps, CardTitle } from './card'; |
| 7 | +import { RadioGroup } from './radio-group'; |
| 8 | +import { cn } from '../lib/utils'; |
| 9 | + |
| 10 | +export type ChoiceboxProps = ComponentProps<typeof RadioGroup> & { testId?: string }; |
| 11 | + |
| 12 | +export const Choicebox = ({ className, testId, ...props }: ChoiceboxProps) => ( |
| 13 | + <RadioGroup className={cn('w-full', className)} data-testid={testId} {...props} /> |
| 14 | +); |
| 15 | + |
| 16 | +export type ChoiceboxItemProps = RadioGroupPrimitive.RadioGroupItemProps & { testId?: string } & Partial< |
| 17 | + Pick<CardProps, 'size'> |
| 18 | + >; |
| 19 | + |
| 20 | +export const ChoiceboxItem = ({ className, children, testId, size, ...props }: ChoiceboxItemProps) => ( |
| 21 | + <RadioGroupPrimitive.Item {...props}> |
| 22 | + <Card |
| 23 | + size={size} |
| 24 | + className={cn( |
| 25 | + 'flex cursor-pointer flex-row items-start justify-between rounded-md p-4 shadow-none transition-all border-2 border-solid !border-border text-left', |
| 26 | + '[&[data-state="checked"]]:border-selected', |
| 27 | + '[&[data-state="checked"]]:bg-selected/5', |
| 28 | + 'hover:shadow-elevated', |
| 29 | + className, |
| 30 | + )} |
| 31 | + data-testid={testId} |
| 32 | + > |
| 33 | + {children} |
| 34 | + </Card> |
| 35 | + </RadioGroupPrimitive.Item> |
| 36 | +); |
| 37 | + |
| 38 | +export type ChoiceboxItemHeaderProps = ComponentProps<typeof CardHeader>; |
| 39 | + |
| 40 | +export const ChoiceboxItemHeader = ({ className, ...props }: ComponentProps<typeof CardHeader>) => ( |
| 41 | + <CardHeader className={cn('flex-1 p-0', className)} {...props} /> |
| 42 | +); |
| 43 | + |
| 44 | +export type ChoiceboxItemTitleProps = ComponentProps<typeof CardTitle>; |
| 45 | + |
| 46 | +export const ChoiceboxItemTitle = ({ className, ...props }: ChoiceboxItemTitleProps) => ( |
| 47 | + <CardTitle className={cn('flex items-center gap-3', className)} {...props} /> |
| 48 | +); |
| 49 | + |
| 50 | +export type ChoiceboxItemSubtitleProps = HTMLAttributes<HTMLSpanElement>; |
| 51 | + |
| 52 | +export const ChoiceboxItemSubtitle = ({ className, ...props }: ChoiceboxItemSubtitleProps) => ( |
| 53 | + <span className={cn('font-normal text-muted-foreground text-xs', className)} {...props} /> |
| 54 | +); |
| 55 | + |
| 56 | +export type ChoiceboxItemDescriptionProps = ComponentProps<typeof CardDescription>; |
| 57 | + |
| 58 | +export const ChoiceboxItemDescription = ({ className, ...props }: ChoiceboxItemDescriptionProps) => ( |
| 59 | + <CardDescription className={cn('text-sm', className)} {...props} /> |
| 60 | +); |
| 61 | + |
| 62 | +export type ChoiceboxItemContentProps = ComponentProps<typeof CardContent>; |
| 63 | + |
| 64 | +export const ChoiceboxItemContent = ({ className, ...props }: ChoiceboxItemContentProps) => ( |
| 65 | + <CardContent |
| 66 | + className={cn( |
| 67 | + 'flex aspect-square size-4 shrink-0 items-center justify-center rounded-full border border-input p-0 text-selected shadow-xs outline-none transition-[color,box-shadow] focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:bg-input/30 dark:aria-invalid:ring-destructive/40', |
| 68 | + className, |
| 69 | + )} |
| 70 | + {...props} |
| 71 | + /> |
| 72 | +); |
| 73 | + |
| 74 | +export type ChoiceboxItemIndicatorProps = ComponentProps<typeof RadioGroupPrimitive.Indicator> & { |
| 75 | + transition?: Transition; |
| 76 | +}; |
| 77 | + |
| 78 | +export const ChoiceboxItemIndicator = ({ |
| 79 | + className, |
| 80 | + transition = { type: 'spring', stiffness: 200, damping: 16 }, |
| 81 | + ...props |
| 82 | +}: ChoiceboxItemIndicatorProps) => ( |
| 83 | + <RadioGroupPrimitive.Indicator |
| 84 | + data-slot="radio-group-indicator" |
| 85 | + className={cn('flex items-center justify-center', className)} |
| 86 | + {...props} |
| 87 | + > |
| 88 | + <AnimatePresence> |
| 89 | + <motion.div |
| 90 | + key="radio-group-indicator-circle" |
| 91 | + data-slot="radio-group-indicator-circle" |
| 92 | + initial={{ opacity: 0, scale: 0 }} |
| 93 | + animate={{ opacity: 1, scale: 1 }} |
| 94 | + exit={{ opacity: 0, scale: 0 }} |
| 95 | + transition={transition} |
| 96 | + > |
| 97 | + <Circle className="size-2.5 fill-current text-current" /> |
| 98 | + </motion.div> |
| 99 | + </AnimatePresence> |
| 100 | + </RadioGroupPrimitive.Indicator> |
| 101 | +); |
0 commit comments