Files
home/Projects/kompose/news/apps/web/src/components/form-controlled-input.tsx

49 lines
1002 B
TypeScript
Raw Normal View History

2025-10-10 16:43:21 +02:00
import { Control, FieldPath, FieldValues } from "react-hook-form";
import type { ComponentProps } from "react";
2025-10-08 10:35:48 +02:00
import {
2025-10-10 16:43:21 +02:00
FormField,
FormItem,
FormLabel,
FormControl,
Input,
FormDescription,
FormMessage,
} from "@repo/ui";
2025-10-08 10:35:48 +02:00
interface FormControlledInputProps<
2025-10-10 16:43:21 +02:00
TFieldValues extends FieldValues = FieldValues,
2025-10-08 10:35:48 +02:00
> {
2025-10-10 16:43:21 +02:00
control: Control<TFieldValues>;
name: FieldPath<TFieldValues>;
label: string;
description?: string;
inputProps?: ComponentProps<typeof Input>;
2025-10-08 10:35:48 +02:00
}
export function FormControlledInput<
2025-10-10 16:43:21 +02:00
TFieldValues extends FieldValues = FieldValues,
2025-10-08 10:35:48 +02:00
>({
2025-10-10 16:43:21 +02:00
control,
name,
label,
description,
inputProps,
2025-10-08 10:35:48 +02:00
}: FormControlledInputProps<TFieldValues>) {
2025-10-10 16:43:21 +02:00
return (
<FormField
control={control}
name={name}
render={({ field }) => (
<FormItem>
<FormLabel>{label}</FormLabel>
<FormControl>
<Input {...inputProps} {...field} />
</FormControl>
{description && <FormDescription>{description}</FormDescription>}
<FormMessage />
</FormItem>
)}
/>
);
2025-10-08 10:35:48 +02:00
}