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

Commit 7fff59c

Browse files
author
Greg Rickaby
authored
Merge pull request #95 from WebDevStudios/feature/45-gravity-form-select-field
Feature/45 gravity form select field
2 parents b94848f + a4d2779 commit 7fff59c

File tree

11 files changed

+167
-0
lines changed

11 files changed

+167
-0
lines changed

components/atoms/Inputs/Checkbox/Checkbox.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import PropTypes from 'prop-types'
22
import {Field} from 'formik'
33

4+
/**
5+
* Render Checkbox component.
6+
*
7+
* @param {object} props Input props.
8+
* @param {string} props.className Input className.
9+
* @param {string|number} props.id Input id.
10+
* @param {string} props.label Input label.
11+
* @param {string} props.name Input name.
12+
* @return {Element} The Checkbox component.
13+
*/
414
export default function Checkbox({className, id, label, name}) {
515
return (
616
<div className={className}>

components/atoms/Inputs/CheckboxGroup/CheckboxGroup.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import PropTypes from 'prop-types'
22
import {ErrorMessage} from 'formik'
33
import Checkbox from '@/components/atoms/Inputs/Checkbox'
44

5+
/**
6+
* Render CheckboxGroup component.
7+
*
8+
* @param {object} props CheckboxGroup props.
9+
* @param {Array} props.checkboxes Array of checkbox data objects.
10+
* @param {string} props.className CheckboxGroup wrapper className.
11+
* @param {string|number} props.id CheckboxGroup id.
12+
* @param {string} props.label CheckboxGroup input label.
13+
* @param {string} props.description CheckboxGroup input name.
14+
* @return {Element} The CheckboxGroup component.
15+
*/
516
export default function CheckboxGroup({
617
checkboxes,
718
className,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {Field, ErrorMessage} from 'formik'
2+
import PropTypes from 'prop-types'
3+
import cn from 'classnames'
4+
import styles from './Select.module.css'
5+
6+
/**
7+
* Render the Select component.
8+
*
9+
* @author WebDevStudios
10+
* @param {object} props props.
11+
* @param {string} props.className Select wrapper className.
12+
* @param {string} props.description Select description.
13+
* @param {string} props.label Select input label.
14+
* @param {string} props.id Select input id.
15+
* @param {boolean} props.isRequired If input is required.
16+
* @param {Array} props.options Array of input options objects.
17+
* @return {Element} The Select component.
18+
*/
19+
export default function Select({
20+
className,
21+
description,
22+
id,
23+
isRequired,
24+
label,
25+
options
26+
}) {
27+
return (
28+
<div className={cn(styles.select, className)}>
29+
{label && <label>{label}</label>}
30+
<Field as="select" id={id} name={id} required={isRequired}>
31+
{!!options?.length > 0 &&
32+
options.map((option, key) => {
33+
const {text, value} = option
34+
35+
return (
36+
<option key={key} value={value}>
37+
{text}
38+
</option>
39+
)
40+
})}
41+
{description && <p>{description}</p>}
42+
</Field>
43+
<ErrorMessage name={id} />
44+
</div>
45+
)
46+
}
47+
48+
Select.propTypes = {
49+
className: PropTypes.string,
50+
description: PropTypes.string,
51+
id: PropTypes.string.isRequired,
52+
isRequired: PropTypes.bool,
53+
label: PropTypes.string,
54+
options: PropTypes.arrayOf(PropTypes.object)
55+
}
56+
57+
Select.defaultProps = {
58+
isRequired: false
59+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.select {
2+
& select {
3+
@apply block border;
4+
}
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './Select'

components/atoms/Inputs/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export {default as Checkbox} from './Checkbox'
22
export {default as CheckboxGroup} from './CheckboxGroup'
3+
export {default as Select} from './Select'
34
export {default as Text} from './Text'

components/molecules/GravityForm/Fields/Checkbox.js

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

6+
/**
7+
* Render GravityForms Checkbox field component.
8+
*
9+
* @param {object} props GravityForm Checkbox field props.
10+
* @param {string} props.className GravityForm field wrapper class.
11+
* @param {string} props.description GravityForm field description.
12+
* @param {Array} props.inputs Array of checkbox field input data.
13+
* @param {string|number} props.id GravityForm field id.
14+
* @param {string} props.label GravityForm field label.
15+
* @param {string} props.size GravityForm field size.
16+
* @param {boolean} props.visibility GravityForm field visibility.
17+
* @return {Element} The Checkbox component.
18+
*/
619
export default function Checkbox({
720
className,
821
description,

components/molecules/GravityForm/Fields/Fields.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export default function Fields({fields, setFormValidation}) {
5151
fieldToRender = <GfFields.Text {...field.node} key={id} />
5252
break
5353

54+
case 'select':
55+
fieldToRender = <GfFields.Select {...field.node} key={id} />
56+
break
57+
5458
case 'text':
5559
fieldToRender = <GfFields.Text {...field.node} key={id} />
5660
break
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import PropTypes from 'prop-types'
2+
import {getGfFieldId, getGfHiddenClassName} from '@/functions/gravityForms'
3+
import * as Input from '@/components/atoms/Inputs'
4+
import cn from 'classnames'
5+
6+
/**
7+
* Render GravityForms Checkbox field component.
8+
*
9+
* @param {object} props GravityForm Checkbox field props.
10+
* @param {string} props.className GravityForm field wrapper class.
11+
* @param {string} props.description GravityForm field description.
12+
* @param {string|number} props.id GravityForm field id.
13+
* @param {boolean} props.isRequired GravityForm field is required.
14+
* @param {string} props.label GravityForm field label.
15+
* @param {string} props.size GravityForm field size.
16+
* @param {Array} props.selectChoices GravityForm field selection options.
17+
* @param {boolean} props.visibility GravityForm field visibility.
18+
* @return {Element} The Checkbox component.
19+
*/
20+
export default function Select({
21+
className,
22+
description,
23+
id,
24+
isRequired,
25+
label,
26+
size,
27+
selectChoices,
28+
visibility
29+
}) {
30+
const fieldId = getGfFieldId(id)
31+
const isHiddenClass = getGfHiddenClassName(visibility)
32+
33+
return (
34+
<div
35+
className={cn(className, isHiddenClass) || null}
36+
field-size={size && `size-${size}`}
37+
>
38+
<Input.Select
39+
id={fieldId}
40+
isRequired={isRequired}
41+
description={description}
42+
label={label}
43+
options={selectChoices}
44+
/>
45+
</div>
46+
)
47+
}
48+
49+
Select.propTypes = {
50+
className: PropTypes.string,
51+
description: PropTypes.string,
52+
id: PropTypes.number.isRequired,
53+
isRequired: PropTypes.bool,
54+
label: PropTypes.string,
55+
selectChoices: PropTypes.arrayOf(PropTypes.object),
56+
size: PropTypes.string,
57+
visibility: PropTypes.string
58+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export {default} from './Fields'
22
export {default as Checkbox} from './Checkbox'
3+
export {default as Select} from './Select'
34
export {default as Text} from './Text'

0 commit comments

Comments
 (0)