From 2d1e53c3b5cf5572974188dd8df321b5354dd95d Mon Sep 17 00:00:00 2001 From: heheer <71265218+newfish-cmyk@users.noreply.github.com> Date: Fri, 26 Jul 2024 12:41:18 +0800 Subject: [PATCH] fix: plugin input default value & validate (#2171) * fix: plugin input default value & validate * delete console --- .../PluginRunBox/components/RenderInput.tsx | 25 ++++- .../components/renderPluginInput.tsx | 7 +- .../SimpleApp/components/ToolSelectModal.tsx | 106 ++++++++---------- 3 files changed, 76 insertions(+), 62 deletions(-) diff --git a/projects/app/src/components/core/chat/ChatContainer/PluginRunBox/components/RenderInput.tsx b/projects/app/src/components/core/chat/ChatContainer/PluginRunBox/components/RenderInput.tsx index 42281e5d6a..a86db89356 100644 --- a/projects/app/src/components/core/chat/ChatContainer/PluginRunBox/components/RenderInput.tsx +++ b/projects/app/src/components/core/chat/ChatContainer/PluginRunBox/components/RenderInput.tsx @@ -1,10 +1,11 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { Controller } from 'react-hook-form'; import RenderPluginInput from './renderPluginInput'; import { Button, Flex } from '@chakra-ui/react'; import { useTranslation } from 'react-i18next'; import { useContextSelector } from 'use-context-selector'; import { PluginRunContext } from '../context'; +import { WorkflowIOValueTypeEnum } from '@fastgpt/global/core/workflow/constants'; const RenderInput = () => { const { pluginInputs, variablesForm, histories, onStartChat, onNewChat, onSubmit, isChatting } = @@ -14,8 +15,21 @@ const RenderInput = () => { const { control, handleSubmit, + reset, formState: { errors } } = variablesForm; + + useEffect(() => { + reset( + pluginInputs.reduce( + (acc, input) => { + acc[input.key] = input.defaultValue; + return acc; + }, + {} as Record + ) + ); + }, [pluginInputs, histories]); const isDisabledInput = histories.length > 0; return ( @@ -26,7 +40,14 @@ const RenderInput = () => { key={input.key} control={control} name={input.key} - rules={{ required: input.required }} + rules={{ + validate: (value) => { + if (input.valueType === WorkflowIOValueTypeEnum.boolean) { + return value !== undefined; + } + return !!value; + } + }} render={({ field: { onChange, value } }) => { return ( import('@fastgpt/web/components/common/Textarea const RenderPluginInput = ({ value, + defaultValue, onChange, label, description, @@ -30,6 +31,7 @@ const RenderPluginInput = ({ isInvalid }: { value: any; + defaultValue?: any; onChange: () => void; label: string; description?: string; @@ -48,6 +50,7 @@ const RenderPluginInput = ({ return (