fix: add check for reset (#2226)

This commit is contained in:
heheer
2024-07-31 15:42:59 +08:00
committed by GitHub
parent 41da52d6ed
commit 8c7f4a3a30

View File

@@ -1,4 +1,4 @@
import React, { useEffect } from 'react'; import React, { useEffect, useMemo } from 'react';
import { Controller } from 'react-hook-form'; import { Controller } from 'react-hook-form';
import RenderPluginInput from './renderPluginInput'; import RenderPluginInput from './renderPluginInput';
import { Button, Flex } from '@chakra-ui/react'; import { Button, Flex } from '@chakra-ui/react';
@@ -6,6 +6,7 @@ import { useTranslation } from 'react-i18next';
import { useContextSelector } from 'use-context-selector'; import { useContextSelector } from 'use-context-selector';
import { PluginRunContext } from '../context'; import { PluginRunContext } from '../context';
import { WorkflowIOValueTypeEnum } from '@fastgpt/global/core/workflow/constants'; import { WorkflowIOValueTypeEnum } from '@fastgpt/global/core/workflow/constants';
import { isEqual } from 'lodash';
const RenderInput = () => { const RenderInput = () => {
const { pluginInputs, variablesForm, histories, onStartChat, onNewChat, onSubmit, isChatting } = const { pluginInputs, variablesForm, histories, onStartChat, onNewChat, onSubmit, isChatting } =
@@ -16,20 +17,25 @@ const RenderInput = () => {
control, control,
handleSubmit, handleSubmit,
reset, reset,
getValues,
formState: { errors } formState: { errors }
} = variablesForm; } = variablesForm;
useEffect(() => { const defaultFormValues = useMemo(() => {
reset( return pluginInputs.reduce(
pluginInputs.reduce(
(acc, input) => { (acc, input) => {
acc[input.key] = input.defaultValue; acc[input.key] = input.defaultValue;
return acc; return acc;
}, },
{} as Record<string, any> {} as Record<string, any>
)
); );
}, [pluginInputs, histories]); }, [pluginInputs]);
useEffect(() => {
if (isEqual(getValues(), defaultFormValues)) return;
reset(defaultFormValues);
}, [defaultFormValues, histories]);
const isDisabledInput = histories.length > 0; const isDisabledInput = histories.length > 0;
return ( return (