Skip to content

Commit

Permalink
fix: correctly update action form values
Browse files Browse the repository at this point in the history
  • Loading branch information
rumzledz committed Nov 5, 2024
1 parent e463315 commit ca797f9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/components/shared/Fields/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ const Form = <FormData extends FieldValues>({
[handleSubmit, onSubmit, formHelpers, onError],
);

// Separate concern for handling defaultValues updates
useEffect(() => {
const initializeForm = async () => {
const resolvedDefaultValues =
typeof defaultValues === 'function'
? await defaultValues()
: defaultValues;

formHelpers.reset(resolvedDefaultValues);
};

initializeForm();
}, [defaultValues, formHelpers]);

return (
<AdditionalFormOptionsContextProvider value={{ readonly }}>
<FormProvider {...formHelpers}>
Expand Down
16 changes: 14 additions & 2 deletions src/components/v5/common/ActionSidebar/hooks/useActionFormProps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { noop } from 'lodash';
import { useCallback, useRef, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useSearchParams } from 'react-router-dom';

import { type Action } from '~constants/actions.ts';
Expand Down Expand Up @@ -33,6 +33,17 @@ const useActionFormProps = (
const { colony } = useColonyContext();
const [searchParams, setSearchParams] = useSearchParams();

// This ensures that actionFormProps always receives the freshest defaultValues updates
useEffect(() => {
if (defaultValues) {
setActionFormProps((state) => ({
...state,
actionType: defaultValues.actionType,
defaultValues,
}));
}
}, [defaultValues]);

const getFormOptions = useCallback<ActionFormBaseProps['getFormOptions']>(
async (formOptions, form) => {
if (!formOptions) {
Expand Down Expand Up @@ -82,7 +93,7 @@ const useActionFormProps = (
readonly: isReadonly,
...formOptions.options,
},
defaultValues: formOptionsWithDefaults,
defaultValues: actionFormProps.defaultValues,
});

if (prevActionTypeRef.current === actionType) {
Expand All @@ -99,6 +110,7 @@ const useActionFormProps = (
defaultValues,
colony.nativeToken.tokenAddress,
isReadonly,
actionFormProps.defaultValues,
searchParams,
setSearchParams,
],
Expand Down

0 comments on commit ca797f9

Please sign in to comment.