diff --git a/src/process/calculation/index.ts b/src/process/calculation/index.ts index 1684f7a0..428ac2e8 100644 --- a/src/process/calculation/index.ts +++ b/src/process/calculation/index.ts @@ -1,6 +1,7 @@ import JSONLogic from 'modules/jsonlogic'; import { ProcessorFn, ProcessorFnSync, CalculationScope, CalculationContext, ProcessorInfo, FilterScope } from 'types'; import _set from 'lodash/set'; +import { getComponentKey } from 'utils/formUtil'; const Evaluator = JSONLogic.evaluator; export const shouldCalculate = (context: CalculationContext): boolean => { @@ -30,7 +31,7 @@ export const calculateProcessSync: ProcessorFnSync = (context: path, value: newValue }); - _set(row, component.key, newValue); + _set(row, getComponentKey(component), newValue); context.value = newValue; } return; diff --git a/src/process/conditions/index.ts b/src/process/conditions/index.ts index a9734f4b..5eebcfb2 100644 --- a/src/process/conditions/index.ts +++ b/src/process/conditions/index.ts @@ -1,7 +1,7 @@ import { ProcessorFn, ProcessorFnSync, ConditionsScope, ProcessorInfo, ConditionsContext, SimpleConditional, JSONConditional, LegacyConditional, SimpleConditionalConditions, Component, NestedComponent, FilterScope } from 'types'; import { Utils } from 'utils'; import unset from 'lodash/unset'; -import { componentInfo, getComponentPath } from 'utils/formUtil'; +import { componentInfo, getComponentKey, getComponentPath } from 'utils/formUtil'; import { checkCustomConditional, checkJsonConditional, @@ -108,14 +108,14 @@ export const conditionalProcess = (context: ConditionsContext, isHidden: Conditi Utils.eachComponentData([component], row, (comp: Component, data: any, compRow: any, compPath: string) => { scope.conditionals?.push({ path: getComponentPath(comp, compPath), conditionallyHidden: true }); if (!comp.hasOwnProperty('clearOnHide') || comp.clearOnHide) { - unset(compRow, comp.key); + unset(compRow, getComponentKey(comp)); } }); } else { scope.conditionals.push({ path, conditionallyHidden: true }); if (!component.hasOwnProperty('clearOnHide') || component.clearOnHide) { - unset(row, component.key); + unset(row, getComponentKey(component)); context.value = undefined; } } diff --git a/src/process/defaultValue/index.ts b/src/process/defaultValue/index.ts index 3b0cbf60..8883f3a3 100644 --- a/src/process/defaultValue/index.ts +++ b/src/process/defaultValue/index.ts @@ -2,6 +2,7 @@ import JSONLogic from 'modules/jsonlogic'; import { ProcessorFn, ProcessorFnSync, ConditionsScope, ProcessorInfo, DefaultValueContext, FilterScope } from 'types'; import has from 'lodash/has'; import set from 'lodash/set'; +import { getComponentKey } from 'utils/formUtil'; const Evaluator = JSONLogic.evaluator; export const hasCustomDefaultValue = (context: DefaultValueContext): boolean => { @@ -34,7 +35,7 @@ export const customDefaultValueProcessSync: ProcessorFnSync = ( return; } if (!scope.defaultValues) scope.defaultValues = []; - if (has(row, component.key)) { + if (has(row, getComponentKey(component))) { return; } let defaultValue = null; @@ -51,7 +52,7 @@ export const customDefaultValueProcessSync: ProcessorFnSync = ( }); } if (defaultValue !== null && defaultValue !== undefined) { - set(row, component.key, defaultValue); + set(row, getComponentKey(component), defaultValue); } }; @@ -65,7 +66,7 @@ export const serverDefaultValueProcessSync: ProcessorFnSync = ( return; } if (!scope.defaultValues) scope.defaultValues = []; - if (has(row, component.key)) { + if (has(row, getComponentKey(component))) { return; } let defaultValue = null; @@ -83,10 +84,7 @@ export const serverDefaultValueProcessSync: ProcessorFnSync = ( }); } if (defaultValue !== null && defaultValue !== undefined) { - set(row, component.key, defaultValue); - if ((scope as FilterScope).filter) { - set((scope as FilterScope).filter, path, defaultValue); - } + set(row, getComponentKey(component), defaultValue); context.value = defaultValue; } }; diff --git a/src/process/fetch/index.ts b/src/process/fetch/index.ts index ab96cb91..dfe9aafe 100644 --- a/src/process/fetch/index.ts +++ b/src/process/fetch/index.ts @@ -2,6 +2,7 @@ import { ProcessorFn, ProcessorInfo, FetchContext, FetchScope, FetchFn } from 't import get from 'lodash/get'; import set from 'lodash/set'; import { Evaluator } from 'utils'; +import { getComponentKey } from 'utils/formUtil'; export const shouldFetch = (context: FetchContext): boolean => { const { component } = context; @@ -64,13 +65,14 @@ export const fetchProcess: ProcessorFn = async (context: FetchContex const mapFunction = get(component, 'fetch.mapFunction'); // Set the row data of the fetched value. - set(row, component.key, mapFunction ? Evaluator.evaluate(mapFunction, { + const key = getComponentKey(component); + set(row, key, mapFunction ? Evaluator.evaluate(mapFunction, { ...evalContextValue, ...{responseData: result} }, 'value') : result); scope.fetched.push({ path, - value: get(row, component.key) + value: get(row, key) }); } catch (err: any) { diff --git a/src/process/processOne.ts b/src/process/processOne.ts index 7f0b0220..b5011a6a 100644 --- a/src/process/processOne.ts +++ b/src/process/processOne.ts @@ -1,15 +1,9 @@ import { get } from "lodash"; -import { CheckboxComponent, Component, ProcessorsContext, ProcessorType } from "types"; +import { Component, ProcessorsContext, ProcessorType } from "types"; +import { getComponentKey } from "utils/formUtil"; export function dataValue(component: Component, row: any) { - let key = component.key; - if ( - component.type === 'checkbox' && - component.inputType === 'radio' && - (component as CheckboxComponent).name - ) { - key = (component as CheckboxComponent).name; - } + const key = getComponentKey(component); return key ? get(row, key) : undefined; } diff --git a/src/utils/formUtil.ts b/src/utils/formUtil.ts index 035589e6..595e7cae 100644 --- a/src/utils/formUtil.ts +++ b/src/utils/formUtil.ts @@ -2,6 +2,7 @@ import { last, get, isEmpty, isNil, isObject } from "lodash"; import { AsyncComponentDataCallback, + CheckboxComponent, Component, ComponentDataCallback, DataObject, @@ -114,23 +115,24 @@ export function getModelType(component: any) { if ((component.input === false) || isComponentModelType(component, 'layout')) { return 'inherit'; } - if (component.key) { + if (getComponentKey(component)) { return 'value'; } return 'inherit'; } export function getComponentPath(component: Component, path: string) { - if (!component.key) { + const key = getComponentKey(component); + if (!key) { return path; } if (!path) { - return component.key; + return key; } - if (path.match(new RegExp(`${component.key}$`))) { + if (path.match(new RegExp(`${key}$`))) { return path; } - return (getModelType(component) === 'inherit') ? `${path}.${component.key}` : path; + return (getModelType(component) === 'inherit') ? `${path}.${key}` : path; } export function isComponentModelType(component: any, modelType: string) { @@ -146,7 +148,8 @@ export function isComponentNestedDataType(component: any) { export function componentPath(component: any, parentPath?: string) { parentPath = component.parentPath || parentPath; - if (!component.key) { + const key = getComponentKey(component); + if (!key) { // If the component does not have a key, then just always return the parent path. return parentPath; } @@ -155,15 +158,6 @@ export function componentPath(component: any, parentPath?: string) { if (component.path) { return component.path; } - - let key = component.key; - if ( - component.type === 'checkbox' && - component.inputType === 'radio' && - component.name - ) { - key = component.name; - } return parentPath ? `${parentPath}.${key}` : key; } @@ -274,8 +268,19 @@ export const eachComponentData = ( ); }; +export function getComponentKey(component: Component) { + if ( + component.type === 'checkbox' && + component.inputType === 'radio' && + (component as CheckboxComponent).name + ) { + return (component as CheckboxComponent).name; + } + return component.key; +} + export function getContextualRowPath(component: Component, path: string): string { - return path.replace(new RegExp(`\.?${component.key}$`), ''); + return path.replace(new RegExp(`\.?${getComponentKey(component)}$`), ''); } diff --git a/src/utils/logic.ts b/src/utils/logic.ts index 17ffb87a..cf4c73e8 100644 --- a/src/utils/logic.ts +++ b/src/utils/logic.ts @@ -3,6 +3,7 @@ import { checkCustomConditional, checkJsonConditional, checkLegacyConditional, c import { LogicActionCustomAction, LogicActionMergeComponentSchema, LogicActionProperty, LogicActionPropertyBoolean, LogicActionPropertyString, LogicActionValue } from "types/AdvancedLogic"; import { get, set, clone, isEqual, assign } from 'lodash'; import { evaluate, interpolate } from 'modules/jsonlogic'; +import { getComponentKey } from "./formUtil"; export const hasLogic = (context: LogicContext): boolean => { const { component } = context; @@ -79,7 +80,7 @@ export function setActionProperty(context: LogicContext, action: LogicActionProp export function setValueProperty(context: LogicContext, action: LogicActionValue) { const { component, row, value } = context; - const oldValue = get(row, component.key); + const oldValue = get(row, getComponentKey(component)); const newValue = evaluate({...context, value}, action.value, 'value', (evalContext: any) => { evalContext.value = clone(oldValue); }); @@ -88,7 +89,7 @@ export function setValueProperty(context: LogicContext, action: LogicActionValue !(component.clearOnHide && conditionallyHidden(context as ProcessorContext)) ) { context.value = newValue; - set(row, component.key, newValue); + set(row, getComponentKey(component), newValue); return true; } return false; @@ -96,7 +97,7 @@ export function setValueProperty(context: LogicContext, action: LogicActionValue export function setMergeComponentSchema(context: LogicContext, action: LogicActionMergeComponentSchema) { const { component, row } = context; - const oldValue = get(row, component.key); + const oldValue = get(row, getComponentKey(component)); const schema = evaluate({...context, value: {}}, action.schemaDefinition, 'schema', (evalContext: any) => { evalContext.value = clone(oldValue); });