This commit is contained in:
David Arranz 2024-06-17 12:42:44 +02:00
parent 82fdc6de13
commit f77b6adb99
2 changed files with 51 additions and 68 deletions

View File

@ -8,27 +8,15 @@ import {
FormMessage, FormMessage,
Textarea, Textarea,
} from "@/ui"; } from "@/ui";
import * as React from "react";
import { import { FieldErrors, FieldPath, FieldValues, UseControllerProps } from "react-hook-form";
FieldErrors,
FieldPath,
FieldValues,
UseControllerProps,
} from "react-hook-form";
import { FormLabel, FormLabelProps } from "./FormLabel"; import { FormLabel, FormLabelProps } from "./FormLabel";
export const FormTextAreaField = < export type FormTextAreaFieldProps<
TFieldValues extends FieldValues = FieldValues, TFieldValues extends FieldValues = FieldValues,
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
>({ > = {
label,
hint,
placeholder,
description,
autoSize,
className,
...props
}: {
autoSize?: boolean; autoSize?: boolean;
placeholder?: string; placeholder?: string;
description?: string; description?: string;
@ -36,27 +24,33 @@ export const FormTextAreaField = <
} & Partial<FormLabelProps> & } & Partial<FormLabelProps> &
UseControllerProps<TFieldValues, TName> & { UseControllerProps<TFieldValues, TName> & {
errors?: FieldErrors<TFieldValues>; errors?: FieldErrors<TFieldValues>;
}) => { };
export const FormTextAreaField = React.forwardRef<
HTMLDivElement,
React.TextareaHTMLAttributes<HTMLTextAreaElement> & FormTextAreaFieldProps
>(({ label, hint, placeholder, description, autoSize, className, ...props }, ref) => {
return ( return (
<FormField <FormField
control={props.control} control={props.control}
name={props.name} name={props.name}
render={({ field }) => ( // eslint-disable-next-line @typescript-eslint/no-unused-vars
<FormItem className={cn(className, "flex flex-col")}> render={({ field, fieldState, formState }) => (
<FormItem ref={ref} className={cn(className, "flex flex-col")}>
{label && <FormLabel label={label} hint={hint} />} {label && <FormLabel label={label} hint={hint} />}
<FormControl> <FormControl>
{autoSize ? ( {autoSize ? (
<AutosizeTextarea <AutosizeTextarea
disabled={props.disabled} disabled={props.disabled}
placeholder={placeholder} placeholder={placeholder}
className="resize-y" className='resize-y'
{...field} {...field}
/> />
) : ( ) : (
<Textarea <Textarea
disabled={props.disabled} disabled={props.disabled}
placeholder={placeholder} placeholder={placeholder}
className="resize-y" className='resize-y'
{...field} {...field}
/> />
)} )}
@ -67,4 +61,4 @@ export const FormTextAreaField = <
)} )}
/> />
); );
}; });

View File

@ -11,20 +11,15 @@ import {
import * as React from "react"; import * as React from "react";
import { createElement } from "react"; import { createElement } from "react";
import { import { FieldErrors, FieldPath, FieldValues, UseControllerProps } from "react-hook-form";
FieldErrors,
FieldPath,
FieldValues,
UseControllerProps,
} from "react-hook-form";
import { FormLabel, FormLabelProps } from "./FormLabel"; import { FormLabel, FormLabelProps } from "./FormLabel";
import { FormInputProps, FormInputWithIconProps } from "./FormProps"; import { FormInputProps, FormInputWithIconProps } from "./FormProps";
export type FormTextFieldProps< export type FormTextFieldProps<
TFieldValues extends FieldValues = FieldValues, TFieldValues extends FieldValues = FieldValues,
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
> = { > = {
button?: (props?) => React.ReactNode; button?: (props?: React.PropsWithChildren) => React.ReactNode;
} & InputProps & } & InputProps &
FormInputProps & FormInputProps &
Partial<FormLabelProps> & Partial<FormLabelProps> &
@ -33,23 +28,27 @@ export type FormTextFieldProps<
errors?: FieldErrors<TFieldValues>; errors?: FieldErrors<TFieldValues>;
}; };
export function FormTextField({ export const FormTextField = React.forwardRef<
label, HTMLDivElement,
placeholder, React.HTMLAttributes<HTMLDivElement> & FormTextFieldProps
hint, >((props, ref) => {
description, const {
required, label,
className, placeholder,
leadIcon, hint,
trailIcon, description,
button, required,
disabled, className,
errors, leadIcon,
name, trailIcon,
control, button,
type, disabled,
}: FormTextFieldProps) { // eslint-disable-next-line @typescript-eslint/no-unused-vars
//const error = Boolean(errors && errors[name]); errors,
name,
control,
type,
} = props;
return ( return (
<FormField <FormField
@ -57,54 +56,44 @@ export function FormTextField({
name={name} name={name}
rules={{ required }} rules={{ required }}
disabled={disabled} disabled={disabled}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
render={({ field, fieldState, formState }) => ( render={({ field, fieldState, formState }) => (
<FormItem className={cn(className, "space-y-3")}> <FormItem ref={ref} className={cn(className, "space-y-3")}>
{label && <FormLabel label={label} hint={hint} />} {label && <FormLabel label={label} hint={hint} />}
<div className={cn(button ? "flex" : null)}> <div className={cn(button ? "flex" : null)}>
<div <div
className={cn( className={cn(
leadIcon leadIcon ? "relative flex items-stretch flex-grow focus-within:z-10" : ""
? "relative flex items-stretch flex-grow focus-within:z-10"
: "",
)} )}
> >
{leadIcon && ( {leadIcon && (
<div className="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none"> <div className='absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none'>
{React.createElement( {React.createElement(
leadIcon, leadIcon,
{ {
className: "h-5 w-5 text-muted-foreground", className: "h-5 w-5 text-muted-foreground",
"aria-hidden": true, "aria-hidden": true,
}, },
null, null
)} )}
</div> </div>
)} )}
<FormControl <FormControl
className={cn( className={cn("block", leadIcon ? "pl-10" : "", trailIcon ? "pr-10" : "")}
"block",
leadIcon ? "pl-10" : "",
trailIcon ? "pr-10" : "",
)}
> >
<Input <Input type={type} placeholder={placeholder} disabled={disabled} {...field} />
type={type}
placeholder={placeholder}
disabled={disabled}
{...field}
/>
</FormControl> </FormControl>
{trailIcon && ( {trailIcon && (
<div className="absolute inset-y-0 right-0 flex items-center pl-3 pointer-events-none"> <div className='absolute inset-y-0 right-0 flex items-center pl-3 pointer-events-none'>
{createElement( {createElement(
trailIcon, trailIcon,
{ {
className: "h-5 w-5 text-muted-foreground", className: "h-5 w-5 text-muted-foreground",
"aria-hidden": true, "aria-hidden": true,
}, },
null, null
)} )}
</div> </div>
)} )}
@ -117,4 +106,4 @@ export function FormTextField({
)} )}
/> />
); );
} });