Skip to content

Commit f014928

Browse files
authored
Merge pull request #212 from formio/FIO-9508
FIO-9508: includeAll flag now works with nested components
2 parents 0271c99 + eff58e8 commit f014928

File tree

5 files changed

+109
-1
lines changed

5 files changed

+109
-1
lines changed

src/process/calculation/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const calculateProcessSync: ProcessorFnSync<CalculationScope> = (
2727
}
2828

2929
const calculationContext = (scope as FetchScope).fetched
30-
? {...context, data: {...data, ...(scope as FetchScope).fetched}}
30+
? { ...context, data: { ...data, ...(scope as FetchScope).fetched } }
3131
: context;
3232
const evalContextValue = evalContext
3333
? evalContext(normalizeContext(calculationContext))
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { expect } from 'chai';
2+
3+
import { eachComponentData } from '../eachComponentData';
4+
5+
describe('eachComponentData', function () {
6+
it('Should not iterate over each component in a nested component if includeAll=false and there is no data associated with the component', function () {
7+
const components = [
8+
{
9+
type: 'datagrid',
10+
key: 'dataGrid',
11+
label: 'Data Grid',
12+
input: true,
13+
components: [
14+
{
15+
key: 'textField',
16+
type: 'textfield',
17+
label: 'Text Field',
18+
input: true,
19+
},
20+
],
21+
},
22+
];
23+
24+
const data = {};
25+
26+
const rowResults: Map<string, any> = new Map();
27+
eachComponentData(
28+
components,
29+
data,
30+
(component, data, row, path) => {
31+
rowResults.set(path, component.key);
32+
},
33+
false,
34+
);
35+
expect(rowResults.size).to.equal(1);
36+
expect(rowResults.get('dataGrid')).to.deep.equal('dataGrid');
37+
});
38+
39+
it('Should iterate over each component in a nested component if includeAll=true and there is no data associated with the component', function () {
40+
const components = [
41+
{
42+
type: 'datagrid',
43+
key: 'dataGrid',
44+
label: 'Data Grid',
45+
input: true,
46+
components: [
47+
{
48+
key: 'textField',
49+
type: 'textfield',
50+
label: 'Text Field',
51+
input: true,
52+
},
53+
],
54+
},
55+
];
56+
57+
const data = {};
58+
59+
const rowResults: Map<string, any> = new Map();
60+
eachComponentData(
61+
components,
62+
data,
63+
(component, data, row, path) => {
64+
rowResults.set(path, component.key);
65+
},
66+
true,
67+
);
68+
expect(rowResults.size).to.equal(2);
69+
expect(rowResults.get('dataGrid')).to.deep.equal('dataGrid');
70+
expect(rowResults.get('dataGrid[0].textField')).to.deep.equal('textField');
71+
});
72+
});

src/utils/formUtil/eachComponentData.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ export const eachComponentData = (
8383
compPaths,
8484
);
8585
}
86+
} else if (includeAll) {
87+
eachComponentData(
88+
component.components,
89+
data,
90+
fn,
91+
includeAll,
92+
local,
93+
component,
94+
compPaths,
95+
);
8696
}
8797
resetComponentScope(component);
8898
return true;

src/utils/formUtil/eachComponentDataAsync.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ export const eachComponentDataAsync = async (
7777
compPaths,
7878
);
7979
}
80+
} else if (includeAll) {
81+
await eachComponentDataAsync(
82+
component.components,
83+
data,
84+
fn,
85+
includeAll,
86+
local,
87+
component,
88+
compPaths,
89+
);
8090
}
8191
resetComponentScope(component);
8292
return true;

src/utils/formUtil/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
pad,
1313
isPlainObject,
1414
isArray,
15+
isNumber,
1516
isEqual,
1617
isBoolean,
1718
omit,
@@ -335,6 +336,21 @@ export function getComponentPaths(
335336
};
336337
}
337338

339+
export function getStringFromComponentPath(path: string | string[]) {
340+
if (!isArray(path)) {
341+
return path;
342+
}
343+
let strPath = '';
344+
path.forEach((part, i) => {
345+
if (isNumber(part)) {
346+
strPath += `[${part}]`;
347+
} else {
348+
strPath += i === 0 ? part : `.${part}`;
349+
}
350+
});
351+
return strPath;
352+
}
353+
338354
export type ComponentMatch = {
339355
component: Component | undefined;
340356
paths: ComponentPaths | undefined;

0 commit comments

Comments
 (0)