Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(form): 修复校验函数类型错误 #2949

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions packages/ui/form/src/use-form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -27,7 +34,7 @@ export const useFormField = <Values = any>(props: UseFormFieldProps<Values>) =>

const { getFieldProps, registerField, unregisterField } = useFormContext()

const fieldRules: Rules[] = useFiledRules(props)
const fieldRules: RuleItem[] = useFiledRules(props)

// 当前 field 的唯一校验器
const fieldValidate = useCallback(
Expand All @@ -44,10 +51,22 @@ export const useFormField = <Values = any>(props: UseFormFieldProps<Values>) =>
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
Expand Down