-
Notifications
You must be signed in to change notification settings - Fork 908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React component #336
Comments
Thanks for sharing this! |
Was there a question or suggestion with this particular issue? Or did it work for you? |
This is actually a suggestion. I didn't know where exactly I could put this code and maybe make it available for everyone so I opened this issue. Sorry, kinda new to this "contributing" stuff. |
Awesome, this is a great place to share that actually - thanks again for doing that! Just wanted to make sure I didn't miss anything :) |
Oh, nice. Feel free to share it, it's kinda a way of 'giving back to the community' |
Thanks @angelod1as ! This is a great way to share it, if anyone wants to see this in the main readme, we could add links or references to the FAQ section perhaps. I've seen similar requests before, so definitely something we'll consider if people keep asking for it, too. Thanks again for sharing! |
I just updated this form using hooks and React Hook Form. It's below, for comparison and later usage, import { useCallback, useState } from 'react'
import { useForm } from 'react-hook-form'
const config = {
script:
'https://script.google.com/macros/s/AKfycbwMxYDrufp73bKdU8gMvxFDdHRuzcR4IKQUB33B7GqwyfyZS04/exec',
sheet: 'responses',
// email: '',
}
export default function Contact() {
const { register, handleSubmit } = useForm()
const [sent, setSent] = useState(false)
const onSubmit = useCallback((data) => {
const { honeypot } = data
// checking if bot
if (honeypot) {
return false
}
// use xhr as axios won't work (CORS)
const xhr = new XMLHttpRequest()
xhr.open('POST', config.script)
// xhr.withCredentials = true
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
xhr.onreadystatechange = function xhrGo() {
setSent(true)
}
// preparing data
const encoded = Object.keys(data)
.map(k => {
return `${encodeURIComponent(k)}=${encodeURIComponent(data[k])}`
})
.join('&')
// finally sending
xhr.send(encoded)
return true
}, [])
return (
<>
{sent ? (
<div className="thankyou_message">
<h2>Thanks for contacting us! We will get back to you soon!</h2>
</div>
) : (
<form onSubmit={handleSubmit(onSubmit)}>
<div>
<label htmlFor="name">
Name:
<input
ref={register}
id="name"
key="name"
name="name"
placeholder="What your Mom calls you"
/>
</label>
<label htmlFor="message">
Message:
<textarea
ref={register}
id="message"
key="message"
name="message"
rows={10}
placeholder="Tell us what's on your mind..."
/>
</label>
<label htmlFor="email">
Your Email Address:
<input
ref={register}
id="email"
key="email"
name="email"
type="email"
required
placeholder="[email protected]"
/>
</label>
<label htmlFor="honeypot" style={{ display: 'none' }}>
{/* To help avoid spam, utilize a Honeypot technique with a hidden text field; must
be empty to submit the form! Otherwise, we assume the user is a spam bot. */}
<input
ref={register}
id="honeypot"
key="honeypot"
type="text"
name="honeypot"
/>
</label>
<input type="submit" />
</div>
</form>
)}
</>
)
} |
I'm kinda new to github so I don't know if this is the correct way to suggest this.
I've written a working React Component (without any styles, as the dev chose pure css and I'll style with styled components).
It works as intended, with controlled forms, conditional Thanks component and all that React can offer.
The text was updated successfully, but these errors were encountered: