From 498d8ab33c76b2b77a86f21aa6c72115af8cf5cb Mon Sep 17 00:00:00 2001 From: zxhlyh Date: Tue, 15 Jul 2025 15:49:23 +0800 Subject: [PATCH] fix --- .../base/form/components/base/base-form.tsx | 6 +++--- .../base/form/hooks/use-check-validated.ts | 18 +++++++++++++--- .../base/form/hooks/use-get-form-values.ts | 7 +++---- .../authorize/oauth-client-settings.tsx | 21 ++++++++++--------- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/web/app/components/base/form/components/base/base-form.tsx b/web/app/components/base/form/components/base/base-form.tsx index ee78561250..640d474b19 100644 --- a/web/app/components/base/form/components/base/base-form.tsx +++ b/web/app/components/base/form/components/base/base-form.tsx @@ -49,7 +49,7 @@ const BaseForm = ({ defaultValues, }) const form: any = formFromProps || formFromHook - const { getFormValues } = useGetFormValues(form) + const { getFormValues } = useGetFormValues(form, formSchemas) const { getValidators } = useGetValidators() useImperativeHandle(ref, () => { @@ -58,10 +58,10 @@ const BaseForm = ({ return form }, getFormValues: (option) => { - return getFormValues(formSchemas, option) + return getFormValues(option) }, } - }, [form, formSchemas, getFormValues]) + }, [form, getFormValues]) const renderField = useCallback((field: AnyFieldApi) => { const formSchema = formSchemas?.find(schema => schema.name === field.name) diff --git a/web/app/components/base/form/hooks/use-check-validated.ts b/web/app/components/base/form/hooks/use-check-validated.ts index ce2e524e07..3a6b9860fe 100644 --- a/web/app/components/base/form/hooks/use-check-validated.ts +++ b/web/app/components/base/form/hooks/use-check-validated.ts @@ -1,17 +1,29 @@ import { useCallback } from 'react' import type { AnyFormApi } from '@tanstack/react-form' import { useToastContext } from '@/app/components/base/toast' +import type { FormSchema } from '@/app/components/base/form/types' -export const useCheckValidated = (form: AnyFormApi) => { +export const useCheckValidated = (form: AnyFormApi, FormSchemas: FormSchema[]) => { const { notify } = useToastContext() const checkValidated = useCallback(() => { const allError = form?.getAllErrors() + const values = form.state.values if (allError) { const fields = allError.fields const errorArray = Object.keys(fields).reduce((acc: string[], key: string) => { - const errors: any[] = fields[key].errors + const currentSchema = FormSchemas.find(schema => schema.name === key) + const { show_on = [] } = currentSchema || {} + const showOnValues = show_on.reduce((acc, condition) => { + acc[condition.variable] = values[condition.variable] + return acc + }, {} as Record) + const show = currentSchema?.show_on?.every((condition) => { + const conditionValue = showOnValues[condition.variable] + return conditionValue === condition.value + }) + const errors: any[] = show ? fields[key].errors : [] return [...acc, ...errors] }, [] as string[]) @@ -28,7 +40,7 @@ export const useCheckValidated = (form: AnyFormApi) => { } return true - }, [form, notify]) + }, [form, notify, FormSchemas]) return { checkValidated, diff --git a/web/app/components/base/form/hooks/use-get-form-values.ts b/web/app/components/base/form/hooks/use-get-form-values.ts index 918e6220bc..36100a724a 100644 --- a/web/app/components/base/form/hooks/use-get-form-values.ts +++ b/web/app/components/base/form/hooks/use-get-form-values.ts @@ -7,11 +7,10 @@ import type { } from '../types' import { getTransformedValuesWhenSecretInputPristine } from '../utils' -export const useGetFormValues = (form: AnyFormApi) => { - const { checkValidated } = useCheckValidated(form) +export const useGetFormValues = (form: AnyFormApi, formSchemas: FormSchema[]) => { + const { checkValidated } = useCheckValidated(form, formSchemas) const getFormValues = useCallback(( - formSchemas: FormSchema[], { needCheckValidatedValues, needTransformWhenSecretFieldIsPristine, @@ -37,7 +36,7 @@ export const useGetFormValues = (form: AnyFormApi) => { isCheckValidated: false, } } - }, [form, checkValidated]) + }, [form, checkValidated, formSchemas]) return { getFormValues, diff --git a/web/app/components/plugins/plugin-auth/authorize/oauth-client-settings.tsx b/web/app/components/plugins/plugin-auth/authorize/oauth-client-settings.tsx index 71bc0d29c9..6f80570627 100644 --- a/web/app/components/plugins/plugin-auth/authorize/oauth-client-settings.tsx +++ b/web/app/components/plugins/plugin-auth/authorize/oauth-client-settings.tsx @@ -63,17 +63,17 @@ const OAuthClientSettings = ({ const handleConfirm = useCallback(async () => { if (doingActionRef.current) return - const { - isCheckValidated, - values, - } = formRef.current?.getFormValues({ - needCheckValidatedValues: true, - needTransformWhenSecretFieldIsPristine: true, - }) || { isCheckValidated: false, values: {} } - if (!isCheckValidated) - return try { + const { + isCheckValidated, + values, + } = formRef.current?.getFormValues({ + needCheckValidatedValues: true, + needTransformWhenSecretFieldIsPristine: true, + }) || { isCheckValidated: false, values: {} } + if (!isCheckValidated) + throw new Error('error') const { __oauth_client__, ...restValues @@ -115,13 +115,14 @@ const OAuthClientSettings = ({ type: 'success', message: t('common.api.actionSuccess'), }) + onClose?.() invalidPluginCredentialInfo() invalidPluginOAuthClientSchema() } finally { handleSetDoingAction(false) } - }, [invalidPluginCredentialInfo, invalidPluginOAuthClientSchema, deletePluginOAuthCustomClient, notify, t, handleSetDoingAction]) + }, [invalidPluginCredentialInfo, invalidPluginOAuthClientSchema, deletePluginOAuthCustomClient, notify, t, handleSetDoingAction, onClose]) const form = useForm({ defaultValues: editValues || defaultValues, })