fix: variablesinit (#5247)

This commit is contained in:
Archer
2025-07-18 00:25:12 +08:00
committed by GitHub
parent 9860a7a978
commit 0d0bf06307
4 changed files with 30 additions and 29 deletions

View File

@@ -2,7 +2,7 @@ import type { BoxProps } from '@chakra-ui/react';
import { Box, Flex } from '@chakra-ui/react'; import { Box, Flex } from '@chakra-ui/react';
import FormLabel from '@fastgpt/web/components/common/MyBox/FormLabel'; import FormLabel from '@fastgpt/web/components/common/MyBox/FormLabel';
import QuestionTip from '@fastgpt/web/components/common/MyTooltip/QuestionTip'; import QuestionTip from '@fastgpt/web/components/common/MyTooltip/QuestionTip';
import React, { useMemo } from 'react'; import React from 'react';
import type { UseFormReturn } from 'react-hook-form'; import type { UseFormReturn } from 'react-hook-form';
import { Controller } from 'react-hook-form'; import { Controller } from 'react-hook-form';
import InputRender from '.'; import InputRender from '.';
@@ -46,14 +46,13 @@ const LabelAndFormRender = ({
const { control } = variablesForm; const { control } = variablesForm;
return ( return (
<Box _notLast={{ mb: 4 }} px={1}> <Box _notLast={{ mb: 4 }}>
<Flex alignItems={'center'} mb={1}> <Flex alignItems={'center'} mb={1}>
<FormLabel required={required}>{label}</FormLabel> <FormLabel required={required}>{label}</FormLabel>
{placeholder && <QuestionTip ml={1} label={placeholder} />} {placeholder && <QuestionTip ml={1} label={placeholder} />}
</Flex> </Flex>
<Controller <Controller
key={formKey}
control={control} control={control}
name={formKey} name={formKey}
rules={{ rules={{

View File

@@ -11,7 +11,7 @@ export const variableInputTypeToInputType = (inputType: VariableInputEnum) => {
if (inputType === VariableInputEnum.textarea) return InputTypeEnum.textarea; if (inputType === VariableInputEnum.textarea) return InputTypeEnum.textarea;
if (inputType === VariableInputEnum.numberInput) return InputTypeEnum.numberInput; if (inputType === VariableInputEnum.numberInput) return InputTypeEnum.numberInput;
if (inputType === VariableInputEnum.select) return InputTypeEnum.select; if (inputType === VariableInputEnum.select) return InputTypeEnum.select;
return InputTypeEnum.textarea; return InputTypeEnum.JSONEditor;
}; };
// 节点输入类型(通常是一个 reference+一个 form input // 节点输入类型(通常是一个 reference+一个 form input

View File

@@ -37,16 +37,21 @@ const VariableInput = ({
[allVariableList, showExternalVariables] [allVariableList, showExternalVariables]
); );
const { getValues, setValue } = variablesForm; const { getValues, setValue, reset } = variablesForm;
// Init variables and add default values
useEffect(() => { useEffect(() => {
const values = getValues();
allVariableList.forEach((item) => { allVariableList.forEach((item) => {
const val = getValues(`variables.${item.key}`); const val = getValues(`variables.${item.key}`);
if (item.defaultValue !== undefined && (val === undefined || val === null || val === '')) { if (item.defaultValue !== undefined && (val === undefined || val === null || val === '')) {
setValue(`variables.${item.key}`, item.defaultValue); values.variables[item.key] = item.defaultValue;
} }
}); });
}, [allVariableList, getValues, setValue, variableList]);
reset(values);
}, [allVariableList, getValues, reset, setValue, variableList]);
return ( return (
<Box py={3}> <Box py={3}>
@@ -90,6 +95,7 @@ const VariableInput = ({
leftIcon={<MyIcon name={'core/chat/chatFill'} w={'16px'} />} leftIcon={<MyIcon name={'core/chat/chatFill'} w={'16px'} />}
size={'sm'} size={'sm'}
maxW={'100px'} maxW={'100px'}
mt={4}
onClick={variablesForm.handleSubmit(() => { onClick={variablesForm.handleSubmit(() => {
chatForm.setValue('chatStarted', true); chatForm.setValue('chatStarted', true);
})} })}
@@ -123,18 +129,17 @@ const VariableInput = ({
/> />
))} ))}
{!chatStarted && ( {!chatStarted && (
<Box>
<Button <Button
leftIcon={<MyIcon name={'core/chat/chatFill'} w={'16px'} />} leftIcon={<MyIcon name={'core/chat/chatFill'} w={'16px'} />}
size={'sm'} size={'sm'}
maxW={'100px'} maxW={'100px'}
mt={4}
onClick={variablesForm.handleSubmit(() => { onClick={variablesForm.handleSubmit(() => {
chatForm.setValue('chatStarted', true); chatForm.setValue('chatStarted', true);
})} })}
> >
{t('chat:start_chat')} {t('chat:start_chat')}
</Button> </Button>
</Box>
)} )}
</Card> </Card>
</Box> </Box>

View File

@@ -29,16 +29,18 @@ const VariablePopover = ({
const hasExternalVariable = externalVariableList.length > 0; const hasExternalVariable = externalVariableList.length > 0;
const hasVariable = variableList.length > 0; const hasVariable = variableList.length > 0;
const { getValues, setValue } = variablesForm; const { getValues, reset } = variablesForm;
useEffect(() => { useEffect(() => {
const values = getValues();
variables.forEach((item) => { variables.forEach((item) => {
const val = getValues(`variables.${item.key}`); const val = getValues(`variables.${item.key}`);
if (item.defaultValue !== undefined && (val === undefined || val === null || val === '')) { if (item.defaultValue !== undefined && (val === undefined || val === null || val === '')) {
setValue(`variables.${item.key}`, item.defaultValue); values.variables[item.key] = item.defaultValue;
} }
}); });
}, [variables]); reset(values);
}, [getValues, reset, variables]);
return ( return (
<MyPopover <MyPopover
@@ -52,7 +54,7 @@ const VariablePopover = ({
} }
> >
{({ onClose }) => ( {({ onClose }) => (
<Box p={4}> <Box p={4} maxH={'60vh'}>
{hasExternalVariable && ( {hasExternalVariable && (
<Box textAlign={'left'}> <Box textAlign={'left'}>
<Flex <Flex
@@ -83,7 +85,7 @@ const VariablePopover = ({
)} )}
{hasExternalVariable && hasVariable && <MyDivider h={'1px'} />} {hasExternalVariable && hasVariable && <MyDivider h={'1px'} />}
{hasVariable && ( {hasVariable && (
<Box textAlign={'left'}> <Box>
{variableList.map((item) => ( {variableList.map((item) => (
<LabelAndFormRender <LabelAndFormRender
{...item} {...item}
@@ -97,11 +99,6 @@ const VariablePopover = ({
))} ))}
</Box> </Box>
)} )}
<Flex w={'full'} justifyContent={'flex-end'}>
<Button size={'sm'} onClick={onClose}>
{t('common:Confirm')}
</Button>
</Flex>
</Box> </Box>
)} )}
</MyPopover> </MyPopover>