Skip to content

Commit

Permalink
fill in all default values on switch (#21059)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Reuter authored Jan 6, 2023
1 parent f346dc8 commit 4e4800b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { useFormikContext, setIn, useField } from "formik";
import clone from "lodash/clone";
import get from "lodash/get";
import React, { useCallback, useMemo } from "react";

import GroupControls from "components/GroupControls";
import { DropDown, DropDownOptionDataItem } from "components/ui/DropDown";

import { FormBlock, FormConditionItem } from "core/form/types";
import { isDefined } from "utils/common";
import { FormConditionItem } from "core/form/types";

import { ConnectorFormValues } from "../../types";
import { setDefaultValues } from "../../useBuildForm";
import styles from "./ConditionSection.module.scss";
import { FormSection } from "./FormSection";
import { GroupLabel } from "./GroupLabel";
Expand Down Expand Up @@ -39,22 +40,15 @@ export const ConditionSection: React.FC<ConditionSectionProps> = ({ formField, p

const onOptionChange = useCallback(
(selectedItem: DropDownOptionDataItem) => {
const newSelectedPath = formField.conditions[selectedItem.value];
const newSelectedFormBlock = formField.conditions[selectedItem.value];

const newValues =
newSelectedPath._type === "formGroup"
? newSelectedPath.properties?.reduce(
(acc: ConnectorFormValues, property: FormBlock) =>
property._type === "formItem" && isDefined(property.const)
? setIn(acc, property.path, property.const)
: acc,
values
)
: values;
const conditionValues = clone(get(values, path) || {});
conditionValues[formField.selectionKey] = formField.selectionConstValues[selectedItem.value];
setDefaultValues(newSelectedFormBlock, conditionValues, { respectExistingValues: true });

setValues(newValues);
setValues(setIn(values, path, conditionValues));
},
[values, formField.conditions, setValues]
[formField.conditions, formField.selectionKey, formField.selectionConstValues, values, path, setValues]
);

const options = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface BuildFormHook {
validationSchema: AnySchema;
}

function setDefaultValues(
export function setDefaultValues(
formGroup: FormGroupItem,
values: Record<string, unknown>,
options: { respectExistingValues: boolean } = { respectExistingValues: false }
Expand Down

0 comments on commit 4e4800b

Please sign in to comment.