2016-09-21 11:54:53 +08:00
|
|
|
import React from 'react';
|
2017-05-09 13:40:33 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2016-11-08 16:13:07 +08:00
|
|
|
import createDOMForm from 'rc-form/lib/createDOMForm';
|
2016-10-28 13:56:23 +08:00
|
|
|
import PureRenderMixin from 'rc-util/lib/PureRenderMixin';
|
2016-09-10 17:17:55 +08:00
|
|
|
import omit from 'omit.js';
|
2016-11-08 16:13:07 +08:00
|
|
|
import warning from '../_util/warning';
|
2016-08-19 16:43:32 +08:00
|
|
|
import FormItem from './FormItem';
|
2017-10-31 10:37:49 +08:00
|
|
|
import { FIELD_META_PROP, FIELD_DATA_PROP } from './constants';
|
2015-10-09 13:53:04 +08:00
|
|
|
|
2017-08-09 12:59:31 +08:00
|
|
|
export interface FormCreateOption<T> {
|
|
|
|
onFieldsChange?: (props: T, fields: Array<any>) => void;
|
|
|
|
onValuesChange?: (props: T, values: any) => void;
|
|
|
|
mapPropsToFields?: (props: T) => void;
|
2016-09-14 10:25:44 +08:00
|
|
|
withRef?: boolean;
|
2016-08-19 16:43:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-19 16:53:27 +08:00
|
|
|
export interface FormProps {
|
2017-03-02 10:49:06 +08:00
|
|
|
layout?: 'horizontal' | 'inline' | 'vertical';
|
2016-08-19 16:43:32 +08:00
|
|
|
horizontal?: boolean;
|
|
|
|
inline?: boolean;
|
2016-08-24 16:09:55 +08:00
|
|
|
vertical?: boolean;
|
2016-09-14 16:07:28 +08:00
|
|
|
form?: WrappedFormUtils;
|
2016-10-19 17:51:33 +08:00
|
|
|
onSubmit?: React.FormEventHandler<any>;
|
2016-08-19 16:43:32 +08:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
className?: string;
|
2016-08-24 16:09:55 +08:00
|
|
|
prefixCls?: string;
|
2017-03-01 09:13:26 +08:00
|
|
|
hideRequiredMark?: boolean;
|
2016-08-19 16:43:32 +08:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:57:09 +08:00
|
|
|
export type ValidationRule = {
|
|
|
|
/** validation error message */
|
|
|
|
message?: string;
|
|
|
|
/** 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) */
|
2017-05-26 09:16:53 +08:00
|
|
|
validator?: (rule: any, value: any, callback: any, source?: any, options?: any) => any;
|
2017-05-22 11:57:09 +08:00
|
|
|
};
|
|
|
|
|
2017-10-18 23:52:54 +08:00
|
|
|
export type ValidateCallback = (errors: any, values: any) => void;
|
2017-03-30 13:46:38 +08:00
|
|
|
|
2017-07-20 13:20:15 +08:00
|
|
|
export type GetFieldDecoratorOptions = {
|
|
|
|
/** 子节点的值的属性,如 Checkbox 的是 'checked' */
|
|
|
|
valuePropName?: string;
|
|
|
|
/** 子节点的初始值,类型、可选值均由子节点决定 */
|
|
|
|
initialValue?: any;
|
|
|
|
/** 收集子节点的值的时机 */
|
|
|
|
trigger?: string;
|
|
|
|
/** 可以把 onChange 的参数转化为控件的值,例如 DatePicker 可设为:(date, dateString) => dateString */
|
|
|
|
getValueFromEvent?: (...args: any[]) => any;
|
|
|
|
/** 校验子节点值的时机 */
|
|
|
|
validateTrigger?: string | string[];
|
|
|
|
/** 校验规则,参见 [async-validator](https://github.com/yiminghe/async-validator) */
|
|
|
|
rules?: ValidationRule[];
|
|
|
|
/** 是否和其他控件互斥,特别用于 Radio 单选控件 */
|
|
|
|
exclusive?: boolean;
|
2017-09-12 09:08:47 +08:00
|
|
|
/** Normalize value to form component */
|
|
|
|
normalize?: (value: any, prevValue: any, allValues: any) => any;
|
2017-09-13 14:49:59 +08:00
|
|
|
/** Whether stop validate on first rule of error for this field. */
|
|
|
|
validateFirst?: boolean;
|
2017-07-22 15:25:08 +08:00
|
|
|
};
|
2017-07-20 13:20:15 +08:00
|
|
|
|
2016-08-24 16:09:55 +08:00
|
|
|
// function create
|
2016-08-22 17:26:14 +08:00
|
|
|
export type WrappedFormUtils = {
|
2016-08-24 16:09:55 +08:00
|
|
|
/** 获取一组输入控件的值,如不传入参数,则获取全部组件的值 */
|
2016-08-22 17:26:14 +08:00
|
|
|
getFieldsValue(fieldNames?: Array<string>): Object;
|
2016-08-19 16:43:32 +08:00
|
|
|
/** 获取一个输入控件的值*/
|
2016-08-22 17:26:14 +08:00
|
|
|
getFieldValue(fieldName: string): any;
|
2016-08-19 16:43:32 +08:00
|
|
|
/** 设置一组输入控件的值*/
|
2016-08-22 17:26:14 +08:00
|
|
|
setFieldsValue(obj: Object): void;
|
2016-08-19 16:43:32 +08:00
|
|
|
/** 设置一组输入控件的值*/
|
2016-08-22 17:26:14 +08:00
|
|
|
setFields(obj: Object): void;
|
2016-08-24 16:09:55 +08:00
|
|
|
/** 校验并获取一组输入域的值与 Error */
|
2017-03-30 13:46:38 +08:00
|
|
|
validateFields(fieldNames: Array<string>, options: Object, callback: ValidateCallback): any;
|
|
|
|
validateFields(fieldNames: Array<string>, callback: ValidateCallback): any;
|
|
|
|
validateFields(options: Object, callback: ValidateCallback): any;
|
|
|
|
validateFields(callback: ValidateCallback): any;
|
2016-08-19 16:43:32 +08:00
|
|
|
/** 与 `validateFields` 相似,但校验完后,如果校验不通过的菜单域不在可见范围内,则自动滚动进可见范围 */
|
2017-03-30 13:46:38 +08:00
|
|
|
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;
|
2016-08-19 16:43:32 +08:00
|
|
|
/** 获取某个输入控件的 Error */
|
2016-08-22 17:26:14 +08:00
|
|
|
getFieldError(name: string): Object[];
|
2017-03-02 10:53:47 +08:00
|
|
|
getFieldsError(names?: Array<string>): Object;
|
2016-08-19 16:43:32 +08:00
|
|
|
/** 判断一个输入控件是否在校验状态*/
|
2016-08-22 17:26:14 +08:00
|
|
|
isFieldValidating(name: string): boolean;
|
2017-03-29 09:36:54 +08:00
|
|
|
isFieldTouched(name: string): boolean;
|
|
|
|
isFieldsTouched(names?: Array<string>): boolean;
|
2016-08-24 16:09:55 +08:00
|
|
|
/** 重置一组输入控件的值与状态,如不传入参数,则重置所有组件 */
|
2016-08-22 17:26:14 +08:00
|
|
|
resetFields(names?: Array<string>): void;
|
2016-08-19 16:43:32 +08:00
|
|
|
|
2017-07-20 13:20:15 +08:00
|
|
|
getFieldDecorator(id: string, options?: GetFieldDecoratorOptions): (node: React.ReactNode) => React.ReactNode;
|
2017-03-23 21:15:49 +08:00
|
|
|
};
|
2016-08-19 16:43:32 +08:00
|
|
|
|
2016-08-19 16:53:27 +08:00
|
|
|
export interface FormComponentProps {
|
2017-03-24 11:43:38 +08:00
|
|
|
form: WrappedFormUtils;
|
2016-08-19 16:43:32 +08:00
|
|
|
}
|
|
|
|
|
2017-08-18 17:21:18 +08:00
|
|
|
export type Diff<T extends string, U extends string> =
|
|
|
|
({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T];
|
|
|
|
export type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>;
|
|
|
|
|
2017-03-24 11:43:38 +08:00
|
|
|
export interface ComponentDecorator<TOwnProps> {
|
2017-08-18 17:21:18 +08:00
|
|
|
<P extends FormComponentProps>(
|
|
|
|
component: React.ComponentClass<P>,
|
|
|
|
): React.ComponentClass<Omit<P, keyof FormComponentProps> & TOwnProps>;
|
2016-08-19 16:43:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class Form extends React.Component<FormProps, any> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-form',
|
2017-03-02 10:49:06 +08:00
|
|
|
layout: 'horizontal',
|
2017-01-13 21:52:48 +08:00
|
|
|
hideRequiredMark: false,
|
2016-03-29 14:01:10 +08:00
|
|
|
onSubmit(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
},
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
|
|
|
static propTypes = {
|
2017-03-02 10:49:06 +08:00
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
layout: PropTypes.oneOf(['horizontal', 'inline', 'vertical']),
|
|
|
|
children: PropTypes.any,
|
|
|
|
onSubmit: PropTypes.func,
|
|
|
|
hideRequiredMark: PropTypes.bool,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2017-01-13 21:12:31 +08:00
|
|
|
static childContextTypes = {
|
|
|
|
vertical: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
2016-08-24 16:09:55 +08:00
|
|
|
static Item = FormItem;
|
|
|
|
|
2017-08-09 12:59:31 +08:00
|
|
|
static create = function<TOwnProps>(options: FormCreateOption<TOwnProps> = {}): ComponentDecorator<TOwnProps> {
|
2017-10-31 10:37:49 +08:00
|
|
|
return createDOMForm({
|
2016-08-24 16:09:55 +08:00
|
|
|
fieldNameProp: 'id',
|
2017-07-03 16:57:11 +08:00
|
|
|
...options,
|
2016-08-24 16:09:55 +08:00
|
|
|
fieldMetaProp: FIELD_META_PROP,
|
2017-10-31 10:37:49 +08:00
|
|
|
fieldDataProp: FIELD_DATA_PROP,
|
2017-07-03 16:57:11 +08:00
|
|
|
});
|
2017-03-24 11:56:18 +08:00
|
|
|
};
|
2016-08-24 16:09:55 +08:00
|
|
|
|
2016-07-29 13:59:37 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2016-08-02 09:24:15 +08:00
|
|
|
warning(!props.form, 'It is unnecessary to pass `form` to `Form` after antd@1.7.0.');
|
2016-07-29 13:59:37 +08:00
|
|
|
}
|
|
|
|
|
2016-06-12 10:15:59 +08:00
|
|
|
shouldComponentUpdate(...args) {
|
|
|
|
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
|
|
|
|
}
|
|
|
|
|
2017-01-13 21:12:31 +08:00
|
|
|
getChildContext() {
|
2017-03-02 10:49:06 +08:00
|
|
|
const { layout, vertical } = this.props;
|
2017-01-13 21:12:31 +08:00
|
|
|
return {
|
2017-03-02 10:49:06 +08:00
|
|
|
vertical: layout === 'vertical' || vertical,
|
2017-01-13 21:12:31 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-10-09 13:53:04 +08:00
|
|
|
render() {
|
2017-03-02 10:49:06 +08:00
|
|
|
const {
|
|
|
|
prefixCls, hideRequiredMark, className = '', layout,
|
|
|
|
// @deprecated
|
|
|
|
inline, horizontal, vertical,
|
|
|
|
} = this.props;
|
|
|
|
warning(
|
|
|
|
!inline && !horizontal && !vertical,
|
2017-03-23 21:15:49 +08:00
|
|
|
'`Form[inline|horizontal|vertical]` is deprecated, please use `Form[layout]` instead.',
|
2017-03-02 10:49:06 +08:00
|
|
|
);
|
2016-11-30 10:20:23 +08:00
|
|
|
const formClassName = classNames(prefixCls, {
|
2017-03-02 10:49:06 +08:00
|
|
|
[`${prefixCls}-horizontal`]: (!inline && !vertical && layout === 'horizontal') || horizontal,
|
|
|
|
[`${prefixCls}-vertical`]: layout === 'vertical' || vertical,
|
|
|
|
[`${prefixCls}-inline`]: layout === 'inline' || inline,
|
2017-01-13 21:52:48 +08:00
|
|
|
[`${prefixCls}-hide-required-mark`]: hideRequiredMark,
|
2016-11-30 10:20:23 +08:00
|
|
|
}, className);
|
2015-10-09 13:53:04 +08:00
|
|
|
|
2016-07-07 12:06:05 +08:00
|
|
|
const formProps = omit(this.props, [
|
|
|
|
'prefixCls',
|
|
|
|
'className',
|
2017-03-02 10:49:06 +08:00
|
|
|
'layout',
|
2016-07-07 12:06:05 +08:00
|
|
|
'inline',
|
|
|
|
'horizontal',
|
2016-08-04 17:45:15 +08:00
|
|
|
'vertical',
|
2016-07-07 12:06:05 +08:00
|
|
|
'form',
|
2017-01-13 21:52:48 +08:00
|
|
|
'hideRequiredMark',
|
2016-07-07 12:06:05 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
return <form {...formProps} className={formClassName} />;
|
2015-10-09 13:53:04 +08:00
|
|
|
}
|
|
|
|
}
|