ldc-groupe/resources/ts/back/Components/Inputs/EnumSelect.tsx
2025-10-17 14:52:51 +02:00

78 lines
2.4 KiB
TypeScript

// * Packages
import { usePage } from "@inertiajs/react";
import { useState } from "react";
// * Components shadcn
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
// * Other
import InputError from "@/back/Components/InputError";
import { cn } from "@/lib/utils";
type EnumSelectProps = {
name: string;
label: string;
placeholder?: string;
options?: Record<string, { label: string; name: string; value: number }>;
required?: boolean;
disabled?: boolean;
selected?: string;
// eslint-disable-next-line no-unused-vars
setData: (name: string, value: string) => void;
};
export default function EnumSelect({
name,
label,
placeholder = "ex: Administrateur",
options = {},
selected = "",
required = true,
disabled = false,
setData,
}: EnumSelectProps) {
const { errors } = usePage().props;
const [selectedValue, setSelected] = useState(selected);
const updateData = (name: string, value: string) => {
setSelected(value);
setData(name, value);
};
return (
<div className="flex flex-col w-full justify-start items-start gap-1.5">
<Label htmlFor={name}>{label + (required ? " *" : "")}</Label>
<Select
name={name}
required={required}
disabled={disabled}
value={selectedValue}
onValueChange={(value) => updateData(name, value)}
>
<SelectTrigger
className={cn(
"w-full",
{ "text-muted-foreground": !selectedValue },
{ "border-destructive/50": errors[name] },
)}
>
<SelectValue placeholder={placeholder} />
</SelectTrigger>
<SelectContent>
{Object.values(options).map((option) => (
<SelectItem
key={option.name}
value={String(option.value)}
>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
<InputError message={errors[name]} />
</div>
);
}