import React, { useState } from "react"; import FormStatus from "./../FormStatus"; import FormField from "./../FormField"; import SectionButton from "./../SectionButton"; import "./styles.scss"; function ContactForm(props) { // State for input values const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [message, setMessage] = useState(""); // Whether to show errors // We set to true if they submit and there are errors. // We only show errors after they submit because // it's annoying to see errors while typing. const [showErrors, setShowErrors] = useState(false); // Error array we'll populate let errors = []; // Function for fetching error for a field const getError = field => { return errors.find(e => e.field === field); }; // Function to see if field is empty const isEmpty = val => val.trim() === ""; // Add error if email empty if (isEmpty(email)) { errors.push({ field: "email", message: "Please enter an email" }); } // Add error if message empty if (isEmpty(message)) { errors.push({ field: "message", message: "Please enter a message" }); } // Add error if name shown and empty if (props.showNameField) { if (isEmpty(name)) { errors.push({ field: "name", message: "Please enter your name" }); } } // Handle form submission const handleSubmit = e => { // If field errors then show them if (errors.length) { setShowErrors(true); } else { // Otherwise call onSubmit with form data if (props.onSubmit) { props.onSubmit({ name, email, message }); } } }; return ( <> {props.status && props.status.message && ( )}
{ e.preventDefault(); handleSubmit(); }} >
{props.showNameField && ( setName(value)} /> )} setEmail(value)} />
setMessage(value)} />
{props.buttonText}
); } export default ContactForm;