Support for incoming value modification (#10525)

This commit is contained in:
Charlie.Wei 2024-11-11 11:52:32 +08:00 committed by GitHub
parent 0587e24fdb
commit 55edd5047e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,3 +1,4 @@
import React, { useEffect, useState } from 'react'
import Button from '@/app/components/base/button' import Button from '@/app/components/base/button'
import Input from '@/app/components/base/input' import Input from '@/app/components/base/input'
import Textarea from '@/app/components/base/textarea' import Textarea from '@/app/components/base/textarea'
@ -32,20 +33,31 @@ const MarkdownForm = ({ node }: any) => {
// </form> // </form>
const { onSend } = useChatContext() const { onSend } = useChatContext()
const getFormValues = (children: any) => { const [formValues, setFormValues] = useState<{ [key: string]: any }>({})
const formValues: { [key: string]: any } = {}
children.forEach((child: any) => { useEffect(() => {
if (child.tagName === SUPPORTED_TAGS.INPUT) const initialValues: { [key: string]: any } = {}
formValues[child.properties.name] = child.properties.value node.children.forEach((child: any) => {
if (child.tagName === SUPPORTED_TAGS.TEXTAREA) if ([SUPPORTED_TAGS.INPUT, SUPPORTED_TAGS.TEXTAREA].includes(child.tagName))
formValues[child.properties.name] = child.properties.value initialValues[child.properties.name] = child.properties.value
}) })
return formValues setFormValues(initialValues)
}, [node.children])
const getFormValues = (children: any) => {
const values: { [key: string]: any } = {}
children.forEach((child: any) => {
if ([SUPPORTED_TAGS.INPUT, SUPPORTED_TAGS.TEXTAREA].includes(child.tagName))
values[child.properties.name] = formValues[child.properties.name]
})
return values
} }
const onSubmit = (e: any) => { const onSubmit = (e: any) => {
e.preventDefault() e.preventDefault()
const format = node.properties.dataFormat || DATA_FORMAT.TEXT const format = node.properties.dataFormat || DATA_FORMAT.TEXT
const result = getFormValues(node.children) const result = getFormValues(node.children)
if (format === DATA_FORMAT.JSON) { if (format === DATA_FORMAT.JSON) {
onSend?.(JSON.stringify(result)) onSend?.(JSON.stringify(result))
} }
@ -77,25 +89,22 @@ const MarkdownForm = ({ node }: any) => {
</label> </label>
) )
} }
if (child.tagName === SUPPORTED_TAGS.INPUT) { if (child.tagName === SUPPORTED_TAGS.INPUT && Object.values(SUPPORTED_TYPES).includes(child.properties.type)) {
if (Object.values(SUPPORTED_TYPES).includes(child.properties.type)) { return (
return ( <Input
<Input key={index}
key={index} type={child.properties.type}
type={child.properties.type} name={child.properties.name}
name={child.properties.name} placeholder={child.properties.placeholder}
placeholder={child.properties.placeholder} value={formValues[child.properties.name]}
value={child.properties.value} onChange={(e) => {
onChange={(e) => { setFormValues(prevValues => ({
e.preventDefault() ...prevValues,
child.properties.value = e.target.value [child.properties.name]: e.target.value,
}} }))
/> }}
) />
} )
else {
return <p key={index}>Unsupported input type: {child.properties.type}</p>
}
} }
if (child.tagName === SUPPORTED_TAGS.TEXTAREA) { if (child.tagName === SUPPORTED_TAGS.TEXTAREA) {
return ( return (
@ -103,10 +112,12 @@ const MarkdownForm = ({ node }: any) => {
key={index} key={index}
name={child.properties.name} name={child.properties.name}
placeholder={child.properties.placeholder} placeholder={child.properties.placeholder}
value={child.properties.value} value={formValues[child.properties.name]}
onChange={(e) => { onChange={(e) => {
e.preventDefault() setFormValues(prevValues => ({
child.properties.value = e.target.value ...prevValues,
[child.properties.name]: e.target.value,
}))
}} }}
/> />
) )