infoalloggi-monorepo/apps/infoalloggi/src/components/ui/select.tsx

186 lines
5.8 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
"use client";
import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from "lucide-react";
2025-08-28 18:27:07 +02:00
import { Select as SelectPrimitive } from "radix-ui";
import type * as React from "react";
2025-08-04 17:45:44 +02:00
import { cn } from "~/lib/utils";
function Select({
2025-08-28 18:27:07 +02:00
...props
2025-08-04 17:45:44 +02:00
}: React.ComponentProps<typeof SelectPrimitive.Root>) {
2025-08-28 18:27:07 +02:00
return <SelectPrimitive.Root data-slot="select" {...props} />;
2025-08-04 17:45:44 +02:00
}
function SelectGroup({
2025-08-28 18:27:07 +02:00
...props
2025-08-04 17:45:44 +02:00
}: React.ComponentProps<typeof SelectPrimitive.Group>) {
2025-08-28 18:27:07 +02:00
return <SelectPrimitive.Group data-slot="select-group" {...props} />;
2025-08-04 17:45:44 +02:00
}
function SelectValue({
2025-08-28 18:27:07 +02:00
...props
2025-08-04 17:45:44 +02:00
}: React.ComponentProps<typeof SelectPrimitive.Value>) {
2025-08-28 18:27:07 +02:00
return <SelectPrimitive.Value data-slot="select-value" {...props} />;
2025-08-04 17:45:44 +02:00
}
function SelectTrigger({
2025-08-28 18:27:07 +02:00
className,
size = "default",
children,
...props
2025-08-04 17:45:44 +02:00
}: React.ComponentProps<typeof SelectPrimitive.Trigger> & {
2025-08-28 18:27:07 +02:00
size?: "sm" | "default";
2025-08-04 17:45:44 +02:00
}) {
2025-08-28 18:27:07 +02:00
return (
<SelectPrimitive.Trigger
className={cn(
"flex w-fit items-center justify-between gap-2 whitespace-nowrap rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-xs outline-hidden 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 data-[size=default]:h-9 data-[size=sm]:h-8 data-placeholder:text-muted-foreground *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-2 dark:bg-input/30 dark:aria-invalid:ring-destructive/40 dark:hover:bg-input/50 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0",
2025-08-28 18:27:07 +02:00
className,
)}
2025-08-29 16:18:32 +02:00
data-size={size}
data-slot="select-trigger"
2025-08-28 18:27:07 +02:00
{...props}
>
{children}
<SelectPrimitive.Icon asChild>
<ChevronDownIcon className="size-4 opacity-50" />
</SelectPrimitive.Icon>
</SelectPrimitive.Trigger>
);
2025-08-04 17:45:44 +02:00
}
function SelectContent({
2025-08-28 18:27:07 +02:00
className,
children,
position = "popper",
...props
2025-08-04 17:45:44 +02:00
}: React.ComponentProps<typeof SelectPrimitive.Content>) {
2025-08-28 18:27:07 +02:00
return (
<SelectPrimitive.Portal>
<SelectPrimitive.Content
className={cn(
"data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 relative z-50 max-h-(--radix-select-content-available-height) min-w-32 origin-(--radix-select-content-transform-origin) overflow-y-auto overflow-x-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=closed]:animate-out data-[state=open]:animate-in",
2025-08-28 18:27:07 +02:00
position === "popper" &&
"data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=bottom]:translate-y-1 data-[side=top]:-translate-y-1",
2025-08-28 18:27:07 +02:00
className,
)}
2025-08-29 16:18:32 +02:00
data-slot="select-content"
2025-08-28 18:27:07 +02:00
position={position}
{...props}
>
<SelectScrollUpButton />
<SelectPrimitive.Viewport
className={cn(
"p-1",
position === "popper" &&
"h-(--radix-select-trigger-height) w-full min-w-(--radix-select-trigger-width) scroll-my-1",
2025-08-28 18:27:07 +02:00
)}
>
{children}
</SelectPrimitive.Viewport>
<SelectScrollDownButton />
</SelectPrimitive.Content>
</SelectPrimitive.Portal>
);
2025-08-04 17:45:44 +02:00
}
function SelectLabel({
2025-08-28 18:27:07 +02:00
className,
...props
2025-08-04 17:45:44 +02:00
}: React.ComponentProps<typeof SelectPrimitive.Label>) {
2025-08-28 18:27:07 +02:00
return (
<SelectPrimitive.Label
2025-10-10 16:18:43 +02:00
className={cn("px-2 py-1.5 text-muted-foreground text-xs", className)}
2025-08-29 16:18:32 +02:00
data-slot="select-label"
2025-08-28 18:27:07 +02:00
{...props}
/>
);
2025-08-04 17:45:44 +02:00
}
function SelectItem({
2025-08-28 18:27:07 +02:00
className,
children,
...props
2025-08-04 17:45:44 +02:00
}: React.ComponentProps<typeof SelectPrimitive.Item>) {
2025-08-28 18:27:07 +02:00
return (
<SelectPrimitive.Item
className={cn(
"relative flex w-full cursor-default select-none items-center gap-2 rounded-sm py-1.5 pr-8 pl-2 text-sm outline-hidden focus:bg-accent focus:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",
2025-08-28 18:27:07 +02:00
className,
)}
2025-08-29 16:18:32 +02:00
data-slot="select-item"
2025-08-28 18:27:07 +02:00
{...props}
>
<span className="absolute right-2 flex size-3.5 items-center justify-center">
<SelectPrimitive.ItemIndicator>
<CheckIcon className="size-4" />
</SelectPrimitive.ItemIndicator>
</span>
<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
</SelectPrimitive.Item>
);
2025-08-04 17:45:44 +02:00
}
function SelectSeparator({
2025-08-28 18:27:07 +02:00
className,
...props
2025-08-04 17:45:44 +02:00
}: React.ComponentProps<typeof SelectPrimitive.Separator>) {
2025-08-28 18:27:07 +02:00
return (
<SelectPrimitive.Separator
className={cn("pointer-events-none -mx-1 my-1 h-px bg-border", className)}
2025-08-29 16:18:32 +02:00
data-slot="select-separator"
2025-08-28 18:27:07 +02:00
{...props}
/>
);
2025-08-04 17:45:44 +02:00
}
function SelectScrollUpButton({
2025-08-28 18:27:07 +02:00
className,
...props
2025-08-04 17:45:44 +02:00
}: React.ComponentProps<typeof SelectPrimitive.ScrollUpButton>) {
2025-08-28 18:27:07 +02:00
return (
<SelectPrimitive.ScrollUpButton
className={cn(
"flex cursor-default items-center justify-center py-1",
className,
)}
2025-08-29 16:18:32 +02:00
data-slot="select-scroll-up-button"
2025-08-28 18:27:07 +02:00
{...props}
>
<ChevronUpIcon className="size-4" />
</SelectPrimitive.ScrollUpButton>
);
2025-08-04 17:45:44 +02:00
}
function SelectScrollDownButton({
2025-08-28 18:27:07 +02:00
className,
...props
2025-08-04 17:45:44 +02:00
}: React.ComponentProps<typeof SelectPrimitive.ScrollDownButton>) {
2025-08-28 18:27:07 +02:00
return (
<SelectPrimitive.ScrollDownButton
className={cn(
"flex cursor-default items-center justify-center py-1",
className,
)}
2025-08-29 16:18:32 +02:00
data-slot="select-scroll-down-button"
2025-08-28 18:27:07 +02:00
{...props}
>
<ChevronDownIcon className="size-4" />
</SelectPrimitive.ScrollDownButton>
);
2025-08-04 17:45:44 +02:00
}
export {
2025-08-28 18:27:07 +02:00
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectScrollDownButton,
SelectScrollUpButton,
SelectSeparator,
SelectTrigger,
SelectValue,
2025-08-04 17:45:44 +02:00
};