diff --git a/06_react/unterricht/tag37/02_react-with-forms/src/components/forms/FormContact.jsx b/06_react/unterricht/tag37/02_react-with-forms/src/components/forms/FormContact.jsx index 00e01dd..fbefec7 100644 --- a/06_react/unterricht/tag37/02_react-with-forms/src/components/forms/FormContact.jsx +++ b/06_react/unterricht/tag37/02_react-with-forms/src/components/forms/FormContact.jsx @@ -3,23 +3,33 @@ import { useState } from 'react'; const FormContact = (props) => { // const { } = props; - const FORM_DATA = { + const INITIAL_FORM_DATA = { firstname: '', lastname: '', email: '', message: '', }; - const [formData, setFormData] = useState(FORM_DATA); - + // Hooks + const [formData, setFormData] = useState(INITIAL_FORM_DATA); + const [errors, setErrors] = useState({}); // EventHandlers const handleSubmit = (e) => { e.preventDefault(); + const form = e.currentTarget; const errors = validate(formData); - // AJAX/FETCH send to server - console.log('FormData: ', formData); + if (Object.keys(errors).length === 0) { + // AJAX/FETCH send to server + console.log('FormData: ', formData); + setFormData(INITIAL_FORM_DATA); // reset + form.reset(); // not necessary in react + setErrors({}); + } else { + console.error('Form submit error'); + setErrors(errors); + } }; const handleChange = (e) => { @@ -40,7 +50,16 @@ const FormContact = (props) => { - + +
OK
+
{errors?.firstname || 'Error'}
@@ -48,7 +67,14 @@ const FormContact = (props) => { - + +
{errors?.lastname || 'Error'}
@@ -58,7 +84,14 @@ const FormContact = (props) => { - + +
{errors?.email || 'Error'}
@@ -73,8 +106,9 @@ const FormContact = (props) => { id="textarea-message" cols="30" rows="5" - className="form-control" - onChange={handleChange}> + onChange={handleChange} + className={`form-control ${errors.message ? 'is-invalid' : ''}`}> +
{errors?.message || 'Error'}
diff --git a/06_react/unterricht/tag37/02_react-with-forms/src/components/forms/react-hook-form/FormContact.jsx b/06_react/unterricht/tag37/02_react-with-forms/src/components/forms/react-hook-form/FormContact.jsx new file mode 100644 index 0000000..62c5392 --- /dev/null +++ b/06_react/unterricht/tag37/02_react-with-forms/src/components/forms/react-hook-form/FormContact.jsx @@ -0,0 +1,125 @@ +import validate from '../../../utils/validators/validate'; +import { useState } from 'react'; + +const FormContact = (props) => { + // const { } = props; + const INITIAL_FORM_DATA = { + firstname: '', + lastname: '', + email: '', + message: '', + }; + + // Hooks + const [formData, setFormData] = useState(INITIAL_FORM_DATA); + const [errors, setErrors] = useState({}); + // EventHandlers + const handleSubmit = (e) => { + e.preventDefault(); + const form = e.currentTarget; + + const errors = validate(formData); + + if (Object.keys(errors).length === 0) { + // AJAX/FETCH send to server + console.log('FormData: ', formData); + setFormData(INITIAL_FORM_DATA); // reset + form.reset(); // not necessary in react + setErrors({}); + } else { + console.error('Form submit error'); + setErrors(errors); + } + }; + + const handleChange = (e) => { + // console.log('Event: ', e); + const { name, value } = e.target; + + setFormData((state) => ({ ...state, [name]: value })); + + // console.log({ [e.target.name]: e.target.value }); + // console.log('formData: ', formData); + }; + + return ( +
+
+
+
+ + +
OK
+
{errors?.firstname || 'Error'}
+
+
+
+
+ + +
{errors?.lastname || 'Error'}
+
+
+
+
+
+
+ + +
{errors?.email || 'Error'}
+
+
+
+
+
+
+ + +
{errors?.message || 'Error'}
+
+
+
+
+
+ +
+
+
+ ); +}; +export default FormContact; diff --git a/06_react/unterricht/tag37/02_react-with-forms/src/pages/ContactPage.jsx b/06_react/unterricht/tag37/02_react-with-forms/src/pages/ContactPage.jsx index bfbe7be..d00ae27 100644 --- a/06_react/unterricht/tag37/02_react-with-forms/src/pages/ContactPage.jsx +++ b/06_react/unterricht/tag37/02_react-with-forms/src/pages/ContactPage.jsx @@ -1,4 +1,4 @@ -import FormContact from '../components/forms/FormContact'; +import FormContact from '../components/forms/react-hook-form/FormContact'; function ContactPage() { return ( diff --git a/06_react/unterricht/tag37/02_react-with-forms/src/utils/validators/validate.js b/06_react/unterricht/tag37/02_react-with-forms/src/utils/validators/validate.js index 753126a..8931def 100644 --- a/06_react/unterricht/tag37/02_react-with-forms/src/utils/validators/validate.js +++ b/06_react/unterricht/tag37/02_react-with-forms/src/utils/validators/validate.js @@ -25,3 +25,5 @@ const validate = (formData) => { return errors; }; + +export default validate;