@tplc/wot
Version:
88 lines (87 loc) • 1.83 kB
TypeScript
import {
type ComponentPublicInstance,
type ExtractPropTypes,
type InjectionKey,
type PropType,
} from 'vue'
export type FormProvide = {
props: {
model: Record<string, any>
rules?: FormRules
border?: boolean
}
errorMessages?: Record<string, string>
}
export declare const FORM_KEY: InjectionKey<FormProvide>
export type FormRules = {
[key: string]: FormItemRule[]
}
export type ErrorMessage = {
prop: string
message: string
}
export interface FormItemRule {
[key: string]: any
required: boolean
message: string
pattern?: RegExp
validator?: (
value: any,
rule: FormItemRuleWithoutValidator,
) => boolean | Promise<string> | Promise<boolean> | Promise<void> | Promise<unknown>
}
export type FormItemRuleWithoutValidator = Omit<FormItemRule, 'validator'>
export declare const formProps: {
/**
* 表单数据对象
*/
model: {
type: PropType<Record<string, any>>
required: true
}
/**
* 表单验证规则
*/
rules: {
type: PropType<FormRules>
default: () => {}
}
/**
* 是否在输入时重置表单校验信息
*/
resetOnChange: {
type: BooleanConstructor
default: boolean
}
/**
* 错误提示类型
*/
errorType: {
type: PropType<'toast' | 'message' | 'none'>
default: string
}
customStyle: {
type: PropType<string>
default: string
}
customClass: {
type: PropType<string>
default: string
}
}
export type FormProps = ExtractPropTypes<typeof formProps>
export type FormExpose = {
/**
* 表单校验
* @param prop 指定校验字段
*/
validate: (prop?: string) => Promise<{
valid: boolean
errors: ErrorMessage[]
}>
/**
* 重置表单项的验证提示
*/
reset: () => void
}
export type FormInstance = ComponentPublicInstance<FormProps, FormExpose>