81 lines
2 KiB
TypeScript
81 lines
2 KiB
TypeScript
import { usePage } from "@inertiajs/react";
|
|
import { type ReactNode } from "react";
|
|
import type React from "react";
|
|
|
|
import { Label } from "@/components/ui/label";
|
|
import { Switch } from "@/components/ui/switch";
|
|
import InputError from "@/front/Components/cookie/InputError";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
interface CookieSwitchProps {
|
|
name: string;
|
|
checked?: boolean;
|
|
description?: string | React.ReactNode;
|
|
label?: ReactNode | string;
|
|
className?: string;
|
|
inputClassName?: string;
|
|
required?: boolean;
|
|
disabled?: boolean;
|
|
parentErrors?: Record<string, string> | null;
|
|
setData: (name: string, value: boolean) => void;
|
|
labelClassName?: string;
|
|
descClassName?: string;
|
|
title?: string;
|
|
}
|
|
|
|
export default function CookieSwitch({
|
|
name,
|
|
checked,
|
|
description,
|
|
label,
|
|
className = "",
|
|
inputClassName = "",
|
|
required = false,
|
|
disabled = false,
|
|
parentErrors = null,
|
|
descClassName = "text-xs",
|
|
labelClassName = "text-sm",
|
|
title = name,
|
|
setData,
|
|
}: CookieSwitchProps) {
|
|
const { errors } = usePage().props;
|
|
const localErrors = parentErrors ?? errors;
|
|
|
|
return (
|
|
<div className={cn("flex flex-col items-start w-fit gap-1.5", className)}>
|
|
<div>
|
|
{label && (
|
|
<Label
|
|
htmlFor={name}
|
|
className={cn("flex items-center gap-1.5", labelClassName)}
|
|
>
|
|
{label}
|
|
{required && " *"}
|
|
</Label>
|
|
)}
|
|
{description && (
|
|
<p className={cn("text-muted-foreground", descClassName)}>
|
|
{description}
|
|
</p>
|
|
)}
|
|
</div>
|
|
<div className="flex items-center gap-3">
|
|
<Switch
|
|
title={title}
|
|
id={name}
|
|
name={name}
|
|
onCheckedChange={(value) => {
|
|
setData(name, value);
|
|
}}
|
|
required={required}
|
|
checked={checked}
|
|
disabled={disabled}
|
|
className={cn(inputClassName, {
|
|
"border-destructive/50": localErrors[name],
|
|
})}
|
|
/>
|
|
</div>
|
|
<InputError message={localErrors[name]} />
|
|
</div>
|
|
);
|
|
}
|