UNPKG

7.76 kBTypeScriptView Raw
1import * as React from 'react';
2import type { ColProps } from 'antd/lib/grid/col';
3import FormItem from './FormItem';
4import type { FormLabelAlign } from './FormItem';
5import type { FormWrappedProps } from './interface';
6type FormCreateOptionMessagesCallback = (...args: any[]) => string;
7interface FormCreateOptionMessages {
8 [messageId: string]: string | FormCreateOptionMessagesCallback | FormCreateOptionMessages;
9}
10export interface FormCreateOption<T> {
11 onFieldsChange?: (props: T, fields: any, allFields: any) => void;
12 onValuesChange?: (props: T, changedValues: any, allValues: any) => void;
13 mapPropsToFields?: (props: T) => void;
14 validateMessages?: FormCreateOptionMessages;
15 withRef?: boolean;
16 name?: string;
17}
18declare const FormLayouts: ["horizontal", "inline", "vertical"];
19export type FormLayout = typeof FormLayouts[number];
20export interface FormProps extends React.FormHTMLAttributes<HTMLFormElement> {
21 layout?: FormLayout;
22 form?: WrappedFormUtils;
23 onSubmit?: React.FormEventHandler<HTMLFormElement>;
24 style?: React.CSSProperties;
25 className?: string;
26 prefixCls?: string;
27 hideRequiredMark?: boolean;
28 /**
29 * @since 3.14.0
30 */
31 wrapperCol?: ColProps;
32 labelCol?: ColProps;
33 /**
34 * @since 3.15.0
35 */
36 colon?: boolean;
37 labelAlign?: FormLabelAlign;
38}
39export interface ValidationRule {
40 /** validation error message */
41 message?: React.ReactNode;
42 /** built-in validation type, available options: https://github.com/yiminghe/async-validator#type */
43 type?: string;
44 /** indicates whether field is required */
45 required?: boolean;
46 /** treat required fields that only contain whitespace as errors */
47 whitespace?: boolean;
48 /** validate the exact length of a field */
49 len?: number;
50 /** validate the min length of a field */
51 min?: number;
52 /** validate the max length of a field */
53 max?: number;
54 /** validate the value from a list of possible values */
55 enum?: string | string[];
56 /** validate from a regular expression */
57 pattern?: RegExp;
58 /** transform a value before validation */
59 transform?: (value: any) => any;
60 /** custom validate function (Note: callback must be called) */
61 validator?: (rule: any, value: any, callback: any, source?: any, options?: any) => any;
62}
63export type ValidateCallback<V> = (errors: any, values: V) => void;
64export interface GetFieldDecoratorOptions {
65 /** 子节点的值的属性,如 Checkbox 的是 'checked' */
66 valuePropName?: string;
67 /** 子节点的初始值,类型、可选值均由子节点决定 */
68 initialValue?: any;
69 /** 收集子节点的值的时机 */
70 trigger?: string;
71 /** 可以把 onChange 的参数转化为控件的值,例如 DatePicker 可设为:(date, dateString) => dateString */
72 getValueFromEvent?: (...args: any[]) => any;
73 /** Get the component props according to field value. */
74 getValueProps?: (value: any) => any;
75 /** 校验子节点值的时机 */
76 validateTrigger?: string | string[];
77 /** 校验规则,参见 [async-validator](https://github.com/yiminghe/async-validator) */
78 rules?: ValidationRule[];
79 /** 是否和其他控件互斥,特别用于 Radio 单选控件 */
80 exclusive?: boolean;
81 /** Normalize value to form component */
82 normalize?: (value: any, prevValue: any, allValues: any) => any;
83 /** Whether stop validate on first rule of error for this field. */
84 validateFirst?: boolean;
85 /** 是否一直保留子节点的信息 */
86 preserve?: boolean;
87}
88/** dom-scroll-into-view 组件配置参数 */
89export interface DomScrollIntoViewConfig {
90 /** 是否和左边界对齐 */
91 alignWithLeft?: boolean;
92 /** 是否和上边界对齐 */
93 alignWithTop?: boolean;
94 /** 顶部偏移量 */
95 offsetTop?: number;
96 /** 左侧偏移量 */
97 offsetLeft?: number;
98 /** 底部偏移量 */
99 offsetBottom?: number;
100 /** 右侧偏移量 */
101 offsetRight?: number;
102 /** 是否允许容器水平滚动 */
103 allowHorizontalScroll?: boolean;
104 /** 当内容可见时是否允许滚动容器 */
105 onlyScrollIfNeeded?: boolean;
106}
107export interface ValidateFieldsOptions {
108 /** 所有表单域是否在第一个校验规则失败后停止继续校验 */
109 first?: boolean;
110 /** 指定哪些表单域在第一个校验规则失败后停止继续校验 */
111 firstFields?: string[];
112 /** 已经校验过的表单域,在 validateTrigger 再次被触发时是否再次校验 */
113 force?: boolean;
114 /** 定义 validateFieldsAndScroll 的滚动行为 */
115 scroll?: DomScrollIntoViewConfig;
116}
117export interface WrappedFormUtils<V = any> {
118 /** 获取一组输入控件的值,如不传入参数,则获取全部组件的值 */
119 getFieldsValue(fieldNames?: string[]): Record<string, any>;
120 /** 获取一个输入控件的值 */
121 getFieldValue(fieldName: string): any;
122 /** 设置一组输入控件的值 */
123 setFieldsValue(obj: Object, callback?: Function): void;
124 /** 设置一组输入控件的值 */
125 setFields(obj: Object): void;
126 /** 校验并获取一组输入域的值与 Error */
127 validateFields(fieldNames: string[], options: ValidateFieldsOptions, callback: ValidateCallback<V>): void;
128 validateFields(options: ValidateFieldsOptions, callback: ValidateCallback<V>): void;
129 validateFields(fieldNames: string[], callback: ValidateCallback<V>): void;
130 validateFields(fieldNames: string[], options: ValidateFieldsOptions): void;
131 validateFields(fieldNames: string[]): void;
132 validateFields(callback: ValidateCallback<V>): void;
133 validateFields(options: ValidateFieldsOptions): void;
134 validateFields(): void;
135 /** 与 `validateFields` 相似,但校验完后,如果校验不通过的菜单域不在可见范围内,则自动滚动进可见范围 */
136 validateFieldsAndScroll(fieldNames: string[], options: ValidateFieldsOptions, callback: ValidateCallback<V>): void;
137 validateFieldsAndScroll(options: ValidateFieldsOptions, callback: ValidateCallback<V>): void;
138 validateFieldsAndScroll(fieldNames: string[], callback: ValidateCallback<V>): void;
139 validateFieldsAndScroll(fieldNames: string[], options: ValidateFieldsOptions): void;
140 validateFieldsAndScroll(fieldNames: string[]): void;
141 validateFieldsAndScroll(callback: ValidateCallback<V>): void;
142 validateFieldsAndScroll(options: ValidateFieldsOptions): void;
143 validateFieldsAndScroll(): void;
144 /** 获取某个输入控件的 Error */
145 getFieldError(name: string): string[] | undefined;
146 getFieldsError(names?: string[]): Record<string, string[] | undefined>;
147 /** 判断一个输入控件是否在校验状态 */
148 isFieldValidating(name: string): boolean;
149 isFieldTouched(name: string): boolean;
150 isFieldsTouched(names?: string[]): boolean;
151 /** 重置一组输入控件的值与状态,如不传入参数,则重置所有组件 */
152 resetFields(names?: string[]): void;
153 getFieldDecorator<T extends Object = {}>(id: keyof T, options?: GetFieldDecoratorOptions): (node: React.ReactNode) => React.ReactNode;
154}
155export interface WrappedFormInternalProps<V = any> {
156 form: WrappedFormUtils<V>;
157}
158export interface RcBaseFormProps {
159 wrappedComponentRef?: any;
160}
161export interface FormComponentProps<V = any> extends WrappedFormInternalProps<V>, RcBaseFormProps {
162 form: WrappedFormUtils<V>;
163}
164declare const FormFC: React.ForwardRefExoticComponent<FormProps & React.RefAttributes<any>> & {
165 Item: typeof FormItem;
166 createFormField: any;
167 create: typeof create;
168};
169declare function create<TOwnProps extends FormComponentProps>(options?: FormCreateOption<TOwnProps>): FormWrappedProps<TOwnProps>;
170export default FormFC;