pull/22493/head
zxhlyh 7 months ago
parent a923087b57
commit 498d8ab33c

@ -49,7 +49,7 @@ const BaseForm = ({
defaultValues, defaultValues,
}) })
const form: any = formFromProps || formFromHook const form: any = formFromProps || formFromHook
const { getFormValues } = useGetFormValues(form) const { getFormValues } = useGetFormValues(form, formSchemas)
const { getValidators } = useGetValidators() const { getValidators } = useGetValidators()
useImperativeHandle(ref, () => { useImperativeHandle(ref, () => {
@ -58,10 +58,10 @@ const BaseForm = ({
return form return form
}, },
getFormValues: (option) => { getFormValues: (option) => {
return getFormValues(formSchemas, option) return getFormValues(option)
}, },
} }
}, [form, formSchemas, getFormValues]) }, [form, getFormValues])
const renderField = useCallback((field: AnyFieldApi) => { const renderField = useCallback((field: AnyFieldApi) => {
const formSchema = formSchemas?.find(schema => schema.name === field.name) const formSchema = formSchemas?.find(schema => schema.name === field.name)

@ -1,17 +1,29 @@
import { useCallback } from 'react' import { useCallback } from 'react'
import type { AnyFormApi } from '@tanstack/react-form' import type { AnyFormApi } from '@tanstack/react-form'
import { useToastContext } from '@/app/components/base/toast' 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 { notify } = useToastContext()
const checkValidated = useCallback(() => { const checkValidated = useCallback(() => {
const allError = form?.getAllErrors() const allError = form?.getAllErrors()
const values = form.state.values
if (allError) { if (allError) {
const fields = allError.fields const fields = allError.fields
const errorArray = Object.keys(fields).reduce((acc: string[], key: string) => { 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] return [...acc, ...errors]
}, [] as string[]) }, [] as string[])
@ -28,7 +40,7 @@ export const useCheckValidated = (form: AnyFormApi) => {
} }
return true return true
}, [form, notify]) }, [form, notify, FormSchemas])
return { return {
checkValidated, checkValidated,

@ -7,11 +7,10 @@ import type {
} from '../types' } from '../types'
import { getTransformedValuesWhenSecretInputPristine } from '../utils' import { getTransformedValuesWhenSecretInputPristine } from '../utils'
export const useGetFormValues = (form: AnyFormApi) => { export const useGetFormValues = (form: AnyFormApi, formSchemas: FormSchema[]) => {
const { checkValidated } = useCheckValidated(form) const { checkValidated } = useCheckValidated(form, formSchemas)
const getFormValues = useCallback(( const getFormValues = useCallback((
formSchemas: FormSchema[],
{ {
needCheckValidatedValues, needCheckValidatedValues,
needTransformWhenSecretFieldIsPristine, needTransformWhenSecretFieldIsPristine,
@ -37,7 +36,7 @@ export const useGetFormValues = (form: AnyFormApi) => {
isCheckValidated: false, isCheckValidated: false,
} }
} }
}, [form, checkValidated]) }, [form, checkValidated, formSchemas])
return { return {
getFormValues, getFormValues,

@ -63,17 +63,17 @@ const OAuthClientSettings = ({
const handleConfirm = useCallback(async () => { const handleConfirm = useCallback(async () => {
if (doingActionRef.current) if (doingActionRef.current)
return return
const {
isCheckValidated,
values,
} = formRef.current?.getFormValues({
needCheckValidatedValues: true,
needTransformWhenSecretFieldIsPristine: true,
}) || { isCheckValidated: false, values: {} }
if (!isCheckValidated)
return
try { try {
const {
isCheckValidated,
values,
} = formRef.current?.getFormValues({
needCheckValidatedValues: true,
needTransformWhenSecretFieldIsPristine: true,
}) || { isCheckValidated: false, values: {} }
if (!isCheckValidated)
throw new Error('error')
const { const {
__oauth_client__, __oauth_client__,
...restValues ...restValues
@ -115,13 +115,14 @@ const OAuthClientSettings = ({
type: 'success', type: 'success',
message: t('common.api.actionSuccess'), message: t('common.api.actionSuccess'),
}) })
onClose?.()
invalidPluginCredentialInfo() invalidPluginCredentialInfo()
invalidPluginOAuthClientSchema() invalidPluginOAuthClientSchema()
} }
finally { finally {
handleSetDoingAction(false) handleSetDoingAction(false)
} }
}, [invalidPluginCredentialInfo, invalidPluginOAuthClientSchema, deletePluginOAuthCustomClient, notify, t, handleSetDoingAction]) }, [invalidPluginCredentialInfo, invalidPluginOAuthClientSchema, deletePluginOAuthCustomClient, notify, t, handleSetDoingAction, onClose])
const form = useForm({ const form = useForm({
defaultValues: editValues || defaultValues, defaultValues: editValues || defaultValues,
}) })

Loading…
Cancel
Save