Skip to content

Commit

Permalink
updates to model types, naming (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanbond authored Sep 10, 2024
1 parent 5030b37 commit 8a05d71
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/process/filter/__tests__/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ it("Should not filter empty array value for dataGrid component", async () => {
const context: any = generateProcessorContext(dataGridComp, data);
filterProcessSync(context);
expect(context.scope.filter).to.deep.equal({
dataGrid: { compModelType: "array", include: true, value: [] },
dataGrid: { compModelType: "nestedArray", include: true, value: [] },
});
});

Expand All @@ -50,7 +50,7 @@ it("Should not filter empty array value for editGrid component", async () => {
const context: any = generateProcessorContext(editGridComp, data);
filterProcessSync(context);
expect(context.scope.filter).to.deep.equal({
editGrid: { compModelType: "array", include: true, value: [] },
editGrid: { compModelType: "nestedArray", include: true, value: [] },
});
});

Expand All @@ -75,7 +75,7 @@ it("Should not filter empty array value for dataTable component", async () => {
const context: any = generateProcessorContext(dataTableComp, data);
filterProcessSync(context);
expect(context.scope.filter).to.deep.equal({
dataTable: { compModelType: "array", include: true, value: [] },
dataTable: { compModelType: "nestedArray", include: true, value: [] },
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/process/filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const filterProcessSync: ProcessorFnSync<FilterScope> = (context: FilterC
value: { data: {} }
};
break;
case 'array':
case 'nestedArray':
scope.filter[absolutePath] = {
compModelType: modelType,
include: true,
Expand Down
4 changes: 2 additions & 2 deletions src/process/populate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const populateProcessSync: ProcessorFnSync<PopulateScope> = (context: Pop
const compData: any = get(data, compDataPath);
if (!scope.populated) scope.populated = [];
switch (getModelType(component)) {
case 'array':
case 'nestedArray':
if (!compData || !compData.length) {
set(data, compDataPath, [{}]);
scope.row = get(data, compDataPath)[0];
Expand Down Expand Up @@ -39,4 +39,4 @@ export const populateProcessInfo = {
name: 'populate',
shouldProcess: (context: PopulateContext) => true,
processSync: populateProcessSync,
};
};
2 changes: 1 addition & 1 deletion src/process/validation/rules/validateMultiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const validateMultipleSync: RuleFnSync = (

const shouldBeMultipleArray = !!component.multiple;
const isRequired = !!component.validate?.required;
const underlyingValueShouldBeArray = getModelType(component) === 'array' || (isTagsComponent(component) && component.storeas === 'array');
const underlyingValueShouldBeArray = getModelType(component) === 'nestedArray' || (isTagsComponent(component) && component.storeas === 'array');
const valueIsArray = Array.isArray(value);

if (shouldBeMultipleArray) {
Expand Down
38 changes: 27 additions & 11 deletions src/utils/formUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,30 @@ export function uniqueName(name: string, template?: string, evalContext?: any) {
return uniqueName;
}

// TODO: bring in all component types (don't forget premium components)
export const MODEL_TYPES_OF_KNOWN_COMPONENTS: Record<string, string[]> = {
array: [
/**
* Defines model types for known components.
* For now, these will be the only model types supported by the @formio/core library.
*
* nestedArray: for components that store their data as an array and have nested components.
* array: for components that store their data as an array.
* dataObject: for components that store their data in a nested { data: {} } object.
* object: for components that store their data in an object.
* map: for components that store their data in a map.
* content: for components that do not store data.
* string: for components that store their data as a string.
* number: for components that store their data as a number.
* boolean: for components that store their data as a boolean.
* none: for components that do not store data and should not be included in the submission.
* any: for components that can store any type of data.
*
*/
export const MODEL_TYPES_OF_KNOWN_COMPONENTS = {
nestedArray: [
'datagrid',
'editgrid',
'datatable',
'dynamicWizard',
'tagpad',
'file',
],
dataObject: [
'form'
Expand Down Expand Up @@ -166,7 +181,8 @@ export const MODEL_TYPES_OF_KNOWN_COMPONENTS: Record<string, string[]> = {
'button',
'datasource',
'sketchpad',
'reviewpage'
'reviewpage',
'file',
],
};

Expand All @@ -177,9 +193,9 @@ export function getModelType(component: Component): keyof typeof MODEL_TYPES_OF_
}

// Otherwise, check for known component types.
for (const type in MODEL_TYPES_OF_KNOWN_COMPONENTS) {
for (const type of Object.keys(MODEL_TYPES_OF_KNOWN_COMPONENTS) as (keyof typeof MODEL_TYPES_OF_KNOWN_COMPONENTS)[]) {
if (MODEL_TYPES_OF_KNOWN_COMPONENTS[type].includes(component.type)) {
return type as keyof typeof MODEL_TYPES_OF_KNOWN_COMPONENTS;
return type;
}
}

Expand Down Expand Up @@ -216,11 +232,11 @@ export function getComponentPath(component: Component, path: string) {
if (path.match(new RegExp(`${key}$`))) {
return path;
}
return (getModelType(component) === 'layout') ? `${path}.${key}` : path;
return (getModelType(component) === 'none') ? `${path}.${key}` : path;
}

export function isComponentNestedDataType(component: any) {
return component.tree || getModelType(component) === 'array' ||
return component.tree || getModelType(component) === 'nestedArray' ||
getModelType(component) === 'dataObject' ||
getModelType(component) === 'object' ||
getModelType(component) === 'map';
Expand All @@ -244,7 +260,7 @@ export const componentDataPath = (component: any, parentPath: string, path: stri
if (getModelType(component) === 'dataObject') {
return `${path}.data`;
}
if (getModelType(component) === 'array') {
if (getModelType(component) === 'nestedArray') {
return `${path}[0]`;
}
if (isComponentNestedDataType(component)) {
Expand Down Expand Up @@ -415,7 +431,7 @@ export function componentInfo(component: any) {
const hasRows = component.rows && Array.isArray(component.rows);
const hasComps = component.components && Array.isArray(component.components);
const isContent = getModelType(component) === 'content';
const isLayout = getModelType(component) === 'layout';
const isLayout = getModelType(component) === 'none';
const isInput = !component.hasOwnProperty('input') || !!component.input;
return {
hasColumns,
Expand Down

0 comments on commit 8a05d71

Please sign in to comment.