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

88 lines
2.8 KiB
TypeScript

// * Packages
import { Eye, EyeOff } from "lucide-react";
import { useState } from "react";
// * Components shadcn
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Button } from "@/components/ui/button";
// * Other
import InputError from "@/back/Components/InputError";
import WordCounter from "@/back/Components/WordCounter";
import { cn } from "@/lib/utils";
import { usePage } from "@inertiajs/react";
import trans from "@/hooks/use-translations";
export default function SimpleInput({
className,
value = "",
name,
label,
id = name,
type = "text",
placeholder = trans("back.enter_text_here"),
wordCounter = true,
required = true,
disabled = false,
checkVisibility = false,
setData,
}: {
className?: string;
name: string;
label: string;
value?: string;
id?: string;
type?: string;
placeholder: string;
wordCounter?: boolean;
required?: boolean;
disabled?: boolean;
checkVisibility?: boolean;
setData: Function;
}) {
const [isVisible, setIsVisible] = useState(checkVisibility);
const errors = usePage().props.errors;
return (
<div
className={cn(
"flex flex-col w-full items-start gap-1.5",
className
)}
>
<Label htmlFor={name}>{label}</Label>
{wordCounter && <WordCounter value={value} />}
<div className="flex w-full">
<Input
id={id}
name={name}
type={isVisible ? "text" : type}
placeholder={placeholder}
defaultValue={value}
onChange={(e) => {
setData(name, e.target.value);
}}
required={required}
disabled={disabled}
className={`${
type === "password" || checkVisibility
? "w-[calc(100%-40px-0.25rem)]"
: "w-full"
} ${errors[name] ? "border-destructive/50" : ""}`}
/>
{(type === "password" || checkVisibility) && (
<Button
className="ms-1"
type="button"
size="icon"
onClick={() => {
setIsVisible(!isVisible);
}}
>
<EyeOff className={!isVisible ? "hidden " : ""} />
<Eye className={isVisible ? "hidden" : ""} />
</Button>
)}
</div>
<InputError message={errors[name]} />
</div>
);
}