mirror of
https://github.com/langgenius/dify.git
synced 2025-12-19 06:02:20 +00:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: lyzno1 <yuanyouhuilyz@gmail.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Hanqing Zhao <sherry9277@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Harry <xh001x@hotmail.com>
97 lines
2.4 KiB
TypeScript
97 lines
2.4 KiB
TypeScript
import { useAppForm } from '@/app/components/base/form'
|
|
import BaseField from '@/app/components/base/form/form-scenarios/base/field'
|
|
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
|
import Toast from '@/app/components/base/toast'
|
|
import { useCallback, useImperativeHandle } from 'react'
|
|
import type { ZodSchema } from 'zod'
|
|
import Header from './header'
|
|
|
|
type OptionsProps = {
|
|
initialData: Record<string, any>
|
|
configurations: BaseConfiguration[]
|
|
schema: ZodSchema
|
|
onSubmit: (data: Record<string, any>) => void
|
|
onPreview: () => void
|
|
ref: React.RefObject<any>
|
|
isRunning: boolean
|
|
}
|
|
|
|
const Form = ({
|
|
initialData,
|
|
configurations,
|
|
schema,
|
|
onSubmit,
|
|
onPreview,
|
|
ref,
|
|
isRunning,
|
|
}: OptionsProps) => {
|
|
const form = useAppForm({
|
|
defaultValues: initialData,
|
|
validators: {
|
|
onSubmit: ({ value }) => {
|
|
const result = schema.safeParse(value)
|
|
if (!result.success) {
|
|
const issues = result.error.issues
|
|
const firstIssue = issues[0]
|
|
const errorMessage = `"${firstIssue.path.join('.')}" ${firstIssue.message}`
|
|
Toast.notify({
|
|
type: 'error',
|
|
message: errorMessage,
|
|
})
|
|
return errorMessage
|
|
}
|
|
return undefined
|
|
},
|
|
},
|
|
onSubmit: ({ value }) => {
|
|
onSubmit(value)
|
|
},
|
|
})
|
|
|
|
useImperativeHandle(ref, () => {
|
|
return {
|
|
submit: () => {
|
|
form.handleSubmit()
|
|
},
|
|
}
|
|
}, [form])
|
|
|
|
const handleReset = useCallback(() => {
|
|
form.reset()
|
|
}, [form])
|
|
|
|
return (
|
|
<form
|
|
className='flex w-full flex-col rounded-lg border border-components-panel-border bg-components-panel-bg'
|
|
onSubmit={(e) => {
|
|
e.preventDefault()
|
|
e.stopPropagation()
|
|
form.handleSubmit()
|
|
}}
|
|
>
|
|
<form.Subscribe
|
|
selector={state => state.isDirty}
|
|
children={isDirty => (
|
|
<Header
|
|
onReset={handleReset}
|
|
resetDisabled={!isDirty}
|
|
onPreview={onPreview}
|
|
previewDisabled={isRunning}
|
|
/>
|
|
)}
|
|
/>
|
|
<div className='flex flex-col gap-3 border-t border-divider-subtle px-4 py-3'>
|
|
{configurations.map((config, index) => {
|
|
const FieldComponent = BaseField({
|
|
initialData,
|
|
config,
|
|
})
|
|
return <FieldComponent key={index} form={form} />
|
|
})}
|
|
</div>
|
|
</form>
|
|
)
|
|
}
|
|
|
|
export default Form
|