From 279d28968c7fa49341460074be186740c6317699 Mon Sep 17 00:00:00 2001 From: Marco Escaleira Date: Thu, 14 Mar 2024 22:53:42 +0000 Subject: [PATCH] feat: forms display error message --- src/components/Form/ErrorText.tsx | 9 ++ src/components/Form/FormInput.tsx | 25 ++++++ src/components/Form/index.ts | 2 + src/components/Quiz/OptionsFields.tsx | 12 +-- src/components/Quiz/QuestionsFields.tsx | 11 ++- src/components/Quiz/QuizForm.tsx | 43 ++++----- src/components/Register/RegisterForm.tsx | 108 ++++++++--------------- 7 files changed, 104 insertions(+), 106 deletions(-) create mode 100644 src/components/Form/ErrorText.tsx create mode 100644 src/components/Form/FormInput.tsx create mode 100644 src/components/Form/index.ts diff --git a/src/components/Form/ErrorText.tsx b/src/components/Form/ErrorText.tsx new file mode 100644 index 0000000..9f15ac7 --- /dev/null +++ b/src/components/Form/ErrorText.tsx @@ -0,0 +1,9 @@ +import { Typography } from "@material-tailwind/react"; + +export const ErrorText = ({ text, className }: { text: string; className?: string }) => { + return ( + + {text} + + ); +}; diff --git a/src/components/Form/FormInput.tsx b/src/components/Form/FormInput.tsx new file mode 100644 index 0000000..1029efb --- /dev/null +++ b/src/components/Form/FormInput.tsx @@ -0,0 +1,25 @@ +import { Input, InputProps } from "@material-tailwind/react"; +import { FieldError, FieldValues, RegisterOptions, useFormContext } from "react-hook-form"; +import { ErrorText } from "./ErrorText"; + +interface FormInputProps extends Pick { + name: string; + registerOptions?: RegisterOptions; + fieldError?: FieldError; +} + +export const FormInput = ({ name, registerOptions, fieldError, ...props }: FormInputProps) => { + const { + register, + formState: { errors }, + } = useFormContext(); + + const error = fieldError?.message || errors?.[name]?.message?.toString() || ""; + + return ( +
+ + {error && } +
+ ); +}; diff --git a/src/components/Form/index.ts b/src/components/Form/index.ts new file mode 100644 index 0000000..4431801 --- /dev/null +++ b/src/components/Form/index.ts @@ -0,0 +1,2 @@ +export * from "./FormInput"; +export * from "./ErrorText"; diff --git a/src/components/Quiz/OptionsFields.tsx b/src/components/Quiz/OptionsFields.tsx index 4c54c63..35137b8 100644 --- a/src/components/Quiz/OptionsFields.tsx +++ b/src/components/Quiz/OptionsFields.tsx @@ -1,7 +1,8 @@ -import { Checkbox, IconButton, Input, Tooltip, Typography } from "@material-tailwind/react"; +import { Checkbox, IconButton, Tooltip, Typography } from "@material-tailwind/react"; import { Plus, X } from "lucide-react"; import { useFieldArray, useFormContext } from "react-hook-form"; import { z } from "zod"; +import { FormInput } from "@components/Form"; import { quizFormSchema } from "./quizFormSchema"; interface OptionsFieldsProps { @@ -34,7 +35,7 @@ export function OptionsFields({ questionIndex }: OptionsFieldsProps) { {fields.map((field, optionIndex) => (
-
+
Option #{optionIndex + 1} remove(optionIndex)} variant="text" size="sm"> @@ -43,12 +44,11 @@ export function OptionsFields({ questionIndex }: OptionsFieldsProps) {
-
-
diff --git a/src/components/Quiz/QuizForm.tsx b/src/components/Quiz/QuizForm.tsx index 5991f09..9affecc 100644 --- a/src/components/Quiz/QuizForm.tsx +++ b/src/components/Quiz/QuizForm.tsx @@ -1,7 +1,7 @@ import { cloneElement, useCallback, useEffect } from "react"; import { useMutation, useQuery } from "@apollo/client"; import { zodResolver } from "@hookform/resolvers/zod"; -import { Button, Input, Option, Select, Textarea, Typography } from "@material-tailwind/react"; +import { Button, Option, Select, Textarea } from "@material-tailwind/react"; import { Loader2 } from "lucide-react"; import { FormProvider, SubmitHandler, useForm } from "react-hook-form"; import { useNavigate, useParams, useSearchParams } from "react-router-dom"; @@ -9,6 +9,7 @@ import { toast } from "react-toastify"; import { useCountries } from "use-react-countries"; import { z } from "zod"; import { DifficultyChip } from "@components/DifficultyChip/DifficultyChip.tsx"; +import { ErrorText, FormInput } from "@components/Form"; import { QuestionFields } from "@components/Quiz/QuestionsFields.tsx"; import { quizFormSchema } from "@components/Quiz/quizFormSchema.ts"; import { TagsInput } from "@components/TagsInput/TagsInput.tsx"; @@ -114,15 +115,18 @@ export function QuizForm() { return (
- + -