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

Commit eedabcf

Browse files
author
Mike England
committed
Create getFieldDefaultByType for handling defaults
- Setup function - Setup helper function `getCheckboxDefaults`
1 parent 8efa0ec commit eedabcf

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

functions/gravityForms/getGfFormDefaults.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
import getGfFieldId from '@/functions/gravityForms/getGfFieldId'
22

3+
/**
4+
* Assign default values for GravityForm Checkboxes.
5+
*
6+
* @param {Array} checkboxes Array of checkbox data Objects.
7+
* @return {Array} Array of isSelected checkbox values.
8+
*/
9+
function getCheckboxDefaults(checkboxes) {
10+
let checkboxDefault = []
11+
12+
if (!checkboxes.length > 0) {
13+
return checkboxDefault
14+
}
15+
16+
/**
17+
* Assign values from isSelected checkboxes.
18+
*/
19+
checkboxes
20+
.filter((checkbox) => !!checkbox?.isSelected)
21+
.forEach((checkbox) => {
22+
checkboxDefault.push(checkbox.value)
23+
})
24+
25+
return checkboxDefault
26+
}
27+
28+
/**
29+
* Match field type with a default value.
30+
*
31+
* @author WebDevStudios
32+
* @param {object} fieldData GravityForm field props.
33+
* @return {any} Field default value.
34+
*/
35+
function getFieldDefaultByType(fieldData) {
36+
// Setup field data default
37+
let defaultValue = ''
38+
39+
switch (fieldData?.type) {
40+
case 'checkbox':
41+
defaultValue = getCheckboxDefaults(fieldData?.checkboxChoices)
42+
break
43+
44+
default:
45+
defaultValue = fieldData?.defaultValue
46+
}
47+
48+
return defaultValue
49+
}
50+
351
/**
452
* Map field GravityForm ids and defaults to Object.
553
*
@@ -19,7 +67,7 @@ export default function getGfFormDefaults(fields) {
1967
}
2068

2169
Object.assign(formDefaults, {
22-
[getGfFieldId(field.node.id)]: field.node.defaultValue
70+
[getGfFieldId(field.node.id)]: getFieldDefaultByType(field.node)
2371
})
2472
})
2573

0 commit comments

Comments
 (0)