26
loading...
This website collects cookies to deliver better user experience
npx create-react-app forms
yarn add antd // For building the frontend on ant design
Layout
component from Ant design library to layout our content, where we'll add the Header
, Content
and Footer
components to our layout.Contact Us
and Get a Demo
. Contact Us
and Get a Demo
and a Footer
at the bottom. We are keeping <Content>
empty for now, we'll add them once we make our forms.
import React from "react";
import "./App.css";
import { Layout, Menu } from "antd";
import "antd/dist/antd.css";
const { Header, Content, Footer } = Layout;
const App = () => {
return (
//Layout Component
<Layout className="layout">
<Header // Header Component
style={{
position: "fixed",
zIndex: 1,
width: "100%",
}}
>
<div className="logo" />
<Menu // Header Tabs
theme="dark"
mode="horizontal"
defaultSelectedKeys={["contactUs"]}
>
<Menu.Item key="contactUs" style={{ color: "#ffffff" }}>
Contact Us
</Menu.Item>
<Menu.Item key="getADemo" style={{ color: "#ffffff" }}>
Get a Demo
</Menu.Item>
</Menu>
</Header>
<Content // Content Component
className="site-layout"
style={{
marginTop: 64,
padding: 20,
paddingBottom: 0,
}}
>
<div className="site-layout-background"></div>
</Content>
<Footer
style={{ textAlign: "center", backgroundColor: "fff" }} // Footer Component
>
Canonic ©2021 Created by Canonic Inc
</Footer>
</Layout>
);
};
export default App;
src/App.css
#components-layout-demo-fixed .logo {
float: left;
width: 120px;
height: 31px;
margin: 16px 24px 16px 0;
background: rgba(255, 255, 255, 0.2);
}
.site-layout .site-layout-background {
background: #fff;
}
ContactForm
at src/components/Contact Form
. Create the respective ContactForm.js
and index.js
files. export { default } from "./ContactForm";
ContactForm.js
file! We'll use the Form
components of Ant Design for all our input fields (FirstName, LastName, Email, etc.). They've got multiple attributes through which you can configure different settings of your input fields like Required Fields, Custom Error Message, etc. A button at the end of the form, which will let the users submit their request.// Import React & Ant Design Dependencies
import React from "react";
import { Form, Input, Button, Typography } from "antd";
const ContactForm = () => {
const [form] = Form.useForm();
const { Title, Text } = Typography;
return (
<div>
<Title // Form's Title
level={3}
style={{
marginBottom: 0,
paddingTop: 20,
paddingLeft: 30,
paddingRight: 30,
}}
>
✉️ Contact Us!
</Title>
<Text // Form's Description
type="secondary"
style={{
paddingLeft: 30,
paddingRight: 30,
}}
>
Let us know how we can help you.
</Text>
<Form // Ant Design's Form Component
name="contact-us"
layout="vertical"
form={form}
wrapperCol={{
span: 6,
}}
style={{
marginTop: 20,
paddingBottom: 10,
paddingLeft: 30,
paddingRight: 30,
}}
>
<Form.Item // Form Item (First Name)
label="First Name"
name="firstName"
required
tooltip="This is a required field"
rules={[
{
required: true,
message: "Please enter your first name!",
},
]}
>
<Input placeholder="First Name" />
</Form.Item>
<Form.Item // Form Item (Last Name)
label="Last Name"
name="lastName"
required
tooltip="This is a required field"
rules={[
{
required: true,
message: "Please enter your last name!",
},
]}
>
<Input placeholder="Last Name" />
</Form.Item>
<Form.Item // Form Item (Email)
label="Email"
name="email"
required
tooltip="This is a required field"
rules={[
{
required: true,
message: "Please enter your email!",
type: "email",
},
]}
>
<Input placeholder="Email" />
</Form.Item>
<Form.Item // Form Item (Message)
label="Type your message here"
name="message"
required
tooltip="This is a required field"
rules={[
{
required: true,
message: "Message is a required field!",
},
]}
>
<Input.TextArea
placeholder="Message ..."
autoSize={{ minRows: 4, maxRows: 6 }}
/>
</Form.Item>
<Form.Item // Form Item (Submit Button)
>
<Button type="primary">Submit</Button>
</Form.Item>
</Form>
</div>
);
};
export default ContactForm;
ContactForm
component is ready, let's add it to our layout's content to see how it looks. Head back to App.js
, import ContactForm
& update the <Content>
component.// Import ContactForm Component
import ContactForm from "./components/Contact Form";
// Add <ContactForm> in our <Content> component
...
<Content // Content Component
className="site-layout"
style={{
marginTop: 64,
padding: 20,
paddingBottom: 0,
}}
>
<div className="site-layout-background">
<ContactForm></ContactForm>
</div>
</Content>
...
Let's head to canonic. Find the Canonic-Forms
sample project, clone this project and deploy. 🚀
/Create contact us
endpoint of the Contact us Table. This is the POST API that will store the form data in the database.useContactUs.js
at src/utils/apis
.YOUR_URL_HERE
with the URL that you just copied.const UseContactUs = async (data) => {
const url = "YOUR_URL_HERE";
const submitRequest = async (reqBody) => {
try {
const res = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ input: reqBody }),
});
const json = await res.json();
return { response: json, error: undefined };
} catch (error) {
return { response: undefined, error: error };
}
};
return await submitRequest(data);
};
export default UseContactUs;
ContactUs.js
file and trigger this submit request function to post the data on our backend.useContactUs.js
fileonSubmit
which will first validate the form fields and then make a request to our backend to store the filled information.handleSubmission
that will reset our fields if the request is successful or show an error if not.onSubmit
function to our submit button's onClick
....
// Import useContactUs.js
import UseContactUs from "../../utils/apis/useContactUs";
// Add onSubmit & handleSubmission functions inside our //ContactForm component
...
const [form] = Form.useForm();
const { Title, Text } = Typography;
const handleSubmission = React.useCallback(
(result) => {
if (result.error) {
// Handle Error here
} else {
// Handle Success here
form.resetFields();
}
},
[form]
);
const onSubmit = React.useCallback(async () => {
let values;
try {
values = await form.validateFields(); // Validate the form fields
} catch (errorInfo) {
return;
}
const result = await UseContactUs(values); // Submit the form data to the backend
handleSubmission(result); // Handle the submission after the API Call
}, [form, handleSubmission]);
...
// Add the onSubmit to the onClick of our Submit Button
<Form.Item // Form Item (Submit Button)
>
<Button type="primary" onClick={onSubmit}>
Submit
</Button>
</Form.Item>
...
notification
component here that we can use. Let's create a new file showNotification.js
at src/utils/views
where we can write the code to show these notifications and use it in our ContactUs
component.import { notification } from "antd";
const showNotification = (type, details) => {
notification[type]({
message: details.message,
description: details.description,
});
};
export default showNotification;
Constants.js
file at src/utils/constants
that can hold the messages for success and error:const NOTIFICATION_DETAILS = {
success: {
message: "Details Submitted!",
description:
"We've got your information. Our team will get in touch you shortly!",
},
error: {
message: "Something went wrong!",
description: "Please try again later or email us to [email protected]!",
},
};
export default NOTIFICATION_DETAILS;
ContactUs.js
component. We'll use our handleSubmisson
function to show there notifications....
//Import the new Notification and Constants files
import NOTIFICATION_DETAILS from "../../utils/constants/Constants";
import showNotification from "../../utils/views/showNotification";
...
const handleSubmission = (result) => {
if (result.error) {
showNotification("error", NOTIFICATION_DETAILS.error); // Show Success Notification
} else {
showNotification("success", NOTIFICATION_DETAILS.success); // Show Error Notification
form.resetFields();
}
};
...