import { Field as BaseField } from '@base-ui/react/field' import type { ComponentProps, ReactNode } from 'react' import { cn } from '../lib/cn' import { controlHeight, controlPaddingX, focusRing, iconSize } from '../lib/styles' import { Field } from './Field' import { Chevron, Spinner } from './SelectParts' import { FIELD_BOX, groupOptions, type SelectOption, type SelectSize, type SelectVariant } from './select.tokens' export type NativeSelectProps = Omit< ComponentProps<'select'>, 'size' | 'value' | 'defaultValue' | 'multiple' | 'className' | 'children' > & { /** Visible label, linked to the select. */ label?: ReactNode /** Help text under the select, announced with it. */ description?: ReactNode /** Error message. Marks the select invalid and replaces `description`. */ error?: ReactNode size?: SelectSize /** `outline` draws a bordered box; `filled` a tinted one. */ variant?: SelectVariant /** The choices. Options sharing a `group` become an ``. */ options: SelectOption[] /** A first, empty option shown while nothing is chosen. */ placeholder?: string /** The selected value (controlled). `''` selects the placeholder. */ value?: string /** The initial value (uncontrolled). */ defaultValue?: string /** Fires with the new value. Native `onChange` still works too. */ onValueChange?: (value: string) => void /** Disables the select and shows a spinner while options load. */ loading?: boolean /** Icon before the value. */ startIcon?: ReactNode /** Classes for the outer field wrapper. Use `selectClassName` for the select itself. */ className?: string selectClassName?: string } const START_ICON_PAD: Record = { sm: 'pl-8', md: 'pl-9', lg: 'pl-11' } const END_PAD: Record = { sm: 'pr-8', md: 'pr-9', lg: 'pr-11' } const ICON_LEFT: Record = { sm: 'left-2.5', md: 'left-3', lg: 'left-4' } const ICON_RIGHT: Record = { sm: 'right-2.5', md: 'right-3', lg: 'right-4' } /** * A styled native ` {placeholder != null && ( )} {groupOptions(options).map((group) => { const items = group.items.map((option) => ( )) return group.label === null ? ( items ) : ( {items} ) })} )} className={cn( 'w-full cursor-pointer appearance-none truncate', controlHeight[size], controlPaddingX[size], FIELD_BOX[variant], focusRing, END_PAD[size], startIcon != null && START_ICON_PAD[size], 'has-[option[value=""]:checked]:text-fg-muted [&_optgroup]:bg-surface [&_optgroup]:text-fg-muted [&_option]:bg-surface [&_option]:text-fg', 'disabled:cursor-not-allowed disabled:opacity-55', selectClassName, )} {...(props as ComponentProps)} /> ) }