Skip to content

Commit

Permalink
Handle single dropdown fields that are empty/-- Select One -- and set…
Browse files Browse the repository at this point in the history
… them as default value
  • Loading branch information
sc0ttkclark committed Sep 13, 2023
1 parent 1ce6e96 commit f5af2d0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Pods/Admin/Config/Field.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions ui/js/dfv/src/components/field-wrapper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,56 @@ export const FieldWrapper = ( props ) => {
setOptionValue( name, newValue );
};

// Maybe handle updating the value to the default option if empty.
if (
'pick' === field?.type
&& (
'single' === field?.pick_format_type
|| 'undefined' === typeof field.pick_format_type
)
&& (
'dropdown' === field?.pick_format_single
|| 'undefined' === typeof field.pick_format_single
)
&& ! Boolean( parseInt( field?.pick_show_select_text ) )
&& field?.required
) {
// Check if we have a field value.
let fieldValue = undefined;
let fieldVariation = undefined;

let variations = [
field.name,
'pods_meta_' + field.name,
'pods_field_' + field.name,
];

variations.every( variation => {
// Stop the loop if we found the value we were looking for.
if ( 'undefined' !== typeof allPodValues[ variation ] ) {
fieldValue = allPodValues[ variation ];
fieldVariation = variation;

return false;
}

// Continue to the next variation.
return true;
} );

if (
'' === fieldValue
|| (
'undefined' !== typeof field?.data['']
&& field?.data[''] === fieldValue
)
) {
setValue( field?.default ?? '' );

allPodValues[ fieldVariation ] = field?.default ?? '';
}
}

// Calculate dependencies.
const meetsConditionalLogic = useConditionalLogic(
field,
Expand Down

0 comments on commit f5af2d0

Please sign in to comment.