|
1 | 1 | import { useFormikContext, setIn, useField } from "formik";
|
| 2 | +import clone from "lodash/clone"; |
2 | 3 | import get from "lodash/get";
|
3 | 4 | import React, { useCallback, useMemo } from "react";
|
4 | 5 |
|
5 | 6 | import GroupControls from "components/GroupControls";
|
6 | 7 | import { DropDown, DropDownOptionDataItem } from "components/ui/DropDown";
|
7 | 8 |
|
8 |
| -import { FormBlock, FormConditionItem } from "core/form/types"; |
9 |
| -import { isDefined } from "utils/common"; |
| 9 | +import { FormConditionItem } from "core/form/types"; |
10 | 10 |
|
11 | 11 | import { ConnectorFormValues } from "../../types";
|
| 12 | +import { setDefaultValues } from "../../useBuildForm"; |
12 | 13 | import styles from "./ConditionSection.module.scss";
|
13 | 14 | import { FormSection } from "./FormSection";
|
14 | 15 | import { GroupLabel } from "./GroupLabel";
|
@@ -39,22 +40,15 @@ export const ConditionSection: React.FC<ConditionSectionProps> = ({ formField, p
|
39 | 40 |
|
40 | 41 | const onOptionChange = useCallback(
|
41 | 42 | (selectedItem: DropDownOptionDataItem) => {
|
42 |
| - const newSelectedPath = formField.conditions[selectedItem.value]; |
| 43 | + const newSelectedFormBlock = formField.conditions[selectedItem.value]; |
43 | 44 |
|
44 |
| - const newValues = |
45 |
| - newSelectedPath._type === "formGroup" |
46 |
| - ? newSelectedPath.properties?.reduce( |
47 |
| - (acc: ConnectorFormValues, property: FormBlock) => |
48 |
| - property._type === "formItem" && isDefined(property.const) |
49 |
| - ? setIn(acc, property.path, property.const) |
50 |
| - : acc, |
51 |
| - values |
52 |
| - ) |
53 |
| - : values; |
| 45 | + const conditionValues = clone(get(values, path) || {}); |
| 46 | + conditionValues[formField.selectionKey] = formField.selectionConstValues[selectedItem.value]; |
| 47 | + setDefaultValues(newSelectedFormBlock, conditionValues, { respectExistingValues: true }); |
54 | 48 |
|
55 |
| - setValues(newValues); |
| 49 | + setValues(setIn(values, path, conditionValues)); |
56 | 50 | },
|
57 |
| - [values, formField.conditions, setValues] |
| 51 | + [formField.conditions, formField.selectionKey, formField.selectionConstValues, values, path, setValues] |
58 | 52 | );
|
59 | 53 |
|
60 | 54 | const options = useMemo(
|
|
0 commit comments