Skip to content

Commit ce3556c

Browse files
authored
fix: react-hook-form Controller crash with special characters in field names (#5715)
1 parent a6ba974 commit ce3556c

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

projects/app/src/components/core/chat/components/AIResponseBox.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,20 +217,31 @@ const RenderUserFormInteractive = React.memo(function RenderFormInput({
217217

218218
const defaultValues = useMemo(() => {
219219
if (interactive.type === 'userInput') {
220-
return interactive.params.inputForm?.reduce((acc: Record<string, any>, item) => {
221-
acc[item.label] = !!item.value ? item.value : item.defaultValue;
220+
return interactive.params.inputForm?.reduce((acc: Record<string, any>, item, index) => {
221+
acc[`field_${index}`] = !!item.value ? item.value : item.defaultValue;
222222
return acc;
223223
}, {});
224224
}
225225
return {};
226226
}, [interactive]);
227227

228-
const handleFormSubmit = useCallback((data: Record<string, any>) => {
229-
onSendPrompt({
230-
text: JSON.stringify(data),
231-
isInteractivePrompt: true
232-
});
233-
}, []);
228+
const handleFormSubmit = useCallback(
229+
(data: Record<string, any>) => {
230+
const finalData: Record<string, any> = {};
231+
interactive.params.inputForm?.forEach((item, index) => {
232+
const fieldName = `field_${index}`;
233+
if (fieldName in data) {
234+
finalData[item.label] = data[fieldName];
235+
}
236+
});
237+
238+
onSendPrompt({
239+
text: JSON.stringify(finalData),
240+
isInteractivePrompt: true
241+
});
242+
},
243+
[interactive.params.inputForm]
244+
);
234245

235246
return (
236247
<Flex flexDirection={'column'} gap={2} w={'250px'}>

projects/app/src/components/core/chat/components/Interactive/InteractiveComponents.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useCallback } from 'react';
2-
import { Box, Button, Flex } from '@chakra-ui/react';
2+
import { Box, Flex } from '@chakra-ui/react';
33
import { Controller, useForm, type UseFormHandleSubmit } from 'react-hook-form';
44
import Markdown from '@/components/Markdown';
55
import QuestionTip from '@fastgpt/web/components/common/MyTooltip/QuestionTip';
@@ -79,12 +79,12 @@ export const FormInputComponent = React.memo(function FormInputComponent({
7979
});
8080

8181
const RenderFormInput = useCallback(
82-
({ input }: { input: UserInputFormItemType }) => {
82+
({ input, index }: { input: UserInputFormItemType; index: number }) => {
8383
return (
8484
<Controller
8585
key={input.label}
8686
control={control}
87-
name={input.label}
87+
name={`field_${index}`}
8888
rules={{ required: input.required }}
8989
render={({ field: { onChange, value }, fieldState: { error } }) => {
9090
const inputType = nodeInputTypeToInputType([input.type]);
@@ -94,7 +94,7 @@ export const FormInputComponent = React.memo(function FormInputComponent({
9494
inputType={inputType}
9595
value={value}
9696
onChange={onChange}
97-
placeholder={input.description}
97+
placeholder={input.label}
9898
isDisabled={submitted}
9999
isInvalid={!!error}
100100
maxLength={input.maxLength}
@@ -114,14 +114,14 @@ export const FormInputComponent = React.memo(function FormInputComponent({
114114
<Box>
115115
<DescriptionBox description={description} />
116116
<Flex flexDirection={'column'} gap={3}>
117-
{inputForm.map((input) => (
117+
{inputForm.map((input, index) => (
118118
<Box key={input.label}>
119119
<Flex alignItems={'center'} mb={1}>
120120
{input.required && <Box color={'red.500'}>*</Box>}
121121
<FormLabel>{input.label}</FormLabel>
122122
{input.description && <QuestionTip ml={1} label={input.description} />}
123123
</Flex>
124-
<RenderFormInput input={input} />
124+
<RenderFormInput input={input} index={index} />
125125
</Box>
126126
))}
127127
</Flex>

0 commit comments

Comments
 (0)