ldc-laravel-base-project/resources/ts/back/Components/Inputs/SimpleSelect.tsx
2025-03-05 16:50:28 +01:00

60 lines
1.9 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";
export default function SimpleSelect({
name,
label,
placeholder = "ex: administrateur",
options = {},
selected,
required = true,
disabled = false,
setData,
}: {
name: string;
label: string;
placeholder?: string;
options?: Record<string, { label: string; name: string; value: number }>;
required?: boolean;
disabled?: boolean;
selected?: string;
setData: Function;
}) {
const errors = usePage().props.errors;
return (
<div className="flex flex-col w-full 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, optionIndex) => (
<SelectItem
key={optionIndex}
value={String(option.value)}
>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
<InputError message={errors[name]} />
</div>
);
}