This commit is contained in:
zxhlyh 2025-07-15 15:49:23 +08:00
parent a923087b57
commit 498d8ab33c
4 changed files with 32 additions and 20 deletions

View File

@ -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)

View File

@ -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<string, any>)
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,

View File

@ -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,

View File

@ -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,
})