Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/totalityworks/wpgravitybundle
A component and a hook to simplify your headless WordPress React application using Gravity Forms.
https://github.com/totalityworks/wpgravitybundle
gravityforms reactjs wordpress wpgraphql
Last synced: 2 days ago
JSON representation
A component and a hook to simplify your headless WordPress React application using Gravity Forms.
- Host: GitHub
- URL: https://github.com/totalityworks/wpgravitybundle
- Owner: TotalityWorks
- License: mit
- Created: 2021-10-06T23:02:47.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-09-20T21:47:09.000Z (about 1 year ago)
- Last Synced: 2024-04-26T00:42:06.905Z (7 months ago)
- Topics: gravityforms, reactjs, wordpress, wpgraphql
- Language: TypeScript
- Homepage:
- Size: 1.82 MB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# WPGravityBundle
Headless WordPress is the future of WordPress, and WPGraphQL is at it's helm. With `WPGraphQL for Gravity Forms`, you can harness the power of Gravity Forms for your headless website. This package is built to complement the work done by Harness Software by giving you a React Component and a hook that will handle your form data and assemble your submission mutation for you.
## Installation
To install this package simply run
`npm install wpgravitybundle`
The following system is recommended:
- PHP 7.4+ || 8.0
- WordPress 5.4.1+
- WPGraphQL 1.9.0+
- Gravity Forms 2.5+ (Recommend: v2.6+)
- WPGraphQL for Gravity Forms 0.11.10
- WPGraphQL Upload - used for File UploadTechnically, this package simply needs data in JSON format so the above is not necessary. However, the shape of this data is dependant upon the shape that `WPGraphQL for Gravity Forms` gives you for your data and expects upon form submission.
## Use
To use this package, import the form component and the hook.
The GravityForm component needs two props: form and onSubmit.
Since this package is built to use with `WPGraphQL for Gravity Forms` WordPress plugin by Harness Software, it expects a specific json object for each field.
`form` should be the data.wpGfForm object you receive from your query.
The `onSubmit` prop is expecting a function.Internally, the package will map through your fields, display them, and handle state and validation.
The form values are passed into your onSubmit function when the form is submitted.
They are already preformatted so that all you need to do is pass the values into your mutation as an variable.The `useGravityFormMutation` hook needs your form data as well. It will use the form data to create the necessary GraphQL Mutation to submit the user's input. Just assign it a name and plug it into your preferred GraphQL client.
Example Use:
``` js
import React from 'react'
import { graphql } from 'gatsby'
import { useMutation } from "urql"
import Parser from 'html-react-parser'
import GravityForm, { useGravityFormMutation } from "wpgravitybundle"export default function Example({ data }) {
const form = data.wpGfForm
const buttonClass = "btn-primary"
const captchaSiteKey = process.env.GOOGLE_INVISIBLE_CAPTCHA_SITE_KEY
const captchaSecretKey = process.env.GOODLE_INVISIBLE_CAPTCHA_SECRET_KEYconst validation = [
{
id: 5, // id of the form field.
regex: /[0-9]+/g, // regex you would like to validate the field with.
message: "I don't like letters! Only numbers." // error message displayed if validation fails.
}
]
const captcha = {
captchaSiteKey,
captchaSecretKey,
type: "Invisible" // options are "Invisible" and "Checkbox".
}const gravityFormMutation = useGravityFormMutation(form)
const SubmitForm = `${gravityFormMutation}`const [{ data, fetching, error }, submitForm] = useMutation(SubmitForm)
const handleSubmit = (values: any) => {
return submitForm(values)
}if (fetching) {
returnLoading...
}if (error) {
returnThere was an error submitting the form: {error.message}
}if (data) {
returnConfirmation message here!
}return (
<>
{form && (
<>
>
)}
>
)
}export const query = graphql`
query {
wpGfForm(databaseId: {eq: 1}) {
id
databaseId
cssClass
formFields {
nodes {
... on WpTextField {
id
type
cssClass
isRequired
label
pageNumber
placeholder
size
}
}
}
submitButton {
text
type
}
}
}
`
```## Styling
Admittedly, more work could be done to make styling easier and this is something we want to do for future releases.
For now, the easiest way is to take advantage of `cssClass`. You can query for `cssClass` on the form and for every form field.
Each form field is wrapped in a ``. Certain fields have the cssClass set on the fieldset element instead.## Demos
Check out https://WPGravityBundle.com to see it in action.
![Example Form](./imgs/example.png)## Inspiration
This package was inspired by the work of Kellen Mace (https://twitter.com/kellenmace) of WP Engine and his public Github repository: https://github.com/kellenmace/gravity-forms-in-headless-wordpress-gatsby.
## Say Thank You
If you enjoy using WPGravityBundle and want to say thank you, you can buy me a coffee:
## About the Author.
I love WordPress and React.
I have been working with WordPress for 15 years, and React/Node for 4.
Headless WordPress is definitely a passion of mine, and I love looking for opportunities to contribute whenever I can.
If you have a WordPress/React project you'd like a hand with, please reach out to me at [email protected]!