diff --git a/airbyte-webapp/src/views/Connector/ConnectorForm/components/Sections/ConditionSection.tsx b/airbyte-webapp/src/views/Connector/ConnectorForm/components/Sections/ConditionSection.tsx index 7512243b1500..333df432040f 100644 --- a/airbyte-webapp/src/views/Connector/ConnectorForm/components/Sections/ConditionSection.tsx +++ b/airbyte-webapp/src/views/Connector/ConnectorForm/components/Sections/ConditionSection.tsx @@ -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"; @@ -39,22 +40,15 @@ export const ConditionSection: React.FC = ({ 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( diff --git a/airbyte-webapp/src/views/Connector/ConnectorForm/useBuildForm.tsx b/airbyte-webapp/src/views/Connector/ConnectorForm/useBuildForm.tsx index 8960871d30b0..228e0fe68cb5 100644 --- a/airbyte-webapp/src/views/Connector/ConnectorForm/useBuildForm.tsx +++ b/airbyte-webapp/src/views/Connector/ConnectorForm/useBuildForm.tsx @@ -22,7 +22,7 @@ export interface BuildFormHook { validationSchema: AnySchema; } -function setDefaultValues( +export function setDefaultValues( formGroup: FormGroupItem, values: Record, options: { respectExistingValues: boolean } = { respectExistingValues: false }