ldc-groupe/resources/ts/back/Components/Inputs/SimpleSwitch.tsx
2025-12-15 15:50:46 +01:00

62 lines
1.8 KiB
TypeScript

// * Packages
import { usePage } from "@inertiajs/react";
// * Components shadcn
import { Switch } from "@/components/ui/switch";
// * Other
import InputError from "@/back/Components/InputError";
import { cn } from "@/lib/utils";
import { Label } from "@/components/ui/label";
type SimpleSwitchProps = {
name: string;
checked?: boolean;
label?: string;
leftHelper?: string;
rightHelper?: string;
className?: string|null;
required?: boolean;
disabled?: boolean;
parentErrors?: Record<string, string> | null,
// eslint-disable-next-line no-unused-vars
setData: (name: string, value: boolean) => void;
};
export default function SimpleSwitch({
name,
checked,
label,
leftHelper,
rightHelper,
className = null,
required = false,
disabled = false,
parentErrors = null,
setData,
}: SimpleSwitchProps) {
const { errors } = usePage().props;
const localErrors = parentErrors ?? errors;
return (
<div className={`flex flex-col items-start w-fit gap-1.5 ${className}`}>
{label && (<Label htmlFor={name}>{label + (required ? " *" : "")}</Label>)}
<div className="flex items-center gap-3">
{leftHelper && (
<small>{leftHelper}</small>
)}
<Switch
id={name}
name={name}
onCheckedChange={(value) => setData(name, value)}
required={required}
checked={checked}
disabled={disabled}
className={cn({ "border-destructive/50": localErrors[name] })}
/>
{rightHelper && (
<small>{rightHelper}</small>
)}
</div>
<InputError message={localErrors[name]} />
</div>
);
}