mirror of https://github.com/langgenius/dify.git
fix: add default value for form in HITL
This commit is contained in:
parent
45c2167e0f
commit
6aa452d4e3
|
|
@ -1,4 +1,5 @@
|
|||
'use client'
|
||||
import type { ButtonProps } from '@/app/components/base/button'
|
||||
import type { FormInputItem, UserAction } from '@/app/components/workflow/nodes/human-input/types'
|
||||
import type { HumanInputFormError } from '@/service/use-share'
|
||||
import {
|
||||
|
|
@ -66,10 +67,10 @@ const FormContent = () => {
|
|||
return
|
||||
const initialInputs: Record<string, string> = {}
|
||||
formData.inputs.forEach((item) => {
|
||||
initialInputs[item.output_variable_name] = ''
|
||||
initialInputs[item.output_variable_name] = item.placeholder.type === 'variable' ? formData.resolved_placeholder_values[item.output_variable_name] || '' : item.placeholder.value
|
||||
})
|
||||
setInputs(initialInputs)
|
||||
}, [formData?.inputs])
|
||||
}, [formData?.inputs, formData?.resolved_placeholder_values])
|
||||
|
||||
// use immer
|
||||
const handleInputsChange = (name: string, value: string) => {
|
||||
|
|
@ -227,15 +228,14 @@ const FormContent = () => {
|
|||
formInputFields={formData.inputs}
|
||||
inputs={inputs}
|
||||
onInputChange={handleInputsChange}
|
||||
resolvedPlaceholderValues={formData.resolved_placeholder_values}
|
||||
/>
|
||||
))}
|
||||
<div className="flex flex-wrap gap-1 py-1">
|
||||
{formData.user_actions.map((action: any) => (
|
||||
{formData.user_actions.map((action: UserAction) => (
|
||||
<Button
|
||||
key={action.id}
|
||||
disabled={isSubmitting}
|
||||
variant={getButtonStyle(action.button_style) as any}
|
||||
variant={getButtonStyle(action.button_style) as ButtonProps['variant']}
|
||||
onClick={() => submit(action.id)}
|
||||
>
|
||||
{action.title}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ const ContentItem = ({
|
|||
content,
|
||||
formInputFields,
|
||||
inputs,
|
||||
resolvedPlaceholderValues,
|
||||
onInputChange,
|
||||
}: ContentItemProps) => {
|
||||
const isInputField = (field: string) => {
|
||||
|
|
@ -30,14 +29,6 @@ const ContentItem = ({
|
|||
return formInputFields.find(field => field.output_variable_name === fieldName)
|
||||
}, [formInputFields, fieldName])
|
||||
|
||||
const placeholder = useMemo(() => {
|
||||
if (!formInputField)
|
||||
return ''
|
||||
return formInputField.placeholder.type === 'variable'
|
||||
? resolvedPlaceholderValues?.[fieldName] || ''
|
||||
: formInputField.placeholder.value
|
||||
}, [formInputField, resolvedPlaceholderValues, fieldName])
|
||||
|
||||
if (!isInputField(content)) {
|
||||
return (
|
||||
<Markdown content={content} />
|
||||
|
|
@ -52,7 +43,6 @@ const ContentItem = ({
|
|||
{formInputField.type === 'paragraph' && (
|
||||
<Textarea
|
||||
className="h-[104px] sm:text-xs"
|
||||
placeholder={placeholder}
|
||||
value={inputs[fieldName]}
|
||||
onChange={(e) => { onInputChange(fieldName, e.target.value) }}
|
||||
/>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
'use client'
|
||||
import type { HumanInputFormProps } from './type'
|
||||
import type { ButtonProps } from '@/app/components/base/button'
|
||||
import type { UserAction } from '@/app/components/workflow/nodes/human-input/types'
|
||||
import * as React from 'react'
|
||||
import { useCallback, useState } from 'react'
|
||||
import Button from '@/app/components/base/button'
|
||||
|
|
@ -11,7 +13,7 @@ const HumanInputForm = ({
|
|||
onSubmit,
|
||||
}: HumanInputFormProps) => {
|
||||
const formToken = formData.form_token
|
||||
const defaultInputs = initializeInputs(formData.inputs)
|
||||
const defaultInputs = initializeInputs(formData.inputs, formData.resolved_placeholder_values || {})
|
||||
const contentList = splitByOutputVar(formData.form_content)
|
||||
const [inputs, setInputs] = useState(defaultInputs)
|
||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||
|
|
@ -36,17 +38,16 @@ const HumanInputForm = ({
|
|||
key={index}
|
||||
content={content}
|
||||
formInputFields={formData.inputs}
|
||||
resolvedPlaceholderValues={formData.resolved_placeholder_values || {}}
|
||||
inputs={inputs}
|
||||
onInputChange={handleInputsChange}
|
||||
/>
|
||||
))}
|
||||
<div className="flex flex-wrap gap-1 py-1">
|
||||
{formData.actions.map((action: any) => (
|
||||
{formData.actions.map((action: UserAction) => (
|
||||
<Button
|
||||
key={action.id}
|
||||
disabled={isSubmitting}
|
||||
variant={getButtonStyle(action.button_style) as any}
|
||||
variant={getButtonStyle(action.button_style) as ButtonProps['variant']}
|
||||
onClick={() => submit(formToken, action.id, inputs)}
|
||||
>
|
||||
{action.title}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,5 @@ export type ContentItemProps = {
|
|||
content: string
|
||||
formInputFields: FormInputItem[]
|
||||
inputs: Record<string, string>
|
||||
resolvedPlaceholderValues?: Record<string, string>
|
||||
onInputChange: (name: string, value: any) => void
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,11 +28,11 @@ export const splitByOutputVar = (content: string): string[] => {
|
|||
return parts.filter(part => part.length > 0)
|
||||
}
|
||||
|
||||
export const initializeInputs = (formInputs: FormInputItem[]) => {
|
||||
export const initializeInputs = (formInputs: FormInputItem[], defaultValues: Record<string, string> = {}) => {
|
||||
const initialInputs: Record<string, any> = {}
|
||||
formInputs.forEach((item) => {
|
||||
if (item.type === 'text-input' || item.type === 'paragraph')
|
||||
initialInputs[item.output_variable_name] = ''
|
||||
initialInputs[item.output_variable_name] = item.placeholder.type === 'variable' ? defaultValues[item.output_variable_name] || '' : item.placeholder.value
|
||||
else
|
||||
initialInputs[item.output_variable_name] = undefined
|
||||
})
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ const FormContent = ({
|
|||
onSubmit,
|
||||
}: Props) => {
|
||||
const { t } = useTranslation()
|
||||
const defaultInputs = initializeInputs(data.inputs)
|
||||
const defaultInputs = initializeInputs(data.inputs, data.resolved_placeholder_values || {})
|
||||
const contentList = splitByOutputVar(data.form_content)
|
||||
const [inputs, setInputs] = useState(defaultInputs)
|
||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||
|
|
@ -63,7 +63,6 @@ const FormContent = ({
|
|||
formInputFields={data.inputs}
|
||||
inputs={inputs}
|
||||
onInputChange={handleInputsChange}
|
||||
resolvedPlaceholderValues={data.resolved_placeholder_values}
|
||||
/>
|
||||
))}
|
||||
<div className="flex flex-wrap gap-1 py-1">
|
||||
|
|
|
|||
|
|
@ -796,7 +796,7 @@
|
|||
},
|
||||
"app/components/base/chat/chat/answer/human-input-content/human-input-form.tsx": {
|
||||
"ts/no-explicit-any": {
|
||||
"count": 4
|
||||
"count": 2
|
||||
}
|
||||
},
|
||||
"app/components/base/chat/chat/answer/human-input-content/type.ts": {
|
||||
|
|
|
|||
Loading…
Reference in New Issue