Last active
May 4, 2020 22:37
-
-
Save jcz530/82f50f0c7ac508906c3a2c33913a31df to your computer and use it in GitHub Desktop.
Ant Design Netlify Contact Form Component
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 { Row, Col, Button, Form, Input } from 'antd' | |
const { TextArea } = Input | |
import { UserOutlined, MailOutlined } from '@ant-design/icons' | |
function encode(data) { | |
return Object.keys(data) | |
.map(key => encodeURIComponent(key) + `=` + encodeURIComponent(data[key])) | |
.join(`&`) | |
} | |
const NetlifyContactForm = () => { | |
const formName = `contact` | |
const handleSubmit = (values) => { | |
if (values[`bot-field`] === undefined) { | |
delete values[`bot-field`] | |
} | |
fetch(`/`, { | |
method: `POST`, | |
headers: { 'Content-Type': `application/x-www-form-urlencoded` }, | |
body: encode({ | |
'form-name': formName, | |
...values, | |
}), | |
}) | |
.then(() => showSuccess()) | |
.catch(error => showError(error)) | |
} | |
const showSuccess = () => { | |
// TODO: Show a success message or navigate to a success page. | |
console.log(`form submitted successfully`) | |
} | |
const showError = (error) => { | |
// TODO: Show an error message to the user | |
console.log(`There was an error submitting the form`) | |
console.log(error) | |
} | |
return ( | |
<Row | |
justify="space-around" | |
> | |
<Col | |
xs={22} | |
sm={18} | |
md={16} | |
lg={8} | |
> | |
{/* | |
This defines how your form is setup for the Netlify bots. | |
Users will not see or interact with this form. | |
*/} | |
<form | |
name={formName} | |
data-netlify="true" | |
data-netlify-honeypot="bot-field" | |
hidden | |
> | |
<input type="text" name="name" /> | |
<input type="email" name="email" /> | |
<textarea name="message"></textarea> | |
</form> | |
<Form | |
name="cf" | |
method="post" | |
onFinish={handleSubmit} | |
layout="vertical" | |
> | |
{/* This is the hidden field that the netlify-honeypot uses. */} | |
<Form.Item | |
label="Don't fill this out" | |
className={`hidden`} | |
style={{ display: `none` }} | |
name="bot-field" | |
> | |
<Input type={`hidden`} /> | |
</Form.Item> | |
<Form.Item | |
label="Name" | |
rules={[{ required: true, message: `Please enter your name.` }]} | |
name="name" | |
> | |
<Input | |
placeholder="Name" | |
prefix={<UserOutlined className="site-form-item-icon" />} | |
/> | |
</Form.Item> | |
<Form.Item | |
label="Email" | |
rules={[{ required: true, type: `email`, message: `Please enter your email.` }]} | |
name="email" | |
> | |
<Input | |
placeholder="Your Email" | |
prefix={<MailOutlined className="site-form-item-icon" />} | |
/> | |
</Form.Item> | |
<Form.Item | |
label="Message" | |
rules={[{ required: true, message: `Please enter your message.` }]} | |
name="message" | |
> | |
<TextArea | |
placeholder="Your Message" | |
rows={5} | |
/> | |
</Form.Item> | |
<Form.Item> | |
<Button type="primary" htmlType="submit" disabled={false}> | |
Send | |
</Button> | |
</Form.Item> | |
</Form> | |
</Col> | |
</Row> | |
) | |
} | |
export default NetlifyContactForm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment