Form.tsx 7.3 KB
Newer Older
1
import * as React from 'react';
W
Wei Zhu 已提交
2
import * as PropTypes from 'prop-types';
3
import classNames from 'classnames';
B
Benjy Cui 已提交
4
import createDOMForm from 'rc-form/lib/createDOMForm';
B
Benjy Cui 已提交
5
import createFormField from 'rc-form/lib/createFormField';
6
import omit from 'omit.js';
B
Benjy Cui 已提交
7
import warning from '../_util/warning';
8
import FormItem from './FormItem';
9
import { FIELD_META_PROP, FIELD_DATA_PROP } from './constants';
10
import { Omit } from '../_util/type';
S
SimaQ 已提交
11

12 13 14 15 16 17 18 19 20
type FormCreateOptionMessagesCallback = (...args: any[]) => string;

interface FormCreateOptionMessages {
  [messageId: string]:
    | string
    | FormCreateOptionMessagesCallback
    | FormCreateOptionMessages;
}

21
export interface FormCreateOption<T> {
22
  onFieldsChange?: (props: T, fields: Array<any>, allFields: any, add: string) => void;
23
  onValuesChange?: (props: T, changedValues: any, allValues: any) => void;
24
  mapPropsToFields?: (props: T) => void;
25
  validateMessages?: FormCreateOptionMessages;
26
  withRef?: boolean;
27 28
}

E
Ed Moore 已提交
29 30
export type FormLayout = 'horizontal' | 'inline' | 'vertical';

31
export interface FormProps extends React.FormHTMLAttributes<HTMLFormElement> {
E
Ed Moore 已提交
32
  layout?: FormLayout;
33
  form?: WrappedFormUtils;
Y
yiminghe 已提交
34
  onSubmit?: React.FormEventHandler<any>;
35 36
  style?: React.CSSProperties;
  className?: string;
A
afc163 已提交
37
  prefixCls?: string;
38
  hideRequiredMark?: boolean;
39 40
}

41 42
export type ValidationRule = {
  /** validation error message */
43
  message?: React.ReactNode;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  /** built-in validation type, available options: https://github.com/yiminghe/async-validator#type */
  type?: string;
  /** indicates whether field is required */
  required?: boolean;
  /** treat required fields that only contain whitespace as errors */
  whitespace?: boolean;
  /** validate the exact length of a field */
  len?: number;
  /** validate the min length of a field */
  min?: number;
  /** validate the max length of a field */
  max?: number;
  /** validate the value from a list of possible values */
  enum?: string | string[];
  /** validate from a regular expression */
  pattern?: RegExp;
  /** transform a value before validation */
  transform?: (value: any) => any;
  /** custom validate function (Note: callback must be called) */
63
  validator?: (rule: any, value: any, callback: any, source?: any, options?: any) => any;
64 65
};

D
ddcat1115 已提交
66
export type ValidateCallback = (errors: any, values: any) => void;
67

R
Randy 已提交
68 69 70 71 72 73 74 75 76
export type GetFieldDecoratorOptions = {
  /** 子节点的值的属性,如 Checkbox 的是 'checked' */
  valuePropName?: string;
  /** 子节点的初始值,类型、可选值均由子节点决定 */
  initialValue?: any;
  /** 收集子节点的值的时机 */
  trigger?: string;
  /** 可以把 onChange 的参数转化为控件的值,例如 DatePicker 可设为:(date, dateString) => dateString */
  getValueFromEvent?: (...args: any[]) => any;
77 78
  /** Get the component props according to field value. */
  getValueProps?: (value: any) => any;
R
Randy 已提交
79 80 81 82 83 84
  /** 校验子节点值的时机 */
  validateTrigger?: string | string[];
  /** 校验规则,参见 [async-validator](https://github.com/yiminghe/async-validator) */
  rules?: ValidationRule[];
  /** 是否和其他控件互斥,特别用于 Radio 单选控件 */
  exclusive?: boolean;
85 86
  /** Normalize value to form component */
  normalize?: (value: any, prevValue: any, allValues: any) => any;
87
  /** Whether stop validate on first rule of error for this field.  */
88
  validateFirst?: boolean;
A
afc163 已提交
89
};
R
Randy 已提交
90

