Skip to content

Commit

Permalink
Merge pull request #137 from formio/fix/FIO-8723_clearing_default_val…
Browse files Browse the repository at this point in the history
…ues_for_hidden_components

FIO-8723: Clear values from submission for hidden comp with clearOnHide flag
  • Loading branch information
brendanbond authored Sep 20, 2024
2 parents d7371a4 + ba95e46 commit e9f5f0e
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/process/__tests__/process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3133,7 +3133,7 @@ describe('Process Tests', () => {
processSync(context);

expect(context.data).to.deep.equal({
candidates:[{candidate:{data:{section6:{}}}}],
candidates:[{candidate:{data:{section6:{ "c":{}, "d":[]}}}}],
submit: true
});
});
Expand Down Expand Up @@ -3183,7 +3183,7 @@ describe('Process Tests', () => {
processSync(context);

expect(context.data).to.deep.equal({
candidates:[{candidate:{data:{section6:{}}}}],
candidates:[{candidate:{data:{}}}],
submit: true
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/process/clearHidden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const clearHiddenProcess: ProcessorFnSync<ClearHiddenScope> = (context) =

const shouldClearValueWhenHidden = !component.hasOwnProperty('clearOnHide') || component.clearOnHide;

if (shouldClearValueWhenHidden && (isConditionallyHidden || isParentHidden(component))) {
if (shouldClearValueWhenHidden && (isConditionallyHidden || isParentHidden(component) || component.hidden)) {
unset(data, path);
scope.clearHidden[path] = true;
}
Expand Down
5 changes: 3 additions & 2 deletions src/process/conditions/__tests__/conditions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ describe('Condition processor', () => {
form,
submission
);
expect(context.components[1]).to.haveOwnProperty('hidden');
expect(context.components[1].hidden).to.be.true;
expect(context.scope.conditionals).to.have.length(1);
expect(context.scope.conditionals?.[0].path).to.equal(form.components[1].key);
expect(context.scope.conditionals?.[0].conditionallyHidden).to.be.true;
});

it('Should not define a conditional component (that condition is based on selectBoxes value) as hidden', async () => {
Expand Down
3 changes: 0 additions & 3 deletions src/process/conditions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ export const conditionalProcess = (context: ConditionsContext, isHidden: Conditi
}

conditionalComp.conditionallyHidden = conditionalComp.conditionallyHidden || isHidden(context);
if (conditionalComp.conditionallyHidden) {
set(component, 'hidden', true);
}
};

export const customConditionProcess: ProcessorFn<ConditionsScope> = async (context: ConditionsContext) => {
Expand Down
18 changes: 6 additions & 12 deletions src/process/hideChildren.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,19 @@ export const hideChildrenProcessor: ProcessorFnSync<ConditionsScope> = (context)
const isConditionallyHidden = scope.conditionals?.find((cond) => {
return path === cond.path && cond.conditionallyHidden;
});
if (component.hidden && isConditionallyHidden) {

if (!scope.conditionals) {
scope.conditionals = [];
}

if (isConditionallyHidden || component.hidden) {
const info = componentInfo(component);
if (info.hasColumns || info.hasComps || info.hasRows) {
// If this is a container component, we need to make the mutation to all the child components as well.
eachComponentData([component], row, (comp: Component, data: any, compRow: any, compPath: string) => {
if (comp !== component) {
// the path set here is not the absolute path, but the path relative to the parent component
(scope as ConditionsScope).conditionals?.push({ path: getComponentPath(comp, compPath), conditionallyHidden: true });
set(comp, 'hidden', true);
}
});
}
} else if (component.hidden) {
const info = componentInfo(component);
if (info.hasColumns || info.hasComps || info.hasRows) {
// If this is a container component, we need to make the mutation to all the child components as well.
eachComponentData([component], row, (comp: Component, data: any, compRow: any, compPath: string) => {
if (comp !== component) {
set(comp, 'hidden', true);
}
});
}
Expand Down
2 changes: 0 additions & 2 deletions src/utils/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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 { componentInfo, eachComponentData, getComponentPath } from "./formUtil";

export const hasLogic = (context: LogicContext): boolean => {
const { component } = context;
Expand Down Expand Up @@ -70,7 +69,6 @@ export function setActionBooleanProperty(context: LogicContext, action: LogicAct
conditionallyHidden: !!component.hidden,
});
}
set(component, 'hidden', !!component.hidden);
}
return true;
}
Expand Down

0 comments on commit e9f5f0e

Please sign in to comment.