-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Making formspree-react (and core) compatible with new version of reca…
…ptcha v3 (#19) * adding better promise detection for extradata in react * adding ReCaptcha demo. Handling legacy error format * fixes to payment form / styles * better error handling * updated versions
1 parent
452ddc4
commit cc355c4
Showing
19 changed files
with
312 additions
and
144 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { useForm, ValidationError } from "@formspree/react"; | ||
import { useState } from "react"; | ||
|
||
import { | ||
GoogleReCaptchaProvider, | ||
useGoogleReCaptcha, | ||
} from "react-google-recaptcha-v3"; | ||
|
||
const ReCaptchaForm = () => { | ||
const { executeRecaptcha } = useGoogleReCaptcha(); | ||
const [failReCaptcha, setFailReCaptcha] = useState(false); | ||
const [state, handleSubmit] = useForm( | ||
process.env.REACT_APP_RECAPTCHA_FORM_ID as string, | ||
{ | ||
data: { | ||
"g-recaptcha-response": failReCaptcha | ||
? () => new Promise<string>((resolve) => resolve("Nonsense!")) | ||
: executeRecaptcha, | ||
}, | ||
} | ||
); | ||
|
||
return ( | ||
<div> | ||
{state && state.succeeded ? ( | ||
<h2>Your message has been sent successfully!</h2> | ||
) : ( | ||
<form onSubmit={handleSubmit}> | ||
<div className="block"> | ||
<label htmlFor="email">Email</label> | ||
<input id="email" type="email" name="email" /> | ||
<ValidationError | ||
field="email" | ||
prefix="Email" | ||
className="error" | ||
errors={state.errors} | ||
/> | ||
</div> | ||
<div className="block"> | ||
<label htmlFor="name">Name</label> | ||
<input id="name" type="name" name="name" /> | ||
</div> | ||
<div className="block"> | ||
<label className="forCheckbox" htmlFor="failRecaptcha"> | ||
Fail Recaptcha | ||
</label> | ||
<input | ||
id="failReCaptcha" | ||
type="checkbox" | ||
onChange={(ev) => { | ||
setFailReCaptcha(ev.target.checked); | ||
}} | ||
/> | ||
</div> | ||
<div className="block"> | ||
<ValidationError className="error" errors={state.errors} /> | ||
</div> | ||
<button type="submit" disabled={state.submitting}> | ||
{state.submitting ? "Submitting..." : "Submit"} | ||
</button> | ||
</form> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default () => ( | ||
<GoogleReCaptchaProvider | ||
reCaptchaKey={process.env.REACT_APP_RECAPTCHA_KEY as string} | ||
> | ||
<ReCaptchaForm /> | ||
</GoogleReCaptchaProvider> | ||
); |
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
Oops, something went wrong.
cc355c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
formspree-react-demo – ./
formspree-react-demo-git-main-formspree.vercel.app
react-demo.formspree.io
formspree-react.vercel.app
formspree-react-demo-formspree.vercel.app