mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-10-06 11:44:08 +00:00
109 lines
2.8 KiB
TypeScript
109 lines
2.8 KiB
TypeScript
"use client";
|
|
|
|
import { Icon } from "@iconify/react";
|
|
import { Input } from "@nextui-org/react";
|
|
import React, { useState } from "react";
|
|
import { Control, FieldPath, FieldValues } from "react-hook-form";
|
|
|
|
import { FormControl, FormField, FormMessage } from "@/components/ui/form";
|
|
|
|
interface CustomInputProps<T extends FieldValues> {
|
|
control: Control<T>;
|
|
name: FieldPath<T>;
|
|
label?: string;
|
|
labelPlacement?: "inside" | "outside";
|
|
variant?: "flat" | "bordered" | "underlined" | "faded";
|
|
type?: string;
|
|
placeholder?: string;
|
|
password?: boolean;
|
|
confirmPassword?: boolean;
|
|
isRequired?: boolean;
|
|
isInvalid?: boolean;
|
|
}
|
|
|
|
export const CustomInput = <T extends FieldValues>({
|
|
control,
|
|
name,
|
|
type = "text",
|
|
label = name,
|
|
labelPlacement = "inside",
|
|
placeholder,
|
|
variant = "bordered",
|
|
confirmPassword = false,
|
|
password = false,
|
|
isRequired = true,
|
|
isInvalid,
|
|
}: CustomInputProps<T>) => {
|
|
const [isPasswordVisible, setIsPasswordVisible] = useState(false);
|
|
const [isConfirmPasswordVisible, setIsConfirmPasswordVisible] =
|
|
useState(false);
|
|
|
|
const inputLabel = confirmPassword
|
|
? "Confirm Password"
|
|
: password
|
|
? "Password"
|
|
: label;
|
|
|
|
const inputPlaceholder = confirmPassword
|
|
? "Confirm Password"
|
|
: password
|
|
? "Password"
|
|
: placeholder;
|
|
|
|
const inputType =
|
|
password || confirmPassword
|
|
? isPasswordVisible || isConfirmPasswordVisible
|
|
? "text"
|
|
: "password"
|
|
: type;
|
|
const inputIsRequired = password || confirmPassword ? true : isRequired;
|
|
|
|
const toggleVisibility = () => {
|
|
if (password) {
|
|
setIsPasswordVisible(!isPasswordVisible);
|
|
} else if (confirmPassword) {
|
|
setIsConfirmPasswordVisible(!isConfirmPasswordVisible);
|
|
}
|
|
};
|
|
|
|
const endContent = (password || confirmPassword) && (
|
|
<button type="button" onClick={toggleVisibility}>
|
|
<Icon
|
|
className="pointer-events-none text-2xl text-default-400"
|
|
icon={
|
|
(password && isPasswordVisible) ||
|
|
(confirmPassword && isConfirmPasswordVisible)
|
|
? "solar:eye-closed-linear"
|
|
: "solar:eye-bold"
|
|
}
|
|
/>
|
|
</button>
|
|
);
|
|
|
|
return (
|
|
<FormField
|
|
control={control}
|
|
name={name}
|
|
render={({ field }) => (
|
|
<>
|
|
<FormControl>
|
|
<Input
|
|
id={name}
|
|
isRequired={inputIsRequired}
|
|
label={inputLabel}
|
|
labelPlacement={labelPlacement}
|
|
placeholder={inputPlaceholder}
|
|
type={inputType}
|
|
variant={variant}
|
|
isInvalid={isInvalid}
|
|
endContent={endContent}
|
|
{...field}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage className="text-system-error dark:text-system-error" />
|
|
</>
|
|
)}
|
|
/>
|
|
);
|
|
};
|