diff --git a/src/components/shared/Fields/Form/Form.tsx b/src/components/shared/Fields/Form/Form.tsx index b51e699376..cf870a92d6 100644 --- a/src/components/shared/Fields/Form/Form.tsx +++ b/src/components/shared/Fields/Form/Form.tsx @@ -129,6 +129,20 @@ const Form = ({ [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 ( diff --git a/src/components/v5/common/ActionSidebar/hooks/useActionFormProps.ts b/src/components/v5/common/ActionSidebar/hooks/useActionFormProps.ts index 11e446a85d..a78be2cc10 100644 --- a/src/components/v5/common/ActionSidebar/hooks/useActionFormProps.ts +++ b/src/components/v5/common/ActionSidebar/hooks/useActionFormProps.ts @@ -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'; @@ -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( async (formOptions, form) => { if (!formOptions) { @@ -82,7 +93,7 @@ const useActionFormProps = ( readonly: isReadonly, ...formOptions.options, }, - defaultValues: formOptionsWithDefaults, + defaultValues: actionFormProps.defaultValues, }); if (prevActionTypeRef.current === actionType) { @@ -99,6 +110,7 @@ const useActionFormProps = ( defaultValues, colony.nativeToken.tokenAddress, isReadonly, + actionFormProps.defaultValues, searchParams, setSearchParams, ],