Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIO-9508: includeAll flag now works with nested components #212

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading