Files
DevToysWeb/components/ui/select.tsx
2023-08-11 20:31:18 +09:00

113 lines
3.7 KiB
TypeScript

"use client";
import * as React from "react";
import * as SelectPrimitive from "@radix-ui/react-select";
import { cn } from "@/lib/style";
import { icons } from "@/components/icons";
import { Indicator } from "@/components/indicator";
export type Props = React.ComponentPropsWithoutRef<typeof SelectPrimitive.Root>;
export const { Root, Group, Value } = SelectPrimitive;
export const Trigger = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Trigger>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger>
>(({ className, children, ...props }, ref) => (
<SelectPrimitive.Trigger
{...{ ref }}
className={cn(
"flex h-9 w-full items-center justify-between rounded-md border bg-select px-2.5 py-1.5",
"placeholder:text-muted-foreground",
"hover:bg-select-hover",
"disabled:cursor-not-allowed disabled:opacity-50",
className
)}
{...props}
>
{children}
<SelectPrimitive.Icon asChild>
<icons.ChevronDown className="h-4 w-4 opacity-50" />
</SelectPrimitive.Icon>
</SelectPrimitive.Trigger>
));
Trigger.displayName = SelectPrimitive.Trigger.displayName;
export const Content = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Content>
>(({ className, children, position = "popper", ...props }, ref) => (
<SelectPrimitive.Portal>
<SelectPrimitive.Content
{...{ ref, position }}
className={cn(
"relative z-50 overflow-hidden rounded-md border bg-select-content text-select-content-foreground shadow-md animate-in fade-in-80",
position === "popper" && "translate-y-1",
className
)}
{...props}
>
<SelectPrimitive.Viewport
className={cn(
"p-1",
position === "popper" &&
"h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]"
)}
>
{children}
</SelectPrimitive.Viewport>
</SelectPrimitive.Content>
</SelectPrimitive.Portal>
));
Content.displayName = SelectPrimitive.Content.displayName;
export const Label = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Label>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Label>
>(({ className, ...props }, ref) => (
<SelectPrimitive.Label
{...{ ref }}
className={cn("py-1.5 pl-8 pr-2 font-semibold", className)}
{...props}
/>
));
Label.displayName = SelectPrimitive.Label.displayName;
export const Item = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item>
>(({ className, children, ...props }, ref) => (
<SelectPrimitive.Item
{...{ ref }}
className={cn(
"relative flex w-full cursor-default select-none items-center rounded-sm px-2.5 py-1.5 outline-none",
"hover:bg-select-item-hover",
"focus:bg-select-item-focus",
"data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
className
)}
{...props}
>
<span className="absolute left-0">
<SelectPrimitive.ItemIndicator className="flex items-center">
<Indicator />
</SelectPrimitive.ItemIndicator>
</span>
<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
</SelectPrimitive.Item>
));
Item.displayName = SelectPrimitive.Item.displayName;
export const Separator = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Separator>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Separator>
>(({ className, ...props }, ref) => (
<SelectPrimitive.Separator
{...{ ref }}
className={cn("-mx-1 my-1 h-px bg-muted", className)}
{...props}
/>
));
Separator.displayName = SelectPrimitive.Separator.displayName;