Skip to content

Select

Component for selecting options

Code

"use client"

import React, { ComponentPropsWithoutRef, ElementRef } from "react"
import * as SelectPrimitive from "@radix-ui/react-select"
import { CaretDown, Check } from "@phosphor-icons/react"
import { classNames } from "../../helpers"

const Select = SelectPrimitive.Root
const SelectGroup = SelectPrimitive.Group
const SelectValue = SelectPrimitive.Value

const SelectContent = React.forwardRef<ElementRef<typeof SelectPrimitive.Content>, ComponentPropsWithoutRef<typeof SelectPrimitive.Content>>(
	({ className, children, position = "popper", ...rest }, ref) => (
		<SelectPrimitive.Portal>
			<SelectPrimitive.Content
				ref={ref}
				className={classNames(
					"relative z-50 min-w-[8rem] overflow-hidden rounded-md border bg-white text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out 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",
					"data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
					className,
				)}
				position={position}
				{...rest}
			>
				<SelectPrimitive.Viewport className={classNames("p-1", "h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]")}>
					{children}
				</SelectPrimitive.Viewport>
			</SelectPrimitive.Content>
		</SelectPrimitive.Portal>
	),
)

const SelectItem = React.forwardRef<ElementRef<typeof SelectPrimitive.Item>, ComponentPropsWithoutRef<typeof SelectPrimitive.Item>>(({ className, children, ...rest }, ref) => (
	<SelectPrimitive.Item
		ref={ref}
		className={classNames(
			"relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-2 pr-8 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 cursor-pointer hover:bg-gray-100 hover:text-gray-500",
			className,
		)}
		{...rest}
	>
		<span className="absolute right-2 flex h-3.5 w-3.5 items-center justify-center">
			<SelectPrimitive.ItemIndicator>
				<Check className="h-4 w-4" />
			</SelectPrimitive.ItemIndicator>
		</span>
		<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
	</SelectPrimitive.Item>
))

const SelectLabel = React.forwardRef<ElementRef<typeof SelectPrimitive.Label>, ComponentPropsWithoutRef<typeof SelectPrimitive.Label>>(({ className, ...rest }, ref) => (
	<SelectPrimitive.Label ref={ref} className={classNames("px-2 py-1.5 text-sm font-semibold", className)} {...rest} />
))

const SelectTrigger = React.forwardRef<ElementRef<typeof SelectPrimitive.Trigger>, ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger>>(
	({ className, children, ...rest }, ref) => (
		<SelectPrimitive.Trigger
			ref={ref}
			className={classNames(
				"flex h-9 w-full items-center justify-between bg-transparent px-3 py-2 text-sm  placeholder:text-muted-foreground focus:outline-none disabled:cursor-not-allowed disabled:opacity-50",
				className,
			)}
			{...rest}
		>
			{children}
			<SelectPrimitive.Icon asChild>
				<CaretDown className="h-4 w-4 opacity-50" />
			</SelectPrimitive.Icon>
		</SelectPrimitive.Trigger>
	),
)

SelectContent.displayName = SelectPrimitive.Content.displayName
SelectItem.displayName = SelectPrimitive.Item.displayName
SelectLabel.displayName = SelectPrimitive.Label.displayName
SelectTrigger.displayName = SelectPrimitive.Trigger.displayName

export { Select, SelectGroup, SelectValue, SelectTrigger, SelectContent, SelectLabel, SelectItem }