Last active
February 8, 2022 09:09
-
-
Save rzubrycki/822b9ed7639e4fa03816a5c3850bf3d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { Formik } from 'formik'; | |
import Grid from '@material-ui/core/Grid'; | |
import * as Yup from 'yup'; | |
import Button from '@material-ui/core/Button'; | |
import TextField from '@material-ui/core/TextField'; | |
import Container from '@material-ui/core/Container'; | |
const ContactSchema = Yup.object().shape({ | |
email: Yup.string() | |
.email('Enter a valid email') | |
.required('Email is required'), | |
firstName: Yup.string().required('First name is required'), | |
lastName: Yup.string().required('Last name is required'), | |
company: Yup.string(), | |
phone: Yup.number().required('Phone is required'), | |
message: Yup.string() | |
.min(5, 'Message is too short') | |
.required('Message is required'), | |
}); | |
export const ContactForm = () => { | |
return ( | |
<Container maxWidth="md"> | |
<Formik | |
initialValues={{ | |
email: '', | |
firstName: '', | |
lastName: '', | |
company: '', | |
phone: '', | |
message: '', | |
}} | |
validationSchema={ContactSchema} | |
onSubmit={() => {}} | |
> | |
{({ | |
values, | |
errors, | |
touched, | |
isSubmitting, | |
handleSubmit, | |
handleChange, | |
}) => ( | |
<form | |
name="contact" | |
method="post" | |
data-netlify="true" | |
data-netlify-honeypot="bot-field" | |
action="/success/" | |
autoComplete="off" | |
onSubmit={handleSubmit} | |
> | |
<h2>Contact</h2> | |
<input type="hidden" name="bot-field" /> | |
<input type="hidden" name="form-name" value="contact" /> | |
<Grid container spacing={2}> | |
<Grid item xs={12}> | |
<TextField | |
fullWidth | |
variant="outlined" | |
id="email" | |
name="email" | |
label="Email" | |
type="email" | |
defaultValue={values.email} | |
onChange={handleChange} | |
error={errors.email && touched.email} | |
helperText={touched.email && errors.email} | |
/> | |
</Grid> | |
<Grid item xs={12}> | |
<TextField | |
fullWidth | |
variant="outlined" | |
id="firstName" | |
name="firstName" | |
label="First name" | |
defaultValue={values.firstName} | |
onChange={handleChange} | |
error={touched.firstName && Boolean(errors.firstName)} | |
helperText={touched.firstName && errors.firstName} | |
/> | |
</Grid> | |
<Grid item xs={12}> | |
<TextField | |
fullWidth | |
variant="outlined" | |
id="lastName" | |
name="lastName" | |
label="Last name" | |
defaultValue={values.lastName} | |
onChange={handleChange} | |
error={touched.lastName && Boolean(errors.lastName)} | |
helperText={touched.lastName && errors.lastName} | |
/> | |
</Grid> | |
<Grid item xs={12}> | |
<TextField | |
fullWidth | |
variant="outlined" | |
id="company" | |
name="company" | |
label="Company" | |
defaultValue={values.company} | |
onChange={handleChange} | |
/> | |
</Grid> | |
<Grid item xs={12}> | |
<TextField | |
fullWidth | |
variant="outlined" | |
id="phone" | |
name="phone" | |
label="Phone number" | |
onChange={handleChange} | |
error={touched.phone && Boolean(errors.phone)} | |
helperText={touched.phone && errors.phone} | |
defaultValue={values.phone} | |
/> | |
</Grid> | |
<Grid item xs={12}> | |
<TextField | |
fullWidth | |
id="message" | |
name="message" | |
label="Message" | |
multiline | |
rows={4} | |
variant="outlined" | |
defaultValue={values.message} | |
onChange={handleChange} | |
error={touched.message && Boolean(errors.message)} | |
helperText={touched.message && errors.message} | |
/> | |
</Grid> | |
<Grid item xs={12}> | |
<Button | |
variant="contained" | |
color="primary" | |
type="submit" | |
disabled={isSubmitting} | |
> | |
SUBMIT | |
</Button> | |
</Grid> | |
</Grid> | |
</form> | |
)} | |
</Formik> | |
</Container> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment