From 7d82331fcef2d1ed2c68a2a91485da0f1f6ebfed Mon Sep 17 00:00:00 2001 From: xiamiao Date: Thu, 25 Jul 2024 17:03:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E7=B1=BB=E5=9E=8B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ui/form/src/use-form-field.ts | 27 ++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/ui/form/src/use-form-field.ts b/packages/ui/form/src/use-form-field.ts index d07e3194d..79fa1bd66 100644 --- a/packages/ui/form/src/use-form-field.ts +++ b/packages/ui/form/src/use-form-field.ts @@ -2,7 +2,14 @@ import { useEffect, useCallback, useMemo } from 'react' import { FormFieldPath, FormRuleModel, FormRuleType } from './types' import { useFormContext } from './context' import { isArrayNonEmpty } from '@hi-ui/type-assertion' -import Validater, { Rules } from 'async-validator' +import Validater, { + InternalRuleItem, + Rules, + RuleItem, + ValidateOption, + Values as ValidateValues, + Value, +} from 'async-validator' import { normalizeArray } from '@hi-ui/array-utils' import { isValidField, stringify } from './utils' @@ -27,7 +34,7 @@ export const useFormField = (props: UseFormFieldProps) => const { getFieldProps, registerField, unregisterField } = useFormContext() - const fieldRules: Rules[] = useFiledRules(props) + const fieldRules: RuleItem[] = useFiledRules(props) // 当前 field 的唯一校验器 const fieldValidate = useCallback( @@ -44,10 +51,22 @@ export const useFormField = (props: UseFormFieldProps) => if (rule.validator) { return { ...rule, - validator: (validatorRule: any, value: any, cb: any) => { + validator: ( + validatorRule: any, + value: Value, + callback: (error?: string | Error) => void, + source: ValidateValues, + options: ValidateOption + ) => { const field = validatorRule.field.replace(/"/g, '') const fullField = validatorRule.fullField.replace(/"/g, '') - rule.validator({ ...validatorRule, field, fullField }, value, cb) + rule.validator!( + { ...validatorRule, field, fullField } as InternalRuleItem, + value, + callback, + source, + options + ) }, } } else