Files
OpenStock/components/forms/SelectField.tsx
2025-10-02 23:16:08 +05:30

43 lines
1.6 KiB
TypeScript

import React from 'react'
import {Label} from "@/components/ui/label";
import {Controller} from "react-hook-form";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
const SelectField = ({name, label, placeholder, options, control, error, required = false}: SelectFieldProps) => {
return (
<div className="space-y-2">
<Label htmlFor={name}>{label}</Label>
<Controller
name={name}
control={control}
rules={{
required: required ? `Please select ${label.toLowerCase()}`:false,
}}
render={({field}) => (
<Select value={field.value} onValueChange={field.onChange}>
<SelectTrigger className="select-trigger">
<SelectValue placeholder={placeholder} />
</SelectTrigger>
<SelectContent className="bg-gray-800 border-gray-600 text-white">
{options.map((option) => (
<SelectItem key={option.value} value={option.value} className="focus:bg-gray-600 focus: text-white">
{option.label}
</SelectItem>
))}
</SelectContent>
{error && <p className="text-red-600">{error.message}</p>}
</Select>
)}
/>
</div>
)
}
export default SelectField