A
afc163 已提交
91
// function create
B
Benjy Cui 已提交
92
export type WrappedFormUtils = {
A
afc163 已提交
93
  /** 获取一组输入控件的值,如不传入参数,则获取全部组件的值 */
B
Benjy Cui 已提交
94
  getFieldsValue(fieldNames?: Array<string>): Object;
95
  /** 获取一个输入控件的值*/
B
Benjy Cui 已提交
96
  getFieldValue(fieldName: string): any;
97
  /** 设置一组输入控件的值*/
B
Benjy Cui 已提交
98
  setFieldsValue(obj: Object): void;
99
  /** 设置一组输入控件的值*/
B
Benjy Cui 已提交
100
  setFields(obj: Object): void;
A
afc163 已提交
101
  /** 校验并获取一组输入域的值与 Error */
W
Wei Zhu 已提交
102 103 104 105 106
  validateFields(fieldNames: Array<string>, options: Object, callback: ValidateCallback): void;
  validateFields(fieldNames: Array<string>, callback: ValidateCallback): void;
  validateFields(options: Object, callback: ValidateCallback): void;
  validateFields(callback: ValidateCallback): void;
  validateFields(): void;
107
  /** 与 `validateFields` 相似,但校验完后,如果校验不通过的菜单域不在可见范围内,则自动滚动进可见范围 */
108 109 110 111
  validateFieldsAndScroll(fieldNames?: Array<string>, options?: Object, callback?: ValidateCallback): void;
  validateFieldsAndScroll(fieldNames?: Array<string>, callback?: ValidateCallback): void;
  validateFieldsAndScroll(options?: Object, callback?: ValidateCallback): void;
  validateFieldsAndScroll(callback?: ValidateCallback): void;
W
Wei Zhu 已提交
112
  validateFieldsAndScroll(): void;
113
  /** 获取某个输入控件的 Error */
B
Benjy Cui 已提交
114
  getFieldError(name: string): Object[];
115
  getFieldsError(names?: Array<string>): Object;
116
  /** 判断一个输入控件是否在校验状态*/
B
Benjy Cui 已提交
117
  isFieldValidating(name: string): boolean;
118 119
  isFieldTouched(name: string): boolean;
  isFieldsTouched(names?: Array<string>): boolean;
A
afc163 已提交
120
  /** 重置一组输入控件的值与状态,如不传入参数,则重置所有组件 */
B
Benjy Cui 已提交
121
  resetFields(names?: Array<string>): void;
N
Nikolay 已提交
122
  // tslint:disable-next-line:max-line-length
123
  getFieldDecorator<T extends Object = {}>(id: keyof T, options?: GetFieldDecoratorOptions): (node: React.ReactNode) => React.ReactNode;
偏右 已提交
124
};
125

B
Benjy Cui 已提交
126
export interface FormComponentProps {
127
  form: WrappedFormUtils;
128 129
}

P
paranoidjk 已提交
130 131 132 133
export interface RcBaseFormProps {
   wrappedComponentRef?: any;
}

134
export interface ComponentDecorator {
W
Wei Zhu 已提交
135
  <P extends FormComponentProps>(
136
    component: React.ComponentClass<P> | React.SFC<P>,
P
paranoidjk 已提交
137
  ): React.ComponentClass<RcBaseFormProps & Omit<P, keyof FormComponentProps>>;
138 139 140
}

export default class Form extends React.Component<FormProps, any> {
141 142
  static defaultProps = {
    prefixCls: 'ant-form',
E
Ed Moore 已提交
143
    layout: 'horizontal' as FormLayout,
偏右 已提交
144
    hideRequiredMark: false,
W
Wei Zhu 已提交
145
    onSubmit(e: React.FormEvent<HTMLFormElement>) {
146 147
      e.preventDefault();
    },
偏右 已提交
148
  };
149 150

  static propTypes = {
B
Benjy Cui 已提交
151 152 153 154 155
    prefixCls: PropTypes.string,
    layout: PropTypes.oneOf(['horizontal', 'inline', 'vertical']),
    children: PropTypes.any,
    onSubmit: PropTypes.func,
    hideRequiredMark: PropTypes.bool,
偏右 已提交
156
  };
157

158 159 160 161
  static childContextTypes = {
    vertical: PropTypes.bool,
  };

A
afc163 已提交
162 163
  static Item = FormItem;

B
Benjy Cui 已提交
164 165
  static createFormField = createFormField;

166
  static create = function<TOwnProps>(options: FormCreateOption<TOwnProps> = {}): ComponentDecorator {
167
    return createDOMForm({
A
afc163 已提交
168
      fieldNameProp: 'id',
169
      ...options,
A
afc163 已提交
170
      fieldMetaProp: FIELD_META_PROP,
171
      fieldDataProp: FIELD_DATA_PROP,
172
    });
A
afc163 已提交
173
  };
A
afc163 已提交
174

W
Wei Zhu 已提交
175
  constructor(props: FormProps) {
B
Benjy Cui 已提交
176 177
    super(props);

178
    warning(!props.form, 'It is unnecessary to pass `form` to `Form` after antd@1.7.0.');
B
Benjy Cui 已提交
179 180
  }

181
  getChildContext() {
W
Wei Zhu 已提交
182
    const { layout } = this.props;
183
    return {
W
Wei Zhu 已提交
184
      vertical: layout === 'vertical',
185 186 187
    };
  }

S
SimaQ 已提交
188
  render() {
B
Benjy Cui 已提交
189 190 191
    const {
      prefixCls, hideRequiredMark, className = '', layout,
    } = this.props;
192
    const formClassName = classNames(prefixCls, {
W
Wei Zhu 已提交
193 194 195
      [`${prefixCls}-horizontal`]: layout === 'horizontal',
      [`${prefixCls}-vertical`]: layout === 'vertical',
      [`${prefixCls}-inline`]: layout === 'inline',
偏右 已提交
196
      [`${prefixCls}-hide-required-mark`]: hideRequiredMark,
197
    }, className);
S
SimaQ 已提交
198

A
afc163 已提交
199 200 201
    const formProps = omit(this.props, [
      'prefixCls',
      'className',
B
Benjy Cui 已提交
202
      'layout',
A
afc163 已提交
203
      'form',
偏右 已提交
204
      'hideRequiredMark',
A
afc163 已提交
205 206 207
    ]);

    return <form {...formProps} className={formClassName} />;
S
SimaQ 已提交
208 209
  }
}