Contact Form React.JS Component by #HexiPi
Installation:
npm install contact-form-hexipi --save
OR
yarn add contact-form-hexipi
Example of Usage:
import React from 'react';
import ContactForm, { FormRes } from 'contact-form-hexipi';
import './App.css';
class App extends React.Component {
state = {
formSubmitResult: FormRes.NONE,
};
onSubmit = formData => {
alert(`Form Data: ${JSON.stringify(formData)}`);
this.setState({
formSubmitResult: FormRes.OK,
})
};
formSubmitResultReset = () => this.setState({ formSubmitResult: FormRes.NONE });
render() {
return (
<div className="App">
<header className="App-header">
<ContactForm
submitMethod="post"
email="info@example.com"
//(EITHER OR, BUT NOT BOTH!)
tel="+18005555555"
//OR
telWithCountryCode={["8005555555", "US"]}
socialMediaLinks={[
//Facebook link
"https://www.facebook.com/HexiPi.Web.Dev",
//Instagram Link
"https://instagram.com/hexipi",
//YouTube link
"https://www.youtube.com/channel/UCxJUbbqJ_3hpaL53vn2EFVA",
//Website Link
"https://hexipi.com/"
//...Or anything you can think of!
]}
onSubmitCallback={this.onSubmit}
formSubmitResult={this.state.formSubmitResult}
formSubmitResultReset={this.formSubmitResultReset}
backgroundColor="#270941ec"
autoFormatPhoneNumber={
{ shouldFormat: true, countryCode: 'US' }
}
/>
</header>
</div>
);
}
}
export default App;
import React, { useState } from 'react';
import ContactForm, { FormRes } from 'contact-form-hexipi';
import './App.css';
const App = () => {
const [formSubmitResult, setFormSubmitResult] = useState(FormRes.NONE);
const onSubmit = formData => {
alert(`Form Data: ${JSON.stringify(formData)}`);
setFormSubmitResult(FormRes.OK);
};
const formSubmitResultReset = () => setFormSubmitResult(FormRes.NONE);
return (
<div className="App">
<header className="App-header">
<ContactForm
submitMethod="post"
email="info@example.com"
//(EITHER OR, BUT NOT BOTH!)
tel="+18005555555"
//OR
telWithCountryCode={["8005555555", "US"]}
socialMediaLinks={[
//Facebook link
"https://www.facebook.com/HexiPi.Web.Dev",
//Instagram Link
"https://instagram.com/hexipi",
//YouTube link
"https://www.youtube.com/channel/UCxJUbbqJ_3hpaL53vn2EFVA",
//Website Link
"https://hexipi.com/"
//...Or anything you can think of!
]}
onSubmitCallback={onSubmit}
formSubmitResult={formSubmitResult}
formSubmitResultReset={formSubmitResultReset}
backgroundColor="#270941ec"
autoFormatPhoneNumber={
{ shouldFormat: true, countryCode: 'US' }
}
/>
</header>
</div>
);
}
export default App;
Below is an example of how you can customize your form using CSS:
...
#contact-section {
background-image: linear-gradient(#270941ec, rgba(0, 0, 0, 0.555)),
url(~./images/custom-img.jpg);
#contact-section .contact-btn {
background-color: white;
color: black;
border-radius: 0;
}
#contact-section .contact-btn:hover {
background-color: yellow;
color: blue;
}
...
}
For other CSS selectors and to see the default styles, go to the GitHub Repo here.
Attributes & Data Types:
Below is a list of all the available attributes:
interface ContactFormProps {
submitMethod: 'get' | 'post',
email?: string,
tel?: string,
telWithCountryCode?: string[],
fax?: string,
faxWithCountryCode?: string[],
socialMediaLinks?: string[],
mainHeading?: string,
subHeading?: string,
formSubmitOKMsg?: string,
formSubmitErrorMsg?: string,
formSubmitResult: FormRes,
backgroundColor?: string,
autoFormatPhoneNumber?: boolean
onSubmitCallback: (formData: ContactFormSubmissionData) => void,
formSubmitResultReset: () => void
};
Note: Most attributes are technically optional since they already have default values assigned to them. However the ones that are actually optional (marked with a "?") will not be shown or used by default (with the exception of the default headings). All callback functions are required if you actually want the form to work properly.
Below are the default values:
static defaultProps = {
submitMethod: "get",
email: undefined,
tel: undefined,
telWithCountryCode: undefined,
fax: undefined,
faxWithCountryCode: undefined,
socialMediaLinks: undefined,
onSubmitCallback:
(formData: ContactFormSubmissionData) => alert(JSON.stringify(formData)),
mainHeading: "Need More Information?",
subHeading: "Send Us a Message!",
formSubmitOKMsg: "Form Submitted!",
formSubmitErrorMsg: "An error has occurred (why???)! 😥 Please try again later."
formSubmitResult: FormRes.NONE,
backgroundColor: 'black',
autoFormatPhoneNumber: { shouldFormat: true, countryCode: 'US' }
};
Below are all the available values of the FormRes Enumerator:
interface PhoneNumberFormat {
shouldFormat: boolean,
countryCode?: string
}
enum FormRes {
OK,
ERROR,
NONE
}
Below are all the available values of the ContactFormSubmissionData interface:
interface ContactFormSubmissionData {
name: string,
email: string,
phone_number?: string,
message: string
}