diff --git a/README.md b/README.md index 7602b90..39c5c1d 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,12 @@ Error boundaries occur by ErrorFallBack component +## Generic form + +`yarn add react-hook-form` + + + diff --git a/apps/react-practice-demo/src/app/app.tsx b/apps/react-practice-demo/src/app/app.tsx index 3c94c00..f3ec65b 100644 --- a/apps/react-practice-demo/src/app/app.tsx +++ b/apps/react-practice-demo/src/app/app.tsx @@ -25,6 +25,8 @@ import FormSubmition from './components/FormSubmition/FormSubmition'; import HandleSubmitionError from './components/FormSubmition/HandleSubmissionError'; import FormSubmissionClient from './components/FormSubmition/FormSubmissionClient'; import DisplayPendingStateDuringFormSubmission from './components/FormSubmition/DisplayPendingstateForm'; +import GenericForm from './components/GenericForm/GenericForm'; + //import UserListfetch from './components/AsyncDataFetching/UserListFetch'; // Lazy loading component @@ -132,6 +134,9 @@ const App = () => { Display Pending State During Form Submission + + Generic Form + @@ -201,6 +206,7 @@ const App = () => { } /> } /> } /> + }/> Not Found} /> diff --git a/apps/react-practice-demo/src/app/components/FormSubmition/DisplayPendingStateForm.tsx b/apps/react-practice-demo/src/app/components/FormSubmition/DisplayPendingStateForm.tsx index 8ae7168..c3cbf74 100644 --- a/apps/react-practice-demo/src/app/components/FormSubmition/DisplayPendingStateForm.tsx +++ b/apps/react-practice-demo/src/app/components/FormSubmition/DisplayPendingStateForm.tsx @@ -1,36 +1,29 @@ - -import { useFormStatus } from "react-dom"; +import { useFormStatus } from 'react-dom'; export default function DisplayPendingStateDuringFormSubmission() { - - const Buttons = () => { - const { pending } = useFormStatus(); - return ( - - ); - } - - const handleSubmit = async (formData: any) => { - const name = formData.get('name'); - console.log(name); - // Simulate a network request - await new Promise(resolve => setTimeout(resolve, 2000)); - - } + const Buttons = () => { + const { pending } = useFormStatus(); + return ( + + ); + }; + const handleSubmit = async (formData: any) => { + const name = formData.get('name'); + console.log(name); + // Simulate a network request + await new Promise((resolve) => setTimeout(resolve, 2000)); + }; return ( - -
-
- Display a pending state during form submission -
- - -
- +
+
+ Display a pending state during form submission +
+ + +
); } - diff --git a/apps/react-practice-demo/src/app/components/FormSubmition/FormSubmition.tsx b/apps/react-practice-demo/src/app/components/FormSubmition/FormSubmition.tsx index dc58bc3..64bcccc 100644 --- a/apps/react-practice-demo/src/app/components/FormSubmition/FormSubmition.tsx +++ b/apps/react-practice-demo/src/app/components/FormSubmition/FormSubmition.tsx @@ -9,17 +9,20 @@ export default function FormSubmition() { console.log(name, email, message); } - const saveDraft = async (formData: any) => { + const saveDraft = async (formData: FormData) => { const name = formData.get('name'); const email = formData.get('email'); const message = formData.get('message'); console.log(name, email, message); } + + + return(

Handling multiple submission types

-
+
diff --git a/apps/react-practice-demo/src/app/components/GenericForm/GenericForm.tsx b/apps/react-practice-demo/src/app/components/GenericForm/GenericForm.tsx new file mode 100644 index 0000000..861a2c7 --- /dev/null +++ b/apps/react-practice-demo/src/app/components/GenericForm/GenericForm.tsx @@ -0,0 +1,70 @@ +import React from 'react'; +import { useForm } from 'react-hook-form'; + +interface FormInput { + name: string; + email: string; + phone: string; +} + +const GenericForm = () => { + const { + register, + handleSubmit, + formState: { errors }, + reset, + } = useForm(); + + const onSubmit = (data:any) => { + console.log(JSON.stringify(data)); + reset(); + }; + + return ( +
+ +
+ + + {errors.name &&

{errors.name.message}

} +
+ +
+ + + {errors.email && ( +

{errors.email.message}

+ )} +
+
+ + +
+
+ +
+ +
+ ); +}; + +export default GenericForm; diff --git a/package.json b/package.json index 24149cd..b2af014 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "react-bootstrap": "^2.10.9", "react-dom": "19.0.0", "react-error-boundary": "^6.0.0", + "react-hook-form": "^7.59.0", "react-redux": "^9.2.0", "react-router-dom": "^7.1.5" }, diff --git a/yarn.lock b/yarn.lock index 0ea265c..d76e69e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9451,6 +9451,11 @@ react-error-boundary@^6.0.0: dependencies: "@babel/runtime" "^7.12.5" +react-hook-form@^7.59.0: + version "7.59.0" + resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-7.59.0.tgz#69e7ebc4db933f7aa0ef0973ec8e09f299e5f24e" + integrity sha512-kmkek2/8grqarTJExFNjy+RXDIP8yM+QTl3QL6m6Q8b2bih4ltmiXxH7T9n+yXNK477xPh5yZT/6vD8sYGzJTA== + react-is@^16.13.1, react-is@^16.3.2: version "16.13.1" resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"