Skip to content

Commit

Permalink
Fixing issues with the component key for non standard fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
travist committed Feb 7, 2024
1 parent c71c28c commit 0974c1a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 41 deletions.
3 changes: 2 additions & 1 deletion src/process/calculation/index.ts
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down Expand Up @@ -30,7 +31,7 @@ export const calculateProcessSync: ProcessorFnSync<CalculationScope> = (context:
path,
value: newValue
});
_set(row, component.key, newValue);
_set(row, getComponentKey(component), newValue);
context.value = newValue;
}
return;
Expand Down
6 changes: 3 additions & 3 deletions src/process/conditions/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/process/defaultValue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -34,7 +35,7 @@ export const customDefaultValueProcessSync: ProcessorFnSync<ConditionsScope> = (
return;
}
if (!scope.defaultValues) scope.defaultValues = [];
if (has(row, component.key)) {
if (has(row, getComponentKey(component))) {
return;
}
let defaultValue = null;
Expand All @@ -51,7 +52,7 @@ export const customDefaultValueProcessSync: ProcessorFnSync<ConditionsScope> = (
});
}
if (defaultValue !== null && defaultValue !== undefined) {
set(row, component.key, defaultValue);
set(row, getComponentKey(component), defaultValue);
}
};

Expand All @@ -65,7 +66,7 @@ export const serverDefaultValueProcessSync: ProcessorFnSync<ConditionsScope> = (
return;
}
if (!scope.defaultValues) scope.defaultValues = [];
if (has(row, component.key)) {
if (has(row, getComponentKey(component))) {
return;
}
let defaultValue = null;
Expand All @@ -83,10 +84,7 @@ export const serverDefaultValueProcessSync: ProcessorFnSync<ConditionsScope> = (
});
}
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;
}
};
Expand Down
6 changes: 4 additions & 2 deletions src/process/fetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,13 +65,14 @@ export const fetchProcess: ProcessorFn<FetchScope> = 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) {
Expand Down
12 changes: 3 additions & 9 deletions src/process/processOne.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
37 changes: 21 additions & 16 deletions src/utils/formUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { last, get, isEmpty, isNil, isObject } from "lodash";

import {
AsyncComponentDataCallback,
CheckboxComponent,
Component,
ComponentDataCallback,
DataObject,
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)}$`), '');
}


Expand Down
7 changes: 4 additions & 3 deletions src/utils/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
Expand All @@ -88,15 +89,15 @@ export function setValueProperty(context: LogicContext, action: LogicActionValue
!(component.clearOnHide && conditionallyHidden(context as ProcessorContext<ConditionsScope>))
) {
context.value = newValue;
set(row, component.key, newValue);
set(row, getComponentKey(component), newValue);
return true;
}
return false;
}

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);
});
Expand Down

0 comments on commit 0974c1a

Please sign in to comment.