Skip to content

Commit

Permalink
FIO-8512: fixed an issue where conditionally visible data inside layo…
Browse files Browse the repository at this point in the history
…ut components inside editGrid/dataGrid is unset on server side
  • Loading branch information
TanyaGashtold authored and lane-formio committed Jul 1, 2024
1 parent b42949c commit ba103c5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
56 changes: 55 additions & 1 deletion src/utils/__tests__/formUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
eachComponentDataAsync,
isComponentDataEmpty,
eachComponent,
eachComponentData
eachComponentData,
isLayoutComponent,
findComponent,
findComponents,
getComponent,
flattenComponents,
getComponentActualValue
} from "../formUtil";

describe('getContextualRowData', () => {
Expand Down Expand Up @@ -1484,3 +1490,51 @@ describe('eachComponentData', () => {
]);
});
});

describe('getComponentActualValue', () => {
it('Should return correct value for component inside inside panel inside editGrid', () => {
const component = {
label: 'Radio',
optionsLabelPosition: 'right',
inline: false,
tableView: false,
values: [
{ label: 'yes', value: 'yes', shortcut: '' },
{ label: 'no', value: 'no', shortcut: '' },
],
key: 'radio',
type: 'radio',
input: true,
path: 'editGrid.radio',
parent: {
collapsible: false,
key: 'panel',
type: 'panel',
label: 'Panel',
input: false,
tableView: false,
path: 'editGrid[0].panel',
parent: {
label: 'Edit Grid',
tableView: false,
rowDrafts: false,
key: 'editGrid',
type: 'editgrid',
path: 'editGrid',
displayAsTable: false,
input: true,
},
},
};
const compPath = 'editGrid.radio';
const data = {
editGrid: [{ radio: 'yes', textArea: 'test' }],
submit: true,
};
const row = { radio: 'yes', textArea: 'test' };

const value = getComponentActualValue(component, compPath, data, row);
expect(value).to.equal('yes');
});
});

11 changes: 7 additions & 4 deletions src/utils/conditions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConditionsContext, JSONConditional, LegacyConditional, SimpleConditional } from "types";
import JSONLogic, { EvaluatorFn, evaluate } from 'modules/jsonlogic';
import { getComponentActualValue } from "./formUtil";
import { EvaluatorFn, evaluate, JSONLogicEvaluator } from 'modules/jsonlogic';
import { getComponent, getComponentActualValue } from "./formUtil";
import { has, isObject, map, every, some, find, filter } from 'lodash';
const JSONLogicEvaluator = JSONLogic.evaluator;
import ConditionOperators from './operators';
Expand Down Expand Up @@ -96,7 +96,7 @@ export function checkJsonConditional(conditional: JSONConditional, context: Cond
* @returns
*/
export function checkSimpleConditional(conditional: SimpleConditional, context: ConditionsContext): boolean | null {
const { component, data, row, instance } = context;
const { component, data, row, instance, form } = context;
if (!conditional || !isSimpleConditional(conditional)) {
return null;
}
Expand All @@ -111,7 +111,10 @@ export function checkSimpleConditional(conditional: SimpleConditional, context:
// Ignore conditions if there is no component path.
return null;
}
const value = getComponentActualValue(component, conditionComponentPath, data, row);

const conditionComp = getComponent(form?.components || [], conditionComponentPath, true);
const value = conditionComp ? getComponentActualValue(conditionComp, conditionComponentPath, data, row) : null;

const ConditionOperator = ConditionOperators[operator];
return ConditionOperator
? new ConditionOperator().getResult({ value, comparedValue, instance, component, conditionComponentPath })
Expand Down
14 changes: 12 additions & 2 deletions src/utils/formUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,18 @@ export function getComponentActualValue(component: Component, compPath: string,
//
// a[0].b[2].c[3].d => a.b.c.d
//
if (component.parent?.path) {
const parentCompPath = component.parent?.path.replace(/\[[0-9]+\]/g, '');
let parentInputComponent: any = null;
let parent = component;

while (parent?.parent?.path && !parentInputComponent) {
parent = parent.parent;
if (parent.input) {
parentInputComponent = parent;
}
}

if (parentInputComponent) {
const parentCompPath = parentInputComponent.path.replace(/\[[0-9]+\]/g, '');
compPath = compPath.replace(parentCompPath, '');
compPath = trim(compPath, '. ');
}
Expand Down

0 comments on commit ba103c5

Please sign in to comment.