import React from 'react'; import { Combobox } from '@base-ui/react/combobox'; import { Check, ChevronDown } from 'lucide-react'; import { cn } from '@/lib/cn'; import { isNumber, isString } from '@/lib/objects'; export type SelectValue = string | number; export type SelectOption = { value: SelectValue; label: React.ReactNode; textLabel?: string; disabled?: boolean; }; export interface SelectGroup { id?: SelectValue; label: string; options: SelectOption[]; } interface BaseSelectProps { id?: string; options?: SelectOption[]; groups?: SelectGroup[]; disabled?: boolean; placeholder?: string; searchPlaceholder?: string; emptyMessage?: string; hasError?: boolean; 'aria-describedby'?: string; 'aria-invalid'?: boolean; onBlur?: React.FocusEventHandler; /** Hands the typed query to the caller and turns off client-side filtering. */ onSearchChange?: (value: string) => void; } interface SingleSelectProps extends BaseSelectProps { multiple?: false; value: SelectValue | null | undefined; onChange: (value: SelectValue) => void; } interface MultiSelectProps extends BaseSelectProps { multiple: true; value: SelectValue[]; onChange: (value: SelectValue[]) => void; } export type SelectProps = SingleSelectProps | MultiSelectProps; type ComboboxGroupItem = { value: string; label: string; items: SelectOption[]; }; type WithStringClassName = Omit & { className?: string }; const inputGroupClass = [ 'relative flex items-center w-full rounded-sm border text-sm transition-colors duration-150', 'bg-input border-input text-foreground hover:border-input data-[popup-open]:border-input', 'focus-within:shadow-md focus-within:border-primary focus-within:ring-2 focus-within:ring-ring/50', 'data-[disabled]:opacity-75 data-[disabled]:cursor-default', ].join(' '); const inputErrorClass = 'border-destructive text-destructive focus-within:border-destructive focus-within:ring-destructive'; const inputClass = [ 'w-full min-w-0 appearance-none border-0 bg-transparent p-3 pr-10 text-sm text-foreground outline-hidden', 'placeholder:text-muted-foreground focus:ring-0 focus:outline-hidden disabled:cursor-default', ].join(' '); const triggerClass = 'absolute right-0 top-0 flex h-full w-10 items-center justify-center border-0 bg-transparent p-0 text-muted-foreground [&_svg]:h-4 [&_svg]:w-4 [&_svg]:shrink-0'; const positionerClass = 'z-[99999] min-w-[var(--anchor-width)] outline-none'; const popupClass = [ 'bg-card border border-border rounded-sm shadow-lg py-1 text-sm text-foreground', 'w-[var(--anchor-width)] max-w-[var(--available-width)] max-h-[min(18rem,var(--available-height))]', ].join(' '); const listClass = 'max-h-72 overflow-y-auto overscroll-contain outline-none'; const itemClass = 'flex items-center justify-between px-3 py-2 cursor-pointer select-none outline-hidden data-[highlighted]:bg-popover data-[highlighted]:text-foreground data-[selected]:text-accent data-[disabled]:opacity-50 data-[disabled]:cursor-default'; const emptyClass = 'px-3 py-2 text-sm text-muted-foreground empty:p-0'; const groupLabelClass = 'px-3 pt-2 pb-1 text-xs uppercase text-muted-foreground empty:hidden'; const selectionSummaryClass = 'pointer-events-none whitespace-nowrap px-3 py-3 text-sm text-foreground'; const selectionPreviewClass = 'pointer-events-none flex min-w-0 flex-1 items-center px-3 py-2 pr-10 text-sm'; const optionLabel = (option: SelectOption | null | undefined): string => { if (!option) { return ''; } if (option.textLabel !== undefined) { return option.textLabel; } if (isString(option.label) || isNumber(option.label)) { return String(option.label); } return String(option.value); }; const isTextLabel = (label: React.ReactNode): boolean => isString(label) || isNumber(label); const sameValue = (left: SelectValue, right: SelectValue): boolean => Object.is(left, right); const isSameOption = (option: SelectOption, selected: SelectOption): boolean => sameValue(option.value, selected.value); const optionKey = (option: Pick): string => `${isNumber(option.value) ? 'number' : 'string'}:${String(option.value)}`; const getOptions = (options: SelectOption[] | undefined, groups: SelectGroup[] | undefined): SelectOption[] => options ?? groups?.flatMap((group) => group.options) ?? []; const getGroups = (groups: SelectGroup[] | undefined): ComboboxGroupItem[] | undefined => groups?.map((group, index) => ({ value: group.id !== undefined ? optionKey({ value: group.id }) : `${index}:${group.label}`, label: group.label, items: group.options, })); const ComboboxInputGroup = ({ $hasError, className, ...props }: { $hasError?: boolean } & WithStringClassName>) => ( ); const ComboboxInput = ({ className, ...props }: WithStringClassName>) => ( ); const ComboboxTrigger = ({ className, ...props }: WithStringClassName>) => ( ); const ComboboxPositioner = ({ className, ...props }: WithStringClassName>) => ( ); const ComboboxPopup = ({ className, ...props }: WithStringClassName>) => ( ); const ComboboxList = ({ className, ...props }: WithStringClassName>) => ( ); const ComboboxItem = ({ className, ...props }: WithStringClassName>) => ( ); const ComboboxEmpty = ({ className, ...props }: WithStringClassName>) => ( ); const ComboboxGroupLabel = ({ className, ...props }: WithStringClassName>) => ( ); const ComboboxSelectionSummary = ({ className, ...props }: React.ComponentProps<'span'>) => ( ); const CheckIcon = () => ; const ChevronIcon = () => ; const ComboboxOptionItem = ({ option, showIndicator }: { option: SelectOption; showIndicator: boolean }) => ( {option.label} {showIndicator && ( )} ); const ComboboxOptionsList = ({ groups, showIndicator, }: { groups: ComboboxGroupItem[] | undefined; showIndicator: boolean; }) => groups ? ( {(group: ComboboxGroupItem) => ( {group.label} {(option: SelectOption) => ( )} )} ) : ( {(option: SelectOption) => ( )} ); const ComboboxPopupContent = ({ groupedItems, emptyMessage, showIndicator, }: { groupedItems: ComboboxGroupItem[] | undefined; emptyMessage: string; showIndicator: boolean; }) => ( {emptyMessage} ); const findOption = (candidates: SelectOption[], value: SelectValue): SelectOption | undefined => candidates.find((option) => sameValue(option.value, value)); const resolveSelectedOptions = ( values: SelectValue[], flatOptions: SelectOption[], fallback: SelectOption[] ): SelectOption[] => values.flatMap((value) => { const option = findOption(flatOptions, value) ?? findOption(fallback, value); return option ? [option] : []; }); const selectionSignature = (selected: SelectOption[]): string => selected.map((option) => `${optionKey(option)}:${optionLabel(option)}`).join('\n'); // Keeps the label of a selected value that is no longer listed, e.g. after a server-side search. function useSelectedOptions(flatOptions: SelectOption[], values: SelectValue[]): SelectOption[] { const [remembered, setRemembered] = React.useState(() => resolveSelectedOptions(values, flatOptions, [])); const selected = resolveSelectedOptions(values, flatOptions, remembered); if (selectionSignature(selected) !== selectionSignature(remembered)) { setRemembered(selected); } return selected; } const resolveRichSelection = (selectedOption: SelectOption | null): React.ReactNode | null => selectedOption && !isTextLabel(selectedOption.label) ? selectedOption.label : null; function useSelectSearch({ onBlur, onSearchChange, }: { onBlur?: React.FocusEventHandler; onSearchChange?: (value: string) => void; }) { const [searchValue, setSearchValue] = React.useState(''); const [inputFocused, setInputFocused] = React.useState(false); const resetInputValue = React.useCallback(() => { setSearchValue(''); onSearchChange?.(''); }, [onSearchChange]); const handleInputValueChange = React.useCallback( (value: string, eventDetails: { reason?: string }) => { if (eventDetails.reason === 'focus-out' || eventDetails.reason === 'input-blur' || !inputFocused) { resetInputValue(); return; } setSearchValue(value); onSearchChange?.(value); }, [inputFocused, onSearchChange, resetInputValue] ); const handleFocus: React.FocusEventHandler = React.useCallback(() => { setInputFocused(true); }, []); const handleBlur: React.FocusEventHandler = React.useCallback( (event) => { setInputFocused(false); resetInputValue(); onBlur?.(event); }, [onBlur, resetInputValue] ); return { searchValue, setSearchValue, inputFocused, resetInputValue, handleInputValueChange, handleFocus, handleBlur, externalFilter: onSearchChange ? null : undefined, }; } type SelectSearch = ReturnType; type InputAriaProps = Pick, 'aria-describedby' | 'aria-invalid'>; function MultiSelectCombobox({ id, placeholder, searchPlaceholder, emptyMessage, hasError, disabled, items, groupedItems, selectedOptions, inputValue, inputAria, search, onChange, }: { id: string | undefined; placeholder: string | undefined; searchPlaceholder: string; emptyMessage: string; hasError: boolean | undefined; disabled: boolean | undefined; items: SelectOption[] | ComboboxGroupItem[]; groupedItems: ComboboxGroupItem[] | undefined; selectedOptions: SelectOption[]; inputValue: string; inputAria: InputAriaProps; search: SelectSearch; onChange: (value: SelectValue[]) => void; }) { return ( multiple items={items} value={selectedOptions} inputValue={inputValue} onInputValueChange={search.handleInputValueChange} onOpenChange={(open) => { if (!open) { search.resetInputValue(); } }} onValueChange={(value) => { onChange(value.map((option) => option.value)); search.resetInputValue(); }} filter={search.externalFilter} disabled={disabled} itemToStringLabel={optionLabel} itemToStringValue={(option) => String(option?.value ?? '')} isItemEqualToValue={isSameOption} autoHighlight > {(value: SelectOption[]) => ( <> {value.length > 0 && ( {`${value.length} selected`} )} 0 ? 'pl-0' : undefined} placeholder={value.length > 0 ? searchPlaceholder : placeholder} onFocus={search.handleFocus} onBlur={search.handleBlur} /> )} ); } function SingleSelectCombobox({ id, placeholder, emptyMessage, hasError, disabled, items, groupedItems, selectedOption, inputValue, richSelection, inputFocused, inputAria, search, onChange, onSelectLabel, }: { id: string | undefined; placeholder: string | undefined; emptyMessage: string; hasError: boolean | undefined; disabled: boolean | undefined; items: SelectOption[] | ComboboxGroupItem[]; groupedItems: ComboboxGroupItem[] | undefined; selectedOption: SelectOption | null; inputValue: string; richSelection: React.ReactNode | null; inputFocused: boolean; inputAria: InputAriaProps; search: SelectSearch; onChange: (value: SelectValue) => void; onSelectLabel: (label: string) => void; }) { return ( items={items} value={selectedOption} inputValue={inputValue} onInputValueChange={search.handleInputValueChange} onOpenChange={(open) => { if (!open) { search.resetInputValue(); } }} onValueChange={(option) => { if (option) { onChange(option.value); onSelectLabel(optionLabel(option)); } }} filter={search.externalFilter} disabled={disabled} itemToStringLabel={optionLabel} itemToStringValue={(option) => String(option?.value ?? '')} isItemEqualToValue={isSameOption} autoHighlight > {richSelection && ( {richSelection} )} ); } export default function Select(props: SelectProps) { const { id, options, groups, disabled, placeholder, searchPlaceholder = 'Search...', emptyMessage = 'No results found.', hasError, onBlur, onSearchChange, } = props; const flatOptions = getOptions(options, groups); const groupedItems = getGroups(groups); const items = groupedItems ?? flatOptions; const search = useSelectSearch({ onBlur, onSearchChange }); const selectedValues = props.multiple ? props.value : props.value === null || props.value === undefined ? [] : [props.value]; const selectedOptions = useSelectedOptions(flatOptions, selectedValues); const inputAria: InputAriaProps = { 'aria-describedby': props['aria-describedby'], 'aria-invalid': props['aria-invalid'], }; if (props.multiple) { const inputValue = search.inputFocused ? search.searchValue : ''; return ( ); } const selectedOption = selectedOptions[0] ?? null; const selectedLabel = optionLabel(selectedOption); const richSelection = resolveRichSelection(selectedOption); const inputValue = search.inputFocused ? search.searchValue : selectedLabel; return ( ); }