ldc-groupe/resources/ts/back/Components/Inputs/SimpleSelect.tsx

69 lines
2 KiB
TypeScript

// * Packages
import { usePage } from "@inertiajs/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";
type SimpleSelectProps = {
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 SimpleSelect({
name,
label,
placeholder = "ex: administrateur",
options = {},
selected,
required = true,
disabled = false,
setData,
}: SimpleSelectProps) {
const { errors } = usePage().props;
return (
<div className="flex flex-col w-full justify-between items-start gap-1.5">
<Label htmlFor={name}>{label}</Label>
<Select
name={name}
required={required}
disabled={disabled}
value={selected}
onValueChange={(value) => setData(name, value)}
>
<SelectTrigger
className={`${
!selected ? "text-muted-foreground" : ""
} w-full`}
>
<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>
);
}