2024-06-06 11:05:54 +00:00
|
|
|
import { cn } from "@/lib/utils";
|
2024-07-03 15:15:52 +00:00
|
|
|
import { FormControl, FormDescription, FormField, FormItem, Input, InputProps } from "@/ui";
|
2024-06-06 11:05:54 +00:00
|
|
|
|
|
|
|
|
import * as React from "react";
|
|
|
|
|
import { createElement } from "react";
|
2024-07-03 15:15:52 +00:00
|
|
|
import { FieldPath, FieldValues, UseControllerProps, useFormContext } from "react-hook-form";
|
|
|
|
|
import { FormErrorMessage } from "./FormErrorMessage";
|
2024-06-06 11:05:54 +00:00
|
|
|
import { FormLabel, FormLabelProps } from "./FormLabel";
|
|
|
|
|
import { FormInputProps, FormInputWithIconProps } from "./FormProps";
|
|
|
|
|
|
|
|
|
|
export type FormTextFieldProps<
|
|
|
|
|
TFieldValues extends FieldValues = FieldValues,
|
2024-06-17 10:42:44 +00:00
|
|
|
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
|
2024-06-06 11:05:54 +00:00
|
|
|
> = {
|
2024-06-17 10:42:44 +00:00
|
|
|
button?: (props?: React.PropsWithChildren) => React.ReactNode;
|
2024-06-06 11:05:54 +00:00
|
|
|
} & InputProps &
|
|
|
|
|
FormInputProps &
|
|
|
|
|
Partial<FormLabelProps> &
|
|
|
|
|
FormInputWithIconProps &
|
2024-07-03 15:15:52 +00:00
|
|
|
UseControllerProps<TFieldValues, TName>;
|
2024-06-06 11:05:54 +00:00
|
|
|
|
2024-06-17 10:42:44 +00:00
|
|
|
export const FormTextField = React.forwardRef<
|
|
|
|
|
HTMLDivElement,
|
|
|
|
|
React.HTMLAttributes<HTMLDivElement> & FormTextFieldProps
|
|
|
|
|
>((props, ref) => {
|
|
|
|
|
const {
|
2024-07-03 15:15:52 +00:00
|
|
|
name,
|
2024-06-17 10:42:44 +00:00
|
|
|
label,
|
|
|
|
|
hint,
|
2024-07-03 15:15:52 +00:00
|
|
|
placeholder,
|
2024-06-17 10:42:44 +00:00
|
|
|
description,
|
2024-07-03 15:15:52 +00:00
|
|
|
|
2024-06-17 10:42:44 +00:00
|
|
|
required,
|
|
|
|
|
className,
|
|
|
|
|
leadIcon,
|
|
|
|
|
trailIcon,
|
|
|
|
|
button,
|
2024-07-03 15:15:52 +00:00
|
|
|
|
2024-06-17 10:42:44 +00:00
|
|
|
type,
|
|
|
|
|
} = props;
|
2024-06-06 11:05:54 +00:00
|
|
|
|
2024-06-29 19:39:25 +00:00
|
|
|
const { control } = useFormContext();
|
|
|
|
|
|
2024-06-06 11:05:54 +00:00
|
|
|
return (
|
|
|
|
|
<FormField
|
|
|
|
|
control={control}
|
|
|
|
|
name={name}
|
|
|
|
|
rules={{ required }}
|
2024-06-17 10:42:44 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2024-07-03 15:15:52 +00:00
|
|
|
render={({ field, fieldState, formState }) => {
|
|
|
|
|
return (
|
|
|
|
|
<FormItem ref={ref} className={cn(className, "space-y-3")}>
|
|
|
|
|
{label && <FormLabel label={label} hint={hint} required={required} />}
|
|
|
|
|
<div className={cn(button ? "flex" : null)}>
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
leadIcon ? "relative flex items-stretch flex-grow focus-within:z-10" : ""
|
|
|
|
|
)}
|
2024-06-06 11:05:54 +00:00
|
|
|
>
|
2024-07-03 15:15:52 +00:00
|
|
|
{leadIcon && (
|
|
|
|
|
<div className='absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none'>
|
|
|
|
|
{React.createElement(
|
|
|
|
|
leadIcon,
|
|
|
|
|
{
|
|
|
|
|
className: "h-5 w-5 text-muted-foreground",
|
|
|
|
|
"aria-hidden": true,
|
|
|
|
|
},
|
|
|
|
|
null
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-06-06 11:05:54 +00:00
|
|
|
|
2024-07-03 15:15:52 +00:00
|
|
|
<FormControl
|
|
|
|
|
className={cn("block", leadIcon ? "pl-10" : "", trailIcon ? "pr-10" : "")}
|
|
|
|
|
>
|
|
|
|
|
<Input
|
|
|
|
|
type={type}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
className={cn(
|
|
|
|
|
fieldState.error ? "border-destructive focus-visible:ring-destructive" : ""
|
|
|
|
|
)}
|
|
|
|
|
{...field}
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
|
|
|
|
|
{trailIcon && (
|
|
|
|
|
<div className='absolute inset-y-0 right-0 flex items-center pl-3 pointer-events-none'>
|
|
|
|
|
{createElement(
|
|
|
|
|
trailIcon,
|
|
|
|
|
{
|
|
|
|
|
className: "h-5 w-5 text-muted-foreground",
|
|
|
|
|
"aria-hidden": true,
|
|
|
|
|
},
|
|
|
|
|
null
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
{button && <>{createElement(button)}</>}
|
2024-06-06 11:05:54 +00:00
|
|
|
</div>
|
2024-07-03 15:15:52 +00:00
|
|
|
|
|
|
|
|
{description && <FormDescription>{description}</FormDescription>}
|
|
|
|
|
<FormErrorMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
);
|
|
|
|
|
}}
|
2024-06-06 11:05:54 +00:00
|
|
|
/>
|
|
|
|
|
);
|
2024-06-17 10:42:44 +00:00
|
|
|
});
|