Skip to content

Commit

Permalink
FIO-9021: fixed eachComponentData iteration for nested forms
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-formio committed Nov 22, 2024
1 parent 8c0bf26 commit abca676
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
54 changes: 54 additions & 0 deletions src/utils/formUtil/__tests__/eachComponentData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { expect } from 'chai';

import { eachComponentData } from '../eachComponentData';
import { Component } from 'types';

describe('eachComponentData', function () {
const components = [
{
key: 'form',
type: 'form',
order: 1,
input: true,
components: [
{
type: 'textfield',
key: 'textField1',
order: 2,
},
{
type: 'textfield',
key: 'textField2',
order: 3,
},
],
},
{
key: 'textField',
type: 'textfield',
order: 4,
input: true,
},
];

const data = {
form: {
data: {
textField1: 'textField1Value',
textField2: 'textField2Value',
},
metadata: {},
},
textField: 'textFieldValue',
submit: true,
};

it('Should iterate through nested form components in the right order', function () {
let n = 1;

eachComponentData(components, data, (component: Component) => {
expect((component as any).order).to.equal(n);
n += 1;
});
});
});
4 changes: 2 additions & 2 deletions src/utils/formUtil/eachComponentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ export const eachComponentData = (
}
if (getModelType(component) === 'dataObject') {
// No need to bother processing all the children data if there is no data for this form or the reference value has not been loaded.
const nestedFormValue: any = get(data, component.path);
const nestedFormValue: any = get(data, compPath);
const noReferenceAttached =
nestedFormValue?._id && isEmpty(nestedFormValue.data) && !has(nestedFormValue, 'form');
const shouldProcessNestedFormData = nestedFormValue?._id
? !noReferenceAttached
: has(data, component.path);
: has(data, compPath);
if (shouldProcessNestedFormData) {
// For nested forms, we need to reset the "data" and "path" objects for all of the children components, and then re-establish the data when it is done.
const childPath: string = componentDataPath(component, path, compPath);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/formUtil/eachComponentDataAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ export const eachComponentDataAsync = async (
}
if (getModelType(component) === 'dataObject') {
// No need to bother processing all the children data if there is no data for this form or the reference value has not been loaded.
const nestedFormValue: any = get(data, component.path);
const nestedFormValue: any = get(data, compPath);
const noReferenceAttached =
nestedFormValue?._id && isEmpty(nestedFormValue.data) && !has(nestedFormValue, 'form');
const shouldProcessNestedFormData = nestedFormValue?._id
? !noReferenceAttached
: has(data, component.path);
: has(data, compPath);
if (shouldProcessNestedFormData) {
// For nested forms, we need to reset the "data" and "path" objects for all of the children components, and then re-establish the data when it is done.
const childPath: string = componentDataPath(component, path, compPath);
Expand Down

0 comments on commit abca676

Please sign in to comment.