123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- /// <reference types="@mini-types/global" />
- import { InternalRuleItem, Value, Values, RuleItem, ValidateMessages } from 'async-validator';
- import { IMixin4Legacy } from '@mini-types/alipay';
- export { Value, Values };
- export type Validator = (rule: Omit<InternalRuleItem, 'validator' | 'asyncValidator'>, value: Value) => void | Promise<void>;
- export type ValidatorRender = (form: Form) => {
- validator: Validator;
- };
- export type FormRuleItem = Omit<RuleItem, 'asyncValidator'> | ValidatorRender;
- export type Rule = FormRuleItem | FormRuleItem[];
- export type Rules = Record<string, Rule>;
- export type FormConfig = {
- rules?: Rules;
- initialValues?: Values;
- validateMessages?: ValidateMessages;
- };
- export type ValidateStatus = 'default' | 'success' | 'error' | 'validating';
- export interface ValidatorStatus {
- status: ValidateStatus;
- errors: string[];
- }
- export interface FromItemRef {
- setFormData: (values: Values) => void;
- getFormData: () => Values;
- getProps: () => Record<string, Value>;
- on: (callback: (trigger: EventTrigger, value?: Value) => void) => void;
- }
- export type ValidateTrigger = 'onChange' | 'onBlur' | 'onFocus';
- export type EventTrigger = ValidateTrigger | 'didUnmount' | 'deriveDataFromProps';
- export declare class Form {
- /**
- * 表单初始值
- */
- private initialValues;
- /**
- * 原始规则,用户传入的跟之前的规则区别在于validator
- */
- private rules;
- /**
- * 表单ref组件对象
- */
- private fields;
- /**
- * 验证提示模板
- */
- private validateMessages;
- /**
- * 表单字段 change侦听
- */
- private changeListeners;
- /**
- * 依赖表
- */
- private dependenciesMap;
- /**
- * Form构建
- * @param formConfig 表单配置项
- */
- constructor(formConfig?: FormConfig);
- /**
- * 用户传入的rules转换成async-validator rules
- * @param rules 校验规则
- */
- private transformRules;
- /**
- * 遍历表单field对象
- * @param callback
- */
- private eachField;
- /**
- * 更新 rules
- * @param rules
- */
- updateRules(rules: Rules): void;
- /**
- * 设置 rules
- * @param rules
- */
- private setRules;
- /**
- * 添加表单对象
- * @param ref 表单ref对象
- */
- addItem(ref: FromItemRef, customName?: string): void;
- /**
- * 设置表单值
- * @param name 表单名称
- * @param value 表单初始值
- */
- setFieldValue(name: string, value: Value): void;
- /**
- * 设置表单值
- * @param name 表单名称
- * @param value 表单初始值
- */
- setFieldsValue(values: Values): void;
- /**
- * 设置 initialValues,这个操作不会对页面进行修改,要是需要重置表单可跟上 reset 方法;
- * 这样是对于表单已经在编辑,但是需要重新initialValues的场景
- *
- * eg:
- * this.setInitialValues(initialValues);
- * this.reset();
- *
- * @param initialValues
- */
- setInitialValues(initialValues: Values): void;
- /**
- * 获取对应字段名的值
- * @param name
- * @returns
- */
- getFieldValue(name: string): any;
- /**
- * 获取一组字段名对应的值
- * @param nameList
- * @returns
- */
- getFieldsValue(nameList?: string[]): Values;
- /**
- * 获取对应字段名的校验器状态
- * @param name
- * @returns
- */
- getFieldValidatorStatus(name: string): ValidatorStatus;
- /**
- * 获取一组字段名的校验器状态
- * @param nameList
- * @returns
- */
- getFieldsValidatorStatus(nameList?: string[]): Record<string, ValidatorStatus>;
- /**
- * 设置对应字段名的校验器状态
- * @param name 表单名称
- * @param validatorStatus 校验状态
- * @returns
- */
- setFieldValidatorStatus(name: string, validatorStatus: ValidatorStatus): void;
- /**
- * 设置一组字段名的校验器状态
- * @param fieldsValidatorStatus 表单校验状态
- * @returns
- */
- setFieldsValidatorStatus(fieldsValidatorStatus: Record<string, ValidatorStatus>): void;
- /**
- * 检查对应字段是否被用户操作过
- * @param name 字段名称
- * @returns
- */
- isFieldTouched(name: string): boolean;
- /**
- * 指定表单字段值更新时触发回调方法
- * @param name 表单字段名称
- * @param callback 回调方法
- */
- onValueChange(name: string, callback: (value: Value, allValues: Values) => void): void;
- /**
- * 表单字段值更新时触发回调方法
- * @param name 表单字段名称
- * @param callback 回调方法
- */
- onValuesChange(callback: (changedValues: Values, allValues: Values) => void): void;
- /**
- * 表单提交
- */
- submit(): Promise<Values>;
- /**
- * 表单重置
- */
- reset(): void;
- }
- export declare function createForm({ methods }?: {
- methods?: {};
- }): IMixin4Legacy<{
- formData: {
- value: Value;
- } & ValidatorStatus;
- }, Record<string, any>, {
- emit(trigger: EventTrigger, value?: Value): void;
- setFormData(values: Values): void;
- getFormData(): {
- value: Value;
- } & ValidatorStatus;
- on(callback: (trigger: EventTrigger, value?: Value) => void): void;
- getProps: Record<string, any>;
- }, MiniProgram.UnknownRecord, MiniProgram.UnknownRecord>;
|