File size: 1,604 Bytes
80bcac8 ffd0276 80bcac8 ffd0276 80bcac8 ffd0276 80bcac8 | 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 | 'use client'
import React, { forwardRef, SelectHTMLAttributes, OptionHTMLAttributes } from 'react'
import { cn } from '@/utils/helpers'
export interface SelectProps extends SelectHTMLAttributes<HTMLSelectElement> {
label?: string
error?: string
options: { value: string; label: string }[]
placeholder?: string
}
export const Select = forwardRef<HTMLSelectElement, SelectProps>(
({ className, label, error, options, placeholder, id, ...props }, ref) => {
const selectId = id || label?.toLowerCase().replace(/\s+/g, '-')
return (
<div className="w-full">
{label && (
<label
htmlFor={selectId}
className="block text-sm font-medium text-current mb-1"
>
{label}
</label>
)}
<select
ref={ref}
id={selectId}
className={cn(
'clay-input',
error && 'border-red-500 focus:ring-red-500',
className
)}
aria-invalid={error ? 'true' : 'false'}
{...props}
>
{placeholder && (
<option value="" disabled>
{placeholder}
</option>
)}
{options.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
{error && (
<p id={`${selectId}-error`} className="mt-1 text-sm text-red-600 dark:text-red-400" role="alert">
{error}
</p>
)}
</div>
)
}
)
Select.displayName = 'Select' |