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

63 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

// * Packages
import { usePage } from "@inertiajs/react";
// * Components shadcn
import { Switch } from "@/components/ui/switch";
2026-02-12 15:42:22 +01:00
import { Label } from "@/components/ui/label";
// * Other
import InputError from "@/back/Components/InputError";
2025-09-16 16:44:49 +02:00
import { cn } from "@/lib/utils";
type SimpleSwitchProps = {
name: string;
checked?: boolean;
label?: string;
2025-11-19 16:16:34 +01:00
leftHelper?: string;
rightHelper?: string;
className?: string;
required?: boolean;
disabled?: boolean;
2026-02-10 15:52:48 +01:00
parentErrors?: Record<string, string> | null;
// eslint-disable-next-line no-unused-vars
setData: (name: string, value: boolean) => void;
};
export default function SimpleSwitch({
name,
checked,
2025-11-19 16:16:34 +01:00
label,
leftHelper,
rightHelper,
className = "",
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}`}>
2026-02-10 15:52:48 +01:00
{label && (
<Label htmlFor={name}>{label + (required ? " *" : "")}</Label>
)}
2025-11-19 16:16:34 +01:00
<div className="flex items-center gap-3">
2026-02-10 15:52:48 +01:00
{leftHelper && <small>{leftHelper}</small>}
2025-11-19 16:16:34 +01:00
<Switch
id={name}
2025-11-19 16:16:34 +01:00
name={name}
onCheckedChange={(value) => setData(name, value)}
required={required}
checked={checked}
disabled={disabled}
2026-02-10 15:52:48 +01:00
className={cn({
"border-destructive/50": localErrors[name],
})}
2025-11-19 16:16:34 +01:00
/>
2026-02-10 15:52:48 +01:00
{rightHelper && <small>{rightHelper}</small>}
2025-11-19 16:16:34 +01:00
</div>
<InputError message={localErrors[name]} />
</div>
);
}