2022-03-06 22:20:56 +08:00
|
|
|
import type { InjectionKey, SetupContext, UnwrapRef } from 'vue'
|
2022-03-15 14:09:26 +08:00
|
|
|
import type {
|
|
|
|
RuleItem,
|
|
|
|
ValidateError,
|
|
|
|
ValidateFieldsError,
|
|
|
|
} from 'async-validator'
|
2022-02-09 16:59:08 +08:00
|
|
|
import type { ComponentSize } from '@element-plus/constants'
|
2022-03-06 22:20:56 +08:00
|
|
|
import type {
|
|
|
|
FormProps,
|
|
|
|
FormEmits,
|
2022-03-07 13:37:13 +08:00
|
|
|
FormItemProp,
|
2022-03-06 22:20:56 +08:00
|
|
|
FormItemProps,
|
|
|
|
FormLabelWidthContext,
|
|
|
|
} from '@element-plus/components/form'
|
|
|
|
import type { Arrayable } from '@element-plus/utils'
|
2020-10-03 16:02:53 +08:00
|
|
|
|
2022-03-15 14:09:26 +08:00
|
|
|
export interface FormItemRule extends RuleItem {
|
|
|
|
trigger?: Arrayable<string>
|
|
|
|
}
|
|
|
|
export type FormRules = Partial<Record<string, Arrayable<FormItemRule>>>
|
|
|
|
|
|
|
|
export type FormValidationResult = Promise<boolean>
|
|
|
|
export type FormValidateCallback = (
|
|
|
|
isValid: boolean,
|
|
|
|
invalidFields?: ValidateFieldsError
|
|
|
|
) => void
|
|
|
|
export interface FormValidateFailure {
|
|
|
|
errors: ValidateError[] | null
|
|
|
|
fields: ValidateFieldsError
|
|
|
|
}
|
|
|
|
|
2022-03-06 22:20:56 +08:00
|
|
|
export type FormContext = FormProps &
|
|
|
|
UnwrapRef<FormLabelWidthContext> & {
|
|
|
|
emit: SetupContext<FormEmits>['emit']
|
2020-10-03 16:02:53 +08:00
|
|
|
|
2022-03-06 22:20:56 +08:00
|
|
|
// expose
|
|
|
|
addField: (field: FormItemContext) => void
|
|
|
|
removeField: (field: FormItemContext) => void
|
2022-03-07 13:37:13 +08:00
|
|
|
resetFields: (props?: Arrayable<FormItemProp>) => void
|
|
|
|
clearValidate: (props?: Arrayable<FormItemProp>) => void
|
2022-03-06 22:20:56 +08:00
|
|
|
validateField: (
|
2022-03-07 13:37:13 +08:00
|
|
|
props?: Arrayable<FormItemProp>,
|
2022-03-06 22:20:56 +08:00
|
|
|
callback?: FormValidateCallback
|
2022-03-14 19:47:31 +08:00
|
|
|
) => FormValidationResult
|
2022-03-06 22:20:56 +08:00
|
|
|
}
|
2020-10-03 16:02:53 +08:00
|
|
|
|
2022-03-06 22:20:56 +08:00
|
|
|
export interface FormItemContext extends FormItemProps {
|
|
|
|
$el: HTMLDivElement | undefined
|
|
|
|
size: ComponentSize
|
2020-11-02 11:05:08 +08:00
|
|
|
validateState: string
|
2022-03-14 19:47:31 +08:00
|
|
|
validate: (
|
|
|
|
trigger: string,
|
|
|
|
callback?: FormValidateCallback
|
|
|
|
) => FormValidationResult
|
2020-10-03 16:02:53 +08:00
|
|
|
resetField(): void
|
|
|
|
clearValidate(): void
|
|
|
|
}
|
|
|
|
|
2022-03-06 22:20:56 +08:00
|
|
|
export const formContextKey: InjectionKey<FormContext> =
|
|
|
|
Symbol('formContextKey')
|
|
|
|
export const formItemContextKey: InjectionKey<FormItemContext> =
|
|
|
|
Symbol('formItemContextKey')
|