feat: form hook
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { cn } from "@/utils/class-merge";
|
||||
import { FunctionComponent, Ref } from "preact";
|
||||
import { forwardRef, HTMLProps, useEffect } from "preact/compat";
|
||||
import { tv } from "tailwind-variants";
|
||||
import classes from "./Input.module.scss";
|
||||
|
||||
@@ -20,29 +20,19 @@ const input = tv({
|
||||
},
|
||||
});
|
||||
|
||||
interface InputProps {
|
||||
isPassword?: boolean;
|
||||
placeholder?: string;
|
||||
interface InputProps extends HTMLProps<HTMLInputElement> {
|
||||
textAlign?: "center" | "left";
|
||||
error?: string;
|
||||
textRef?: Ref<HTMLInputElement> | null;
|
||||
}
|
||||
|
||||
const Input: FunctionComponent<InputProps> = ({
|
||||
isPassword = false,
|
||||
placeholder = "",
|
||||
textAlign,
|
||||
error = "",
|
||||
textRef = null,
|
||||
}: InputProps) => {
|
||||
const Input = forwardRef<HTMLInputElement, InputProps>((props, ref) => {
|
||||
const { textAlign, error, type = "text", ...rest } = props;
|
||||
useEffect(() => {
|
||||
console.log(`error: ${error}`);
|
||||
}, [error]);
|
||||
return (
|
||||
<div class="flex w-full flex-col items-center gap-1">
|
||||
<input
|
||||
type={isPassword ? "password" : "text"}
|
||||
class={input({ "text-align": textAlign, "border-error": error !== "" })}
|
||||
placeholder={placeholder}
|
||||
ref={textRef}
|
||||
/>
|
||||
<input class={input({ "text-align": textAlign, "border-error": !!error })} ref={ref} type={type} {...rest} />
|
||||
<p
|
||||
class={cn("invisible h-10 w-[80%] text-center text-[0.7rem] break-words text-red-500", {
|
||||
visible: error !== "",
|
||||
@@ -52,6 +42,8 @@ const Input: FunctionComponent<InputProps> = ({
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
});
|
||||
|
||||
Input.displayName = "AHInput";
|
||||
|
||||
export default Input;
|
||||
|
||||
Reference in New Issue
Block a user