diff --git a/src/process/filter/__tests__/filter.test.ts b/src/process/filter/__tests__/filter.test.ts index 78748da9..0a899e8e 100644 --- a/src/process/filter/__tests__/filter.test.ts +++ b/src/process/filter/__tests__/filter.test.ts @@ -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: [] }, }); }); @@ -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: [] }, }); }); @@ -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: [] }, }); }); diff --git a/src/process/filter/index.ts b/src/process/filter/index.ts index 07df7558..d1569f21 100644 --- a/src/process/filter/index.ts +++ b/src/process/filter/index.ts @@ -18,7 +18,7 @@ export const filterProcessSync: ProcessorFnSync = (context: FilterC value: { data: {} } }; break; - case 'array': + case 'nestedArray': scope.filter[absolutePath] = { compModelType: modelType, include: true, diff --git a/src/process/populate/index.ts b/src/process/populate/index.ts index ece6c9e3..f336106c 100644 --- a/src/process/populate/index.ts +++ b/src/process/populate/index.ts @@ -10,7 +10,7 @@ export const populateProcessSync: ProcessorFnSync = (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]; @@ -39,4 +39,4 @@ export const populateProcessInfo = { name: 'populate', shouldProcess: (context: PopulateContext) => true, processSync: populateProcessSync, -}; \ No newline at end of file +}; diff --git a/src/process/validation/rules/validateMultiple.ts b/src/process/validation/rules/validateMultiple.ts index 7b300768..bd09ccca 100644 --- a/src/process/validation/rules/validateMultiple.ts +++ b/src/process/validation/rules/validateMultiple.ts @@ -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) { diff --git a/src/utils/formUtil.ts b/src/utils/formUtil.ts index a07dbeb1..cf233f23 100644 --- a/src/utils/formUtil.ts +++ b/src/utils/formUtil.ts @@ -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 = { - 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' @@ -166,7 +181,8 @@ export const MODEL_TYPES_OF_KNOWN_COMPONENTS: Record = { 'button', 'datasource', 'sketchpad', - 'reviewpage' + 'reviewpage', + 'file', ], }; @@ -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; } } @@ -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'; @@ -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)) { @@ -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,