UNPKG

@zenweb/form

Version:

Zenweb Form module

71 lines (70 loc) 2.06 kB
import { ResultFail } from '@zenweb/result'; import { TypeKeys, TypeMap, ValidateOption } from 'typecasts'; import { Widget } from './widget.js'; import { FieldResult, FormData } from './types.js'; export declare class FieldFail extends ResultFail { mcode: string; constructor(mcode: string, extra?: any, data?: any); } /** * 使用链式编程生成带有类型标注的字段 */ export declare class Field<T extends TypeKeys, R = TypeMap[T]> extends Widget { protected readonly _valueType: T; protected _validate?: ValidateOption; protected _splitter?: string; protected _maxItems?: number; protected _minItems?: number; protected _default?: R; /** * 是否为必填项 */ protected _required: boolean; /** * 是否允许 null 值 */ protected _nullable: boolean; constructor(_valueType: T); /** * 设置默认值 */ default(value: R): this; /** * 数据项验证 */ validate(opt: ValidateOption): this; /** * 设置列表分隔符 * - 类型为列表时有效 * - 如果原始数据为字符串时使用的切割符 * @param sep 默认 `,` */ splitter(sep: string): this; /** * 限制列表最大数量 */ maxItems(count: number): this; /** * 限制列表最小数量 */ minItems(count: number): this; /** * 验证失败 - 抛出异常 */ protected fail(code: string, params?: any): void; /** * 数据验证清理 * - 在字段数据验证通过后调用 * - 如果验证不通过需要抛出异常可以使用 `this.fail('code')` * - 需要返回清理完成的数据 */ clean(data: any): any; output(formData?: FormData, fieldName?: string): import("./types.js").WidgetOption & Record<string, any> & FieldResult; } /** * 使用函数方式初始化字段类 * @param clazz 字段类 */ export declare function simple<T extends TypeKeys, F extends Field<T>>(clazz: { new (valueType: T): F; }): (valueType: T) => F;