65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import { useState } from "react";
|
|
|
|
interface Option {
|
|
value: string;
|
|
label: string;
|
|
}
|
|
|
|
interface SelectProps {
|
|
options: Option[];
|
|
placeholder?: string;
|
|
onChange: (value: string) => void;
|
|
className?: string;
|
|
defaultValue?: string;
|
|
}
|
|
|
|
const Select: React.FC<SelectProps> = ({
|
|
options,
|
|
placeholder = "Select an option",
|
|
onChange,
|
|
className = "",
|
|
defaultValue = "",
|
|
}) => {
|
|
// Manage the selected value
|
|
const [selectedValue, setSelectedValue] = useState<string>(defaultValue);
|
|
|
|
const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
|
const value = e.target.value;
|
|
setSelectedValue(value);
|
|
onChange(value); // Trigger parent handler
|
|
};
|
|
|
|
return (
|
|
<select
|
|
className={`h-11 w-full appearance-none rounded-lg border border-gray-300 bg-transparent px-4 py-2.5 pr-11 text-sm shadow-theme-xs placeholder:text-gray-400 focus:border-brand-300 focus:outline-hidden focus:ring-3 focus:ring-brand-500/10 dark:border-gray-700 dark:bg-gray-900 dark:text-white/90 dark:placeholder:text-white/30 dark:focus:border-brand-800 ${
|
|
selectedValue
|
|
? "text-gray-800 dark:text-white/90"
|
|
: "text-gray-400 dark:text-gray-400"
|
|
} ${className}`}
|
|
value={selectedValue}
|
|
onChange={handleChange}
|
|
>
|
|
{/* Placeholder option */}
|
|
<option
|
|
value=""
|
|
disabled
|
|
className="text-gray-700 dark:bg-gray-900 dark:text-gray-400"
|
|
>
|
|
{placeholder}
|
|
</option>
|
|
{/* Map over options */}
|
|
{options.map((option) => (
|
|
<option
|
|
key={option.value}
|
|
value={option.value}
|
|
className="text-gray-700 dark:bg-gray-900 dark:text-gray-400"
|
|
>
|
|
{option.label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
);
|
|
};
|
|
|
|
export default Select;
|