UNPKG

8.15 kBTypeScriptView Raw
1import * as React from 'react';
2import { ColProps } from 'antd/lib/grid/col';
3import FormItem, { FormLabelAlign } from './FormItem';
4import { FormWrappedProps } from './interface';
5import { ConfigConsumerProps } from '../CompatibleConsumer';
6declare type 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 declare 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 declare 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[]): {
120 [field: string]: any;
121 };
122 /** 获取一个输入控件的值 */
123 getFieldValue(fieldName: string): any;
124 /** 设置一组输入控件的值 */
125 setFieldsValue(obj: Object, callback?: Function): void;
126 /** 设置一组输入控件的值 */
127 setFields(obj: Object): void;
128 /** 校验并获取一组输入域的值与 Error */
129 validateFields(fieldNames: string[], options: ValidateFieldsOptions, callback: ValidateCallback<V>): void;
130 validateFields(options: ValidateFieldsOptions, callback: ValidateCallback<V>): void;
131 validateFields(fieldNames: string[], callback: ValidateCallback<V>): void;
132 validateFields(fieldNames: string[], options: ValidateFieldsOptions): void;
133 validateFields(fieldNames: string[]): void;
134 validateFields(callback: ValidateCallback<V>): void;
135 validateFields(options: ValidateFieldsOptions): void;
136 validateFields(): void;
137 /** 与 `validateFields` 相似,但校验完后,如果校验不通过的菜单域不在可见范围内,则自动滚动进可见范围 */
138 validateFieldsAndScroll(fieldNames: string[], options: ValidateFieldsOptions, callback: ValidateCallback<V>): void;
139 validateFieldsAndScroll(options: ValidateFieldsOptions, callback: ValidateCallback<V>): void;
140 validateFieldsAndScroll(fieldNames: string[], callback: ValidateCallback<V>): void;
141 validateFieldsAndScroll(fieldNames: string[], options: ValidateFieldsOptions): void;
142 validateFieldsAndScroll(fieldNames: string[]): void;
143 validateFieldsAndScroll(callback: ValidateCallback<V>): void;
144 validateFieldsAndScroll(options: ValidateFieldsOptions): void;
145 validateFieldsAndScroll(): void;
146 /** 获取某个输入控件的 Error */
147 getFieldError(name: string): string[] | undefined;
148 getFieldsError(names?: string[]): Record<string, string[] | undefined>;
149 /** 判断一个输入控件是否在校验状态 */
150 isFieldValidating(name: string): boolean;
151 isFieldTouched(name: string): boolean;
152 isFieldsTouched(names?: string[]): boolean;
153 /** 重置一组输入控件的值与状态,如不传入参数,则重置所有组件 */
154 resetFields(names?: string[]): void;
155 getFieldDecorator<T extends Object = {}>(id: keyof T, options?: GetFieldDecoratorOptions): (node: React.ReactNode) => React.ReactNode;
156}
157export interface WrappedFormInternalProps<V = any> {
158 form: WrappedFormUtils<V>;
159}
160export interface RcBaseFormProps {
161 wrappedComponentRef?: any;
162}
163export interface FormComponentProps<V = any> extends WrappedFormInternalProps<V>, RcBaseFormProps {
164 form: WrappedFormUtils<V>;
165}
166export default class Form extends React.Component<FormProps, any> {
167 static defaultProps: {
168 colon: boolean;
169 layout: "inline" | "horizontal" | "vertical";
170 hideRequiredMark: boolean;
171 onSubmit(e: React.FormEvent<HTMLFormElement>): void;
172 };
173 static Item: typeof FormItem;
174 static createFormField: any;
175 static create: <TOwnProps extends FormComponentProps<any>>(options?: FormCreateOption<TOwnProps>) => FormWrappedProps<TOwnProps>;
176 constructor(props: FormProps);
177 componentDidMount(): void;
178 renderForm: ({ getPrefixCls }: ConfigConsumerProps) => JSX.Element;
179 render(): JSX.Element;
180}
181export {};