From 00538bdc0d51af2707c127a71b9615f042fbc24e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C6=B0=C6=A1ng=20=C4=90=E1=BB=A9c=20Tr=E1=BB=8Dng?= Date: Sat, 22 Jun 2024 01:36:09 +0700 Subject: [PATCH] refactor: add tests --- src/form-control.tsx | 5 +- src/form-description.tsx | 5 +- src/form-item.tsx | 23 ++--- src/form-label.tsx | 34 +++---- src/form-message.tsx | 5 +- src/form.tsx | 94 +++++++++++++++++++ src/hooks/use-scope.ts | 35 ------- src/index.ts | 3 +- .../__snapshots__/create-field.test.tsx.snap | 54 +++++++++++ tests/create-field.test.tsx | 62 ++++++++++++ 10 files changed, 253 insertions(+), 67 deletions(-) create mode 100644 src/form.tsx delete mode 100644 src/hooks/use-scope.ts create mode 100644 tests/__snapshots__/create-field.test.tsx.snap create mode 100644 tests/create-field.test.tsx diff --git a/src/form-control.tsx b/src/form-control.tsx index 1fb918f..6fefe44 100644 --- a/src/form-control.tsx +++ b/src/form-control.tsx @@ -4,9 +4,12 @@ import { Slot } from "@radix-ui/react-slot" import React, { forwardRef } from "react" import { useFormField } from "./hooks/use-form-field" +export interface FormControlProps + extends React.ComponentPropsWithoutRef {} + export const FormControl = forwardRef< React.ElementRef, - React.ComponentPropsWithoutRef + FormControlProps >(({ ...props }, ref) => { const { error, formItemId, formDescriptionId, formMessageId } = useFormField() diff --git a/src/form-description.tsx b/src/form-description.tsx index 4c395ca..42fd360 100644 --- a/src/form-description.tsx +++ b/src/form-description.tsx @@ -4,9 +4,12 @@ import { forwardRef, HTMLAttributes } from "react" import { useFormField } from "./hooks/use-form-field" import { cn } from "./utils" +export interface FormDescriptionProps + extends HTMLAttributes {} + export const FormDescription = forwardRef< HTMLParagraphElement, - HTMLAttributes + FormDescriptionProps >(({ className, ...props }, ref) => { const { formDescriptionId } = useFormField() diff --git a/src/form-item.tsx b/src/form-item.tsx index dc8ca5c..57cbdd4 100644 --- a/src/form-item.tsx +++ b/src/form-item.tsx @@ -4,17 +4,18 @@ import { forwardRef, HTMLAttributes, useId, useMemo } from "react" import FormItemContext from "./context/form-item-context" import { cn } from "./utils" -export const FormItem = forwardRef< - HTMLDivElement, - HTMLAttributes ->(({ className, ...props }, ref) => { - const id = useId() +export interface FormItemProps extends HTMLAttributes {} - return ( - ({ id }), [id])}> -
- - ) -}) +export const FormItem = forwardRef( + ({ className, ...props }, ref) => { + const id = useId() + + return ( + ({ id }), [id])}> +
+ + ) + } +) FormItem.displayName = "FormItem" diff --git a/src/form-label.tsx b/src/form-label.tsx index a687533..f776a1e 100644 --- a/src/form-label.tsx +++ b/src/form-label.tsx @@ -4,22 +4,24 @@ import React, { forwardRef } from "react" import { useFormField } from "./hooks/use-form-field" import { cn } from "./utils" -export const FormLabel = forwardRef< - React.ElementRef<"label">, - React.ComponentPropsWithoutRef<"label"> ->(({ className, ...props }, ref) => { - const { error, formItemId, name } = useFormField() +export interface FormLabelProps + extends React.ComponentPropsWithoutRef<"label"> {} - return ( -