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

Commit 9c52fa2

Browse files
author
Mike England
committed
Add getSelectDefaults
Setup select field defaults based on field options with isSelected value. Note: There can only be one isSelected set to true.
1 parent d7cc3f7 commit 9c52fa2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

functions/gravityForms/getGfFormDefaults.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@ function getCheckboxDefaults(checkboxes) {
2525
return checkboxDefault
2626
}
2727

28+
/**
29+
* Assign default values for GravityForm Select.
30+
*
31+
* @param {Array} options Array of select option data Objects.
32+
* @return {string} Select option value that is selected.
33+
*/
34+
function getSelectDefaults(options) {
35+
let selectDefault = ''
36+
37+
if (!options.length > 0) {
38+
return selectDefault
39+
}
40+
41+
/**
42+
* Get first option with isSelected property.
43+
*/
44+
const selectedOption = [].filter((option) => !!option.isSelected).shift()
45+
46+
if (selectedOption) {
47+
selectDefault = selectedOption.shift().value
48+
}
49+
50+
return selectDefault
51+
}
52+
2853
/**
2954
* Match field type with a default value.
3055
*
@@ -41,6 +66,10 @@ function getFieldDefaultByType(fieldData) {
4166
defaultValue = getCheckboxDefaults(fieldData?.checkboxChoices)
4267
break
4368

69+
case 'select':
70+
defaultValue = getSelectDefaults(fieldData?.selectChoices)
71+
break
72+
4473
default:
4574
defaultValue = fieldData?.defaultValue
4675
}

0 commit comments

Comments
 (0)