Skip to content

Commit

Permalink
Merge pull request #212 from formio/FIO-9508
Browse files Browse the repository at this point in the history
FIO-9508: includeAll flag now works with nested components
  • Loading branch information
lane-formio authored Jan 13, 2025
2 parents 0271c99 + eff58e8 commit f014928
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/process/calculation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const calculateProcessSync: ProcessorFnSync<CalculationScope> = (
}

const calculationContext = (scope as FetchScope).fetched
? {...context, data: {...data, ...(scope as FetchScope).fetched}}
? { ...context, data: { ...data, ...(scope as FetchScope).fetched } }
: context;
const evalContextValue = evalContext
? evalContext(normalizeContext(calculationContext))
Expand Down
72 changes: 72 additions & 0 deletions src/utils/formUtil/__tests__/eachComponentData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { expect } from 'chai';

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

describe('eachComponentData', function () {
it('Should not iterate over each component in a nested component if includeAll=false and there is no data associated with the component', function () {
const components = [
{
type: 'datagrid',
key: 'dataGrid',
label: 'Data Grid',
input: true,
components: [
{
key: 'textField',
type: 'textfield',
label: 'Text Field',
input: true,
},
],
},
];

const data = {};

const rowResults: Map<string, any> = new Map();
eachComponentData(
components,
data,
(component, data, row, path) => {
rowResults.set(path, component.key);
},
false,
);
expect(rowResults.size).to.equal(1);
expect(rowResults.get('dataGrid')).to.deep.equal('dataGrid');
});

it('Should iterate over each component in a nested component if includeAll=true and there is no data associated with the component', function () {
const components = [
{
type: 'datagrid',
key: 'dataGrid',
label: 'Data Grid',
input: true,
components: [
{
key: 'textField',
type: 'textfield',
label: 'Text Field',
input: true,
},
],
},
];

const data = {};

const rowResults: Map<string, any> = new Map();
eachComponentData(
components,
data,
(component, data, row, path) => {
rowResults.set(path, component.key);
},
true,
);
expect(rowResults.size).to.equal(2);
expect(rowResults.get('dataGrid')).to.deep.equal('dataGrid');
expect(rowResults.get('dataGrid[0].textField')).to.deep.equal('textField');
});
});
10 changes: 10 additions & 0 deletions src/utils/formUtil/eachComponentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ export const eachComponentData = (
compPaths,
);
}
} else if (includeAll) {
eachComponentData(
component.components,
data,
fn,
includeAll,
local,
component,
compPaths,
);
}
resetComponentScope(component);
return true;
Expand Down
10 changes: 10 additions & 0 deletions src/utils/formUtil/eachComponentDataAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ export const eachComponentDataAsync = async (
compPaths,
);
}
} else if (includeAll) {
await eachComponentDataAsync(
component.components,
data,
fn,
includeAll,
local,
component,
compPaths,
);
}
resetComponentScope(component);
return true;
Expand Down
16 changes: 16 additions & 0 deletions src/utils/formUtil/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
pad,
isPlainObject,
isArray,
isNumber,
isEqual,
isBoolean,
omit,
Expand Down Expand Up @@ -335,6 +336,21 @@ export function getComponentPaths(
};
}

export function getStringFromComponentPath(path: string | string[]) {
if (!isArray(path)) {
return path;
}
let strPath = '';
path.forEach((part, i) => {
if (isNumber(part)) {
strPath += `[${part}]`;
} else {
strPath += i === 0 ? part : `.${part}`;
}
});
return strPath;
}

export type ComponentMatch = {
component: Component | undefined;
paths: ComponentPaths | undefined;
Expand Down

0 comments on commit f014928

Please sign in to comment.