Skip to content

Commit

Permalink
Fix handling of available fields for conditional logic so that defaul…
Browse files Browse the repository at this point in the history
…t initial value is referencing the first available field instead of any field
  • Loading branch information
sc0ttkclark committed Sep 4, 2023
1 parent 3f67f86 commit 6c569c7
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions ui/js/dfv/src/fields/conditional-logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const NUMERIC_FIELD_TYPES = [
'number',
];

const RELATIONAL_FIELD_TYPES = [
'file',
'avatar',
'pick',
];

const FIELD_TYPES_WITH_NO_BLANK_COMPARISONS = [
'boolean',
];
Expand Down Expand Up @@ -61,6 +67,10 @@ const ConditionalLogic = ( {
conditional_logic_affected_field_name: affectedFieldName,
} = fieldConfig;

const allAvailableFields = currentPodAllFields.filter( ( field ) => {
return ! UNSUPPORTED_FIELD_TYPES.includes( field.type ) && field.name !== affectedFieldName;
} );

const [ conditions, setConditions ] = useState( {
action: 'show',
logic: 'any',
Expand Down Expand Up @@ -185,13 +195,21 @@ const ConditionalLogic = ( {
</div>

{ conditions.rules.map( ( rule, index ) => {
const ruleFieldObject = currentPodAllFields.find(
let ruleFieldObject = allAvailableFields.find(
( field ) => field.name === rule.field,
);

// Default the field object / rule to the first available field in the list if no field is selected.
if ( ! ruleFieldObject ) {
ruleFieldObject = allAvailableFields[ 0 ];

rule.field = ruleFieldObject.name;
}

const ruleFieldType = ruleFieldObject?.type;

const isNumericFieldType = NUMERIC_FIELD_TYPES.includes( ruleFieldType );
const isRelationalFieldType = RELATIONAL_FIELD_TYPES.includes( ruleFieldType );

return (
<div
Expand Down Expand Up @@ -224,6 +242,7 @@ const ConditionalLogic = ( {
<option
value={ field.name }
key={ field.name }
data-field-type={ field.type }
>
{ field.label }
</option>
Expand All @@ -238,8 +257,8 @@ const ConditionalLogic = ( {
value={ rule.compare }
onChange={ ( event ) => setRuleOption( index, 'compare', event.target.value ) }
>
<option value="=">{ __( 'is', 'pods' ) }</option>
<option value="!=">{ __( 'is not', 'pods' ) }</option>
<option value="=">{ isRelationalFieldType ? __( 'is (id/value)', 'pods' ) : __( 'is', 'pods' ) }</option>
<option value="!=">{ isRelationalFieldType ? __( 'is not (id/value)', 'pods' ) : __( 'is not', 'pods' ) }</option>

{ ! FIELD_TYPES_WITH_NO_BLANK_COMPARISONS.includes( ruleFieldType ) ? (
<>
Expand Down

0 comments on commit 6c569c7

Please sign in to comment.