Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit 03541f8

Browse files
author
Greg Rickaby
authored
Merge pull request #93 from WebDevStudios/feature/gravityform-demo
Feature/gravityform demo
2 parents f0a78e6 + 4adff4e commit 03541f8

File tree

7 files changed

+158
-23
lines changed

7 files changed

+158
-23
lines changed

components/molecules/GravityForm/Fields/Fields.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import * as GfFields from '.'
33
import {useEffect} from 'react'
44
import getGfFieldValidationSchema from '@/functions/gravityForms/getGfFieldValidationSchema'
55

6+
/**
7+
* Render the Fields component.
8+
*
9+
* @author WebDevStudios
10+
* @param {object} props The component attributes as props.
11+
* @param {Array} props.fields GravityForm fields data.
12+
* @param {Function} props.setFormValidation Callback function for setting formValidation state.
13+
* @return {Element} The Fields component.
14+
*/
615
export default function Fields({fields, setFormValidation}) {
716
/**
817
* Map through fields to setup form validation.
@@ -34,10 +43,22 @@ export default function Fields({fields, setFormValidation}) {
3443
fieldToRender = <GfFields.Checkbox {...field.node} key={id} />
3544
break
3645

46+
case 'email':
47+
fieldToRender = <GfFields.Text {...field.node} key={id} />
48+
break
49+
50+
case 'phone':
51+
fieldToRender = <GfFields.Text {...field.node} key={id} />
52+
break
53+
3754
case 'text':
3855
fieldToRender = <GfFields.Text {...field.node} key={id} />
3956
break
4057

58+
case 'website':
59+
fieldToRender = <GfFields.Text {...field.node} key={id} />
60+
break
61+
4162
default:
4263
fieldToRender = (
4364
<pre key={id}>

components/molecules/GravityForm/Fields/Text.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ import * as Input from '@/components/atoms/Inputs'
33
import {getGfFieldId, getGfHiddenClassName} from '@/functions/gravityForms'
44
import cn from 'classnames'
55

6+
/**
7+
* Render the GravityForm Text component.
8+
*
9+
* @author WebDevStudios
10+
* @param {object} props GravityForm field props.
11+
* @param {string} props.className Classname string.
12+
* @param {string} props.description GravityForm field description.
13+
* @param {boolean} props.enablePasswordInput GravityForm password enabled option.
14+
* @param {string} props.errorMessage GravityForm error message option.
15+
* @param {number} props.id GravityForm unique field id.
16+
* @param {boolean} props.isRequired GravityForm isRequired field.
17+
* @param {string} props.label GravityForm field label.
18+
* @param {string} props.size GravityForm field size.
19+
* @param {string} props.type GravityForm field type.
20+
* @param {boolean} props.visibility GravityForm visibility option.
21+
* @return {Element} The Text component.
22+
*/
623
export default function Text({
724
className,
825
description,
@@ -18,6 +35,32 @@ export default function Text({
1835
const fieldId = getGfFieldId(id)
1936
const isHiddenClass = getGfHiddenClassName(visibility)
2037

38+
/**
39+
* Convert type to an HTML input type.
40+
*
41+
* @param {string} type GravityForm field type.
42+
* @return {string} HTML input type.
43+
*/
44+
function modifyFieldType(type) {
45+
let inputType = type
46+
47+
if (enablePasswordInput && 'password') {
48+
inputType = 'password'
49+
}
50+
51+
if (type === 'phone') {
52+
inputType = 'tel'
53+
}
54+
55+
if (type === 'website') {
56+
inputType = 'url'
57+
}
58+
59+
return inputType
60+
}
61+
62+
const inputType = modifyFieldType(type)
63+
2164
return (
2265
<div
2366
className={cn(className, isHiddenClass) || null}
@@ -29,7 +72,7 @@ export default function Text({
2972
id={fieldId}
3073
isRequired={isRequired}
3174
label={label}
32-
type={(enablePasswordInput && 'password') || type}
75+
type={inputType}
3376
/>
3477
</div>
3578
)

components/molecules/GravityForm/GravityForm.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@ import Fields from './Fields'
44
import * as Yup from 'yup'
55
import getGfFieldId from '@/functions/gravityForms/getGfFieldId'
66
import {useState} from 'react'
7+
import styles from './GravityForm.module.css'
8+
import cn from 'classnames'
79

10+
/**
11+
* Render the GravityForm component.
12+
*
13+
* @param {object} props The GravityForm block attributes as props.
14+
* @param {object} props.formData GravityForm form data.
15+
* @param {string} props.formData.cssClass GravityForm form classname.
16+
* @param {object} props.formData.fields GravityForm form fields.
17+
* @param {number} props.formData.formId GravityForm form id.
18+
* @param {string} props.formData.title GravityForm form title.
19+
* @return {Element} The GravityForm component.
20+
*/
821
export default function GravityForm({
922
formData: {cssClass, fields, formId, title}
1023
}) {
@@ -15,8 +28,8 @@ export default function GravityForm({
1528
/**
1629
* Map field GravityForm ids and defaults to Object.
1730
*
18-
* @param {Array} fields Array of fields.
19-
* @return {Object} Default field values.
31+
* @param {Array} fields Array of fields.
32+
* @return {object} Default field values.
2033
*/
2134
function getFieldDefaults(fields) {
2235
const defaults = {}
@@ -43,7 +56,7 @@ export default function GravityForm({
4356

4457
return (
4558
<Form
46-
className={cssClass}
59+
className={cn(styles.gravityForm, cssClass)}
4760
formDefaults={fieldDefaults}
4861
id={formId && `gform-${formId}`}
4962
title={title}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.gravityForm {
2+
& > div {
3+
@apply mb-5;
4+
}
5+
}

functions/gravityForms/getGfFieldValidationSchema.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import ArraySchemaFactory from '@/functions/gravityForms/yupSchema/ArraySchemaFa
99
*
1010
* @author WebDevStudios
1111
* @see https://github.com/jquense/yup#api
12-
* @param {Object} fieldData GravityForm field props.
13-
* @return {Object} Schema validation for field.
12+
* @param {object} fieldData GravityForm field props.
13+
* @return {object} Schema validation for field.
1414
*/
1515
function getValidationSchemaByType(fieldData) {
1616
let schemaGetter = null
@@ -20,10 +20,22 @@ function getValidationSchemaByType(fieldData) {
2020
schemaGetter = new ArraySchemaFactory(fieldData).schema
2121
break
2222

23+
case 'email':
24+
schemaGetter = new StringSchemaFactory(fieldData).schema
25+
break
26+
27+
case 'phone':
28+
schemaGetter = new StringSchemaFactory(fieldData).schema
29+
break
30+
2331
case 'text':
2432
schemaGetter = new StringSchemaFactory(fieldData).schema
2533
break
2634

35+
case 'website':
36+
schemaGetter = new StringSchemaFactory(fieldData).schema
37+
break
38+
2739
default:
2840
return
2941
}
@@ -35,8 +47,8 @@ function getValidationSchemaByType(fieldData) {
3547
* Map props to validation schemas.
3648
*
3749
* @author WebDevStudios
38-
* @param {Object} fieldData GravityForm field props.
39-
* @return {Object} Schema validation for field.
50+
* @param {object} fieldData GravityForm field props.
51+
* @return {object} Schema validation for field.
4052
*/
4153
export default function getGfFieldValidationSchema(fieldData) {
4254
return {

functions/gravityForms/yupSchema/StringSchemaFactory.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as Yup from 'yup'
55
*
66
* @author WebDevStudios
77
* @see https://github.com/jquense/yup#string
8-
* @return {Object} Yup validationSchema Object.
8+
* @return {object} Yup validationSchema Object.
99
*/
1010
export default class StringSchemaFactory {
1111
/**
@@ -14,7 +14,7 @@ export default class StringSchemaFactory {
1414
* Note: wp-graphql-gravity-forms plugin may need to be replaced.
1515
*
1616
* @see https://github.com/harness-software/wp-graphql-gravity-forms
17-
* @param {Object} fieldData from GravityForm GraphQL data Object.
17+
* @param {object} fieldData from GravityForm GraphQL data Object.
1818
*/
1919
constructor(fieldData) {
2020
this.fieldData = fieldData
@@ -26,33 +26,35 @@ export default class StringSchemaFactory {
2626
* Combine multiple schemas into one.
2727
*
2828
* @see https://github.com/jquense/yup#mixedconcatschema-schema-schema
29-
* @return {Object} Combined Yup validationSchema Object.
29+
* @return {object} Combined Yup validationSchema Object.
3030
*/
3131
get schema() {
3232
return Yup.string()
33+
.concat(this.getEmailSchema())
3334
.concat(this.getMaxLengthSchema())
3435
.concat(this.getRequiredSchema())
36+
.concat(this.getUrlSchema())
3537
}
3638

3739
/**
3840
* Get Yup required field validaion.
3941
*
40-
* @see https://github.com/jquense/yup#stringrequiredmessage-string--function-schema
41-
* @return {Object} Yup validationSchema Object.
42+
* @see https://github.com/jquense/yup#stringemailmessage-string--function-schema
43+
* @return {object} Yup validationSchema Object.
4244
*/
43-
getRequiredSchema() {
44-
if (!this.fieldData?.isRequired) {
45+
getEmailSchema() {
46+
if (this.fieldData?.type !== 'email') {
4547
return
4648
}
4749

48-
return Yup.string().required('Required!')
50+
return Yup.string().email(`Must be a valid email.`)
4951
}
5052

5153
/**
5254
* Get Yup max line length validation.
5355
*
5456
* @see https://github.com/jquense/yup#stringmaxlimit-number--ref-message-string--function-schema
55-
* @return {Object} Yup validationSchema Object.
57+
* @return {object} Yup validationSchema Object.
5658
*/
5759
getMaxLengthSchema() {
5860
if (!this.fieldData?.maxLength) {
@@ -64,4 +66,32 @@ export default class StringSchemaFactory {
6466
`Must be ${this.fieldData.maxLength} characters or less`
6567
)
6668
}
69+
70+
/**
71+
* Get Yup required field validaion.
72+
*
73+
* @see https://github.com/jquense/yup#stringrequiredmessage-string--function-schema
74+
* @return {object} Yup validationSchema Object.
75+
*/
76+
getRequiredSchema() {
77+
if (!this.fieldData?.isRequired) {
78+
return
79+
}
80+
81+
return Yup.string().required('Required!')
82+
}
83+
84+
/**
85+
* Get Yup required field validaion.
86+
*
87+
* @see https://github.com/jquense/yup#stringurlmessage-string--function-schema
88+
* @return {object} Yup validationSchema Object.
89+
*/
90+
getUrlSchema() {
91+
if (this.fieldData?.type !== 'website') {
92+
return
93+
}
94+
95+
return Yup.string().url(`Must be a valid url "https://example.com".`)
96+
}
6797
}

pages/[...slug].js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@ import getPostTypeStaticProps from '@/api/wordpress/_global/getPostTypeStaticPro
33
import Layout from '@/components/common/Layout'
44
import PropTypes from 'prop-types'
55

6+
// TODO Remove BlockGravityForm once block support is added.
7+
// TODO Remove slug based BlockGravityForm from page render.
8+
import BlockGravityForm from '@/components/blocks/BlockGravityForm'
9+
610
// Define route post type.
711
const postType = 'page'
812

13+
/**
14+
* Render the Page component.
15+
*
16+
* @author WebDevStudios
17+
* @param {object} props The component attributes as props.
18+
* @param {object} props.post Post data from WordPress.
19+
* @return {Element} The Page component.
20+
*/
921
export default function Page({post}) {
1022
return (
1123
<Layout
@@ -35,6 +47,7 @@ export default function Page({post}) {
3547
/>
3648
</article>
3749
</section>
50+
{post.slug === 'form-demo' && <BlockGravityForm {...post?.blocks[0]} />}
3851
</div>
3952
</Layout>
4053
)
@@ -44,7 +57,7 @@ export default function Page({post}) {
4457
* Get post static paths.
4558
*
4659
* @author WebDevStudios
47-
* @return {Object} Object consisting of array of paths and fallback setting.
60+
* @return {object} Object consisting of array of paths and fallback setting.
4861
*/
4962
export async function getStaticPaths() {
5063
return await getPostTypeStaticPaths(postType)
@@ -53,11 +66,9 @@ export async function getStaticPaths() {
5366
/**
5467
* Get post static props.
5568
*
56-
* @param {Object} context Context for current post.
57-
* @param {Object} context.params Route parameters for current post.
58-
* @param {boolean} context.preview Whether requesting preview of post.
59-
* @param {Object} context.previewData Post preview data.
60-
* @return {Object} Post props.
69+
* @param {object} context Context for current post.
70+
* @param {object} context.params Route parameters for current post.
71+
* @return {object} Post props.
6172
*/
6273
export async function getStaticProps({params}) {
6374
return getPostTypeStaticProps(params, postType)

0 commit comments

Comments
 (0)