fix: plugin input default value & validate (#2171)

* fix: plugin input default value & validate

* delete console
This commit is contained in:
heheer
2024-07-26 12:41:18 +08:00
committed by GitHub
parent 71d0093768
commit 2d1e53c3b5
3 changed files with 76 additions and 62 deletions

View File

@@ -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<string, any>
)
);
}, [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 (
<RenderPluginInput

View File

@@ -18,6 +18,7 @@ const JsonEditor = dynamic(() => 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 (
<Textarea
value={value}
defaultValue={defaultValue}
onChange={onChange}
isDisabled={isDisabled}
placeholder={t(placeholder as any)}
@@ -66,7 +69,7 @@ const RenderPluginInput = ({
isDisabled={isDisabled}
isInvalid={isInvalid}
>
<NumberInputField value={value} onChange={onChange} />
<NumberInputField value={value} onChange={onChange} defaultValue={defaultValue} />
<NumberInputStepper>
<NumberIncrementStepper />
<NumberDecrementStepper />
@@ -81,6 +84,7 @@ const RenderPluginInput = ({
onChange={onChange}
isDisabled={isDisabled}
isInvalid={isInvalid}
defaultChecked={defaultValue}
/>
);
}
@@ -93,6 +97,7 @@ const RenderPluginInput = ({
value={value}
onChange={onChange}
isInvalid={isInvalid}
defaultValue={defaultValue}
/>
);
})();