Skip to content

Commit

Permalink
Update useMultiForm
Browse files Browse the repository at this point in the history
  • Loading branch information
OKendigelyan committed Sep 6, 2024
1 parent 2365bb6 commit b44438a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
19 changes: 16 additions & 3 deletions packages/components/src/DynamicDisclosure/DynamicDisclosure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
type ReactElement,
createContext,
useContext,
useEffect,
useRef,
useState,
} from "react";
Expand All @@ -31,6 +32,7 @@ interface DynamicDisclosureContextType {
hasPrevious: boolean;
formValues: Record<string, any>;
allFormValues: Record<string, any>;
setAllFormValues: (values: Record<string, any>) => void;
}

const defaultContextValue = {
Expand All @@ -41,6 +43,7 @@ const defaultContextValue = {
hasPrevious: false,
formValues: {},
allFormValues: {},
setAllFormValues: () => {},
};

/**
Expand Down Expand Up @@ -106,12 +109,21 @@ export const useDynamicDisclosure = () => {
};

const currentItem = stackRef.current[currentIndex] || null;
const _allFormValues = stackRef.current
.map(item => item.formValues)
.reduce((acc, curr) => merge(acc, cloneDeep(curr)), {});

const [allFormValues, setAllFormValues] = useState<Record<string, any>>(_allFormValues);

useEffect(() => {
setAllFormValues(_allFormValues);
}, [currentIndex]);

// Note: be careful not to use the same form input names
// otherwise, the values will be overwritten with the ones from the latest form
const allFormValues = stackRef.current
.map(item => item.formValues)
.reduce((acc, curr) => merge(acc, cloneDeep(curr)), {});
// const allFormValues = stackRef.current
// .map(item => item.formValues)
// .reduce((acc, curr) => merge(acc, cloneDeep(curr)), {});

return {
isOpen: !!currentItem,
Expand All @@ -123,6 +135,7 @@ export const useDynamicDisclosure = () => {
formValues: currentItem?.formValues || {},
hasPrevious: stackRef.current.length > 1,
allFormValues,
setAllFormValues,
};
};

Expand Down
8 changes: 4 additions & 4 deletions packages/components/src/DynamicDisclosure/useMultiForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export const useMultiForm = <
const modalContext = useDynamicModalContext();
const drawerContext = useDynamicDrawerContext();

const formDefaultValues =
usedIn === "drawer" ? drawerContext?.formValues : modalContext?.formValues;
const currentContext = usedIn === "drawer" ? drawerContext : modalContext;

const form = useForm<TFieldValues, TContext, TTransformedValues>({
...props,
defaultValues: { ...props?.defaultValues, ...formDefaultValues } as any,
defaultValues: { ...props?.defaultValues, ...currentContext.allFormValues } as any,
});

const handleSubmit = (
Expand All @@ -31,7 +31,7 @@ export const useMultiForm = <
form.handleSubmit(
((submittedValues: any) => {
// update current context values
merge(formDefaultValues, submittedValues);
currentContext.setAllFormValues(merge(currentContext.allFormValues, submittedValues));
return onValid(submittedValues);
}) as any,
onInvalid
Expand Down

0 comments on commit b44438a

Please sign in to comment.