Skip to content

Commit 0d0bf06

Browse files
authored
fix: variablesinit (#5247)
1 parent 9860a7a commit 0d0bf06

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

projects/app/src/components/core/app/formRender/LabelAndForm.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { BoxProps } from '@chakra-ui/react';
22
import { Box, Flex } from '@chakra-ui/react';
33
import FormLabel from '@fastgpt/web/components/common/MyBox/FormLabel';
44
import QuestionTip from '@fastgpt/web/components/common/MyTooltip/QuestionTip';
5-
import React, { useMemo } from 'react';
5+
import React from 'react';
66
import type { UseFormReturn } from 'react-hook-form';
77
import { Controller } from 'react-hook-form';
88
import InputRender from '.';
@@ -46,14 +46,13 @@ const LabelAndFormRender = ({
4646
const { control } = variablesForm;
4747

4848
return (
49-
<Box _notLast={{ mb: 4 }} px={1}>
49+
<Box _notLast={{ mb: 4 }}>
5050
<Flex alignItems={'center'} mb={1}>
5151
<FormLabel required={required}>{label}</FormLabel>
5252
{placeholder && <QuestionTip ml={1} label={placeholder} />}
5353
</Flex>
5454

5555
<Controller
56-
key={formKey}
5756
control={control}
5857
name={formKey}
5958
rules={{

projects/app/src/components/core/app/formRender/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const variableInputTypeToInputType = (inputType: VariableInputEnum) => {
1111
if (inputType === VariableInputEnum.textarea) return InputTypeEnum.textarea;
1212
if (inputType === VariableInputEnum.numberInput) return InputTypeEnum.numberInput;
1313
if (inputType === VariableInputEnum.select) return InputTypeEnum.select;
14-
return InputTypeEnum.textarea;
14+
return InputTypeEnum.JSONEditor;
1515
};
1616

1717
// 节点输入类型(通常是一个 reference+一个 form input)

projects/app/src/components/core/chat/ChatContainer/ChatBox/components/VariableInputForm.tsx

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,21 @@ const VariableInput = ({
3737
[allVariableList, showExternalVariables]
3838
);
3939

40-
const { getValues, setValue } = variablesForm;
40+
const { getValues, setValue, reset } = variablesForm;
4141

42+
// Init variables and add default values
4243
useEffect(() => {
44+
const values = getValues();
45+
4346
allVariableList.forEach((item) => {
4447
const val = getValues(`variables.${item.key}`);
4548
if (item.defaultValue !== undefined && (val === undefined || val === null || val === '')) {
46-
setValue(`variables.${item.key}`, item.defaultValue);
49+
values.variables[item.key] = item.defaultValue;
4750
}
4851
});
49-
}, [allVariableList, getValues, setValue, variableList]);
52+
53+
reset(values);
54+
}, [allVariableList, getValues, reset, setValue, variableList]);
5055

5156
return (
5257
<Box py={3}>
@@ -90,6 +95,7 @@ const VariableInput = ({
9095
leftIcon={<MyIcon name={'core/chat/chatFill'} w={'16px'} />}
9196
size={'sm'}
9297
maxW={'100px'}
98+
mt={4}
9399
onClick={variablesForm.handleSubmit(() => {
94100
chatForm.setValue('chatStarted', true);
95101
})}
@@ -123,18 +129,17 @@ const VariableInput = ({
123129
/>
124130
))}
125131
{!chatStarted && (
126-
<Box>
127-
<Button
128-
leftIcon={<MyIcon name={'core/chat/chatFill'} w={'16px'} />}
129-
size={'sm'}
130-
maxW={'100px'}
131-
onClick={variablesForm.handleSubmit(() => {
132-
chatForm.setValue('chatStarted', true);
133-
})}
134-
>
135-
{t('chat:start_chat')}
136-
</Button>
137-
</Box>
132+
<Button
133+
leftIcon={<MyIcon name={'core/chat/chatFill'} w={'16px'} />}
134+
size={'sm'}
135+
maxW={'100px'}
136+
mt={4}
137+
onClick={variablesForm.handleSubmit(() => {
138+
chatForm.setValue('chatStarted', true);
139+
})}
140+
>
141+
{t('chat:start_chat')}
142+
</Button>
138143
)}
139144
</Card>
140145
</Box>

projects/app/src/components/core/chat/ChatContainer/ChatBox/components/VariablePopover.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ const VariablePopover = ({
2929
const hasExternalVariable = externalVariableList.length > 0;
3030
const hasVariable = variableList.length > 0;
3131

32-
const { getValues, setValue } = variablesForm;
32+
const { getValues, reset } = variablesForm;
3333

3434
useEffect(() => {
35+
const values = getValues();
3536
variables.forEach((item) => {
3637
const val = getValues(`variables.${item.key}`);
3738
if (item.defaultValue !== undefined && (val === undefined || val === null || val === '')) {
38-
setValue(`variables.${item.key}`, item.defaultValue);
39+
values.variables[item.key] = item.defaultValue;
3940
}
4041
});
41-
}, [variables]);
42+
reset(values);
43+
}, [getValues, reset, variables]);
4244

4345
return (
4446
<MyPopover
@@ -52,7 +54,7 @@ const VariablePopover = ({
5254
}
5355
>
5456
{({ onClose }) => (
55-
<Box p={4}>
57+
<Box p={4} maxH={'60vh'}>
5658
{hasExternalVariable && (
5759
<Box textAlign={'left'}>
5860
<Flex
@@ -83,7 +85,7 @@ const VariablePopover = ({
8385
)}
8486
{hasExternalVariable && hasVariable && <MyDivider h={'1px'} />}
8587
{hasVariable && (
86-
<Box textAlign={'left'}>
88+
<Box>
8789
{variableList.map((item) => (
8890
<LabelAndFormRender
8991
{...item}
@@ -97,11 +99,6 @@ const VariablePopover = ({
9799
))}
98100
</Box>
99101
)}
100-
<Flex w={'full'} justifyContent={'flex-end'}>
101-
<Button size={'sm'} onClick={onClose}>
102-
{t('common:Confirm')}
103-
</Button>
104-
</Flex>
105102
</Box>
106103
)}
107104
</MyPopover>

0 commit comments

Comments
 (0)