Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 2x 41x 2x 54x 54x 54x 54x 54x 54x 150x 54x 54x 39x 1x 1x 39x 39x 54x 7x 7x 54x 3x 3x 54x 21x 3x 2x 41x 4x 4x 9x 37x | import { useState, useRef, useEffect, useId } from 'react';
import { ChevronDown } from 'lucide-react';
import type { Control, FieldError } from 'react-hook-form';
import { Controller } from 'react-hook-form';
import { cn } from '@/lib/utils';
import { Text } from '@/components/text';
interface SelectOption {
value: string;
label: string;
}
// Base props for standalone usage
interface BaseSelectProps {
id?: string;
label?: string;
value?: string;
placeholder?: string;
options: SelectOption[];
onValueChange: (value: string) => void;
className?: string;
required?: boolean;
disabled?: boolean;
error?: string;
}
// Enhanced props for form usage
interface FormSelectProps
extends Omit<BaseSelectProps, 'value' | 'onValueChange' | 'error'> {
name: string;
control: Control<any>;
error?: FieldError;
}
// Union type for both usage patterns
type SelectProps = BaseSelectProps | FormSelectProps;
// Type guard to check if it's form usage
const isFormSelect = (props: SelectProps): props is FormSelectProps => {
return 'name' in props && 'control' in props;
};
// Internal Select component for actual rendering
const InternalSelect = ({
id,
label,
value,
placeholder = 'Select an option',
options,
onValueChange,
className,
required = false,
disabled = false,
error,
}: BaseSelectProps) => {
const [isOpen, setIsOpen] = useState(false);
const selectRef = useRef<HTMLDivElement>(null);
const dropdownRef = useRef<HTMLDivElement>(null);
// Generate unique IDs for accessibility
const generatedId = useId();
const selectId = id || generatedId;
const dropdownId = `${selectId}-dropdown`;
const selectedOption = options.find(option => option.value === value);
const displayValue = selectedOption ? selectedOption.label : placeholder;
// Close dropdown when clicking outside
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
Eif (
selectRef.current &&
!selectRef.current.contains(event.target as Node)
) {
setIsOpen(false);
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => document.removeEventListener('mousedown', handleClickOutside);
}, []);
const handleToggle = () => {
Eif (!disabled) {
setIsOpen(!isOpen);
}
};
const handleOptionSelect = (optionValue: string) => {
onValueChange(optionValue);
setIsOpen(false);
};
return (
<div className={cn('space-y-2 relative', className)} ref={selectRef}>
{label && (
<label htmlFor={selectId} className="text-sm font-medium text-default">
{label}
{required && <span className="text-red-500 ml-1">*</span>}
</label>
)}
<div className="relative">
<button
id={selectId}
type="button"
disabled={disabled}
onClick={handleToggle}
className={cn(
'w-full h-[50px] px-4 py-2 text-left border rounded-full',
'border-[#F0F0F0] text-default',
'disabled:bg-gray-100 disabled:cursor-not-allowed',
'flex items-center justify-between',
isOpen && 'ring-2 ring-blue-500 border-blue-500',
error && 'border-red-500'
)}
aria-expanded={isOpen}
aria-haspopup="listbox"
aria-controls={dropdownId}
role="combobox"
>
<span className={cn('truncate', !selectedOption && 'text-default')}>
{displayValue}
</span>
<ChevronDown
className={cn(
'h-4 w-4 transition-transform duration-200',
isOpen && 'rotate-180'
)}
/>
</button>
{/* Dropdown Portal */}
{isOpen && (
<div
id={dropdownId}
ref={dropdownRef}
className="absolute z-9999 bg-background border border-gray-200 shadow-lg max-h-60 overflow-y-auto top-full left-0 w-full mt-1"
role="listbox"
>
{options.map(option => (
<button
key={option.value}
type="button"
onClick={() => handleOptionSelect(option.value)}
className={cn(
'w-full px-4 py-3 text-left hover:bg-gray-100 hover:text-default',
'focus:outline-none focus:bg-gray-100',
'border-b border-gray-100 last:border-b-0',
value === option.value && 'bg-blue-50 text-blue-600'
)}
role="option"
aria-selected={value === option.value}
>
{option.label}
</button>
))}
</div>
)}
</div>
{error && (
<Text size="sm" variant="destructive" className="mt-1">
{error}
</Text>
)}
</div>
);
};
// Main Select component with smart Controller integration
export const Select = (props: SelectProps) => {
// For form usage with React Hook Form
if (isFormSelect(props)) {
const { name, control, error, ...selectProps } = props;
return (
<Controller
name={name}
control={control}
render={({ field, fieldState }) => (
<InternalSelect
{...selectProps}
value={field.value || ''}
onValueChange={field.onChange}
error={error?.message || fieldState.error?.message}
/>
)}
/>
);
}
// For standalone usage
return <InternalSelect {...props} />;
};
|