pull/22493/head
zxhlyh 7 months ago
parent 2572e99a4b
commit d0ba7adf33

@ -6,7 +6,6 @@ import {
import type { import type {
AnyFieldApi, AnyFieldApi,
} from '@tanstack/react-form' } from '@tanstack/react-form'
import { useTranslation } from 'react-i18next'
import { useForm } from '@tanstack/react-form' import { useForm } from '@tanstack/react-form'
import type { import type {
FormRef, FormRef,
@ -19,7 +18,10 @@ import type {
BaseFieldProps, BaseFieldProps,
} from '.' } from '.'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import { useGetFormValues } from '@/app/components/base/form/hooks' import {
useGetFormValues,
useGetValidators,
} from '@/app/components/base/form/hooks'
export type BaseFormProps = { export type BaseFormProps = {
formSchemas?: FormSchema[] formSchemas?: FormSchema[]
@ -40,11 +42,11 @@ const BaseForm = ({
ref, ref,
disabled, disabled,
}: BaseFormProps) => { }: BaseFormProps) => {
const { t } = useTranslation()
const form = useForm({ const form = useForm({
defaultValues, defaultValues,
}) })
const { getFormValues } = useGetFormValues(form) const { getFormValues } = useGetFormValues(form)
const { getValidators } = useGetValidators()
useImperativeHandle(ref, () => { useImperativeHandle(ref, () => {
return { return {
@ -78,39 +80,21 @@ const BaseForm = ({
}, [formSchemas, fieldClassName, labelClassName, inputContainerClassName, inputClassName, disabled]) }, [formSchemas, fieldClassName, labelClassName, inputContainerClassName, inputClassName, disabled])
const renderFieldWrapper = useCallback((formSchema: FormSchema) => { const renderFieldWrapper = useCallback((formSchema: FormSchema) => {
const validators = getValidators(formSchema)
const { const {
name, name,
validators,
required,
} = formSchema } = formSchema
let mergedValidators = validators
if (required && !validators) {
mergedValidators = {
onMount: ({ value }: any) => {
if (!value)
return t('common.errorMsg.fieldRequired', { field: name })
},
onChange: ({ value }: any) => {
if (!value)
return t('common.errorMsg.fieldRequired', { field: name })
},
onBlur: ({ value }: any) => {
if (!value)
return t('common.errorMsg.fieldRequired', { field: name })
},
}
}
return ( return (
<form.Field <form.Field
key={name} key={name}
name={name} name={name}
validators={mergedValidators} validators={validators}
> >
{renderField} {renderField}
</form.Field> </form.Field>
) )
}, [renderField, form, t]) }, [renderField, form, getValidators])
if (!formSchemas?.length) if (!formSchemas?.length)
return null return null

@ -1,2 +1,3 @@
export * from './use-check-validated' export * from './use-check-validated'
export * from './use-get-form-values' export * from './use-get-form-values'
export * from './use-get-validators'

@ -7,27 +7,28 @@ export const useCheckValidated = (form: AnyFormApi) => {
const checkValidated = useCallback(() => { const checkValidated = useCallback(() => {
const allError = form?.getAllErrors() const allError = form?.getAllErrors()
console.log('allError', allError)
if (allError) {
const fields = allError.fields if (allError) {
const errorArray = Object.keys(fields).reduce((acc: string[], key: string) => { const fields = allError.fields
const errors: any[] = fields[key].errors const errorArray = Object.keys(fields).reduce((acc: string[], key: string) => {
const errors: any[] = fields[key].errors
return [...acc, ...errors]
}, [] as string[]) return [...acc, ...errors]
}, [] as string[])
if (errorArray.length) {
notify({ if (errorArray.length) {
type: 'error', notify({
message: errorArray[0], type: 'error',
}) message: errorArray[0],
return false })
} return false
return true
} }
return true return true
}
return true
}, [form, notify]) }, [form, notify])
return { return {

@ -0,0 +1,36 @@
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import type { FormSchema } from '../types'
export const useGetValidators = () => {
const { t } = useTranslation()
const getValidators = useCallback((formSchema: FormSchema) => {
const {
name,
validators,
required,
} = formSchema
let mergedValidators = validators
if (required && !validators) {
mergedValidators = {
onMount: ({ value }: any) => {
if (!value)
return t('common.errorMsg.fieldRequired', { field: name })
},
onChange: ({ value }: any) => {
if (!value)
return t('common.errorMsg.fieldRequired', { field: name })
},
onBlur: ({ value }: any) => {
if (!value)
return t('common.errorMsg.fieldRequired', { field: name })
},
}
}
return mergedValidators
}, [t])
return {
getValidators,
}
}

@ -41,6 +41,8 @@ export type FormOption = {
icon?: string icon?: string
} }
export type AnyValidators = FieldValidators<any, any, any, any, any, any, any, any, any, any>
export type FormSchema = { export type FormSchema = {
type: FormTypeEnum type: FormTypeEnum
name: string name: string
@ -55,7 +57,7 @@ export type FormSchema = {
placeholder?: string | TypeWithI18N placeholder?: string | TypeWithI18N
options?: FormOption[] options?: FormOption[]
labelClassName?: string labelClassName?: string
validators?: FieldValidators<any, any, any, any, any, any, any, any, any, any> validators?: AnyValidators
} }
export type FormValues = Record<string, any> export type FormValues = Record<string, any>

Loading…
Cancel
Save