2016-09-21 11:54:53 +08:00
|
|
|
import React from 'react';
|
2016-08-24 16:09:55 +08:00
|
|
|
import { PropTypes } from 'react';
|
2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2016-06-16 22:06:06 +08:00
|
|
|
import PureRenderMixin from 'react-addons-pure-render-mixin';
|
2016-09-10 17:17:55 +08:00
|
|
|
import omit from 'omit.js';
|
2016-07-29 13:59:37 +08:00
|
|
|
import warning from 'warning';
|
2016-08-24 16:09:55 +08:00
|
|
|
import assign from 'object-assign';
|
2016-08-19 16:43:32 +08:00
|
|
|
import FormItem from './FormItem';
|
2016-08-24 16:09:55 +08:00
|
|
|
import createDOMForm from 'rc-form/lib/createDOMForm';
|
|
|
|
import { FIELD_META_PROP } from './constants';
|
2015-10-09 13:53:04 +08:00
|
|
|
|
2016-08-19 16:53:27 +08:00
|
|
|
export interface FormCreateOption {
|
2016-08-19 16:43:32 +08:00
|
|
|
onFieldsChange?: (props: any, fields: Array<any>) => void;
|
|
|
|
mapPropsToFields?: (props: any) => 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 {
|
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-08-19 16:43:32 +08:00
|
|
|
onSubmit?: React.FormEventHandler;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
className?: string;
|
2016-08-24 16:09:55 +08:00
|
|
|
prefixCls?: string;
|
2016-08-19 16:43:32 +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 */
|
2016-09-19 17:35:17 +08:00
|
|
|
validateFields(fieldNames: Array<string>, options: Object, callback: (erros: any, values: any) => void): any;
|
|
|
|
validateFields(fieldNames: Array<string>, callback: (erros: any, values: any) => void): any;
|
|
|
|
validateFields(options: Object, callback: (erros: any, values: any) => void): any;
|
|
|
|
validateFields(callback: (erros: any, values: any) => void): any;
|
2016-08-19 16:43:32 +08:00
|
|
|
/** 与 `validateFields` 相似,但校验完后,如果校验不通过的菜单域不在可见范围内,则自动滚动进可见范围 */
|
2016-08-22 17:26:14 +08:00
|
|
|
validateFieldsAndScroll(
|
2016-08-19 16:43:32 +08:00
|
|
|
fieldNames?: Array<string>,
|
|
|
|
options?: Object,
|
|
|
|
callback?: (erros: any, values: any) => void
|
2016-08-22 17:26:14 +08:00
|
|
|
): void;
|
2016-08-19 16:43:32 +08:00
|
|
|
/** 获取某个输入控件的 Error */
|
2016-08-22 17:26:14 +08:00
|
|
|
getFieldError(name: string): Object[];
|
2016-08-19 16:43:32 +08:00
|
|
|
/** 判断一个输入控件是否在校验状态*/
|
2016-08-22 17:26:14 +08:00
|
|
|
isFieldValidating(name: 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
|
|
|
|
2016-09-14 16:07:28 +08:00
|
|
|
getFieldDecorator(id: string, options: {
|
2016-08-24 16:09:55 +08:00
|
|
|
/** 子节点的值的属性,如 Checkbox 的是 'checked' */
|
2016-08-19 16:43:32 +08:00
|
|
|
valuePropName?: string;
|
2016-08-24 16:09:55 +08:00
|
|
|
/** 子节点的初始值,类型、可选值均由子节点决定 */
|
2016-08-19 16:43:32 +08:00
|
|
|
initialValue?: any;
|
2016-08-24 16:09:55 +08:00
|
|
|
/** 收集子节点的值的时机 */
|
2016-08-19 16:43:32 +08:00
|
|
|
trigger?: string;
|
2016-09-14 16:07:28 +08:00
|
|
|
/** 可以把 onChange 的参数转化为控件的值,例如 DatePicker 可设为:(date, dateString) => dateString */
|
|
|
|
getValueFromEvent?: (...args) => any;
|
2016-08-24 16:09:55 +08:00
|
|
|
/** 校验子节点值的时机 */
|
2016-08-19 16:43:32 +08:00
|
|
|
validateTrigger?: string;
|
|
|
|
/** 校验规则,参见 [async-validator](https://github.com/yiminghe/async-validator) */
|
|
|
|
rules?: Array<any>;
|
2016-09-14 16:07:28 +08:00
|
|
|
/** 是否和其他控件互斥,特别用于 Radio 单选控件 */
|
2016-09-19 17:35:17 +08:00
|
|
|
exclusive?: boolean;
|
2016-09-21 09:27:58 +08:00
|
|
|
}): (node: React.ReactNode) => React.ReactNode;
|
2016-08-19 16:43:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-19 16:53:27 +08:00
|
|
|
export interface FormComponentProps {
|
2016-09-19 17:35:17 +08:00
|
|
|
form?: WrappedFormUtils;
|
2016-08-19 16:43:32 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 15:31:29 +08:00
|
|
|
export class FormComponent extends React.Component<FormComponentProps, {}> {
|
2016-08-19 16:43:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-19 16:53:27 +08:00
|
|
|
export interface ComponentDecorator {
|
2016-08-19 16:43:32 +08:00
|
|
|
<T extends (typeof FormComponent)>(component: T): T;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Form extends React.Component<FormProps, any> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-form',
|
|
|
|
onSubmit(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
},
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
prefixCls: React.PropTypes.string,
|
2016-08-04 17:45:15 +08:00
|
|
|
vertical: React.PropTypes.bool,
|
2016-03-29 14:01:10 +08:00
|
|
|
horizontal: React.PropTypes.bool,
|
|
|
|
inline: React.PropTypes.bool,
|
|
|
|
children: React.PropTypes.any,
|
|
|
|
onSubmit: React.PropTypes.func,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2016-08-24 16:09:55 +08:00
|
|
|
static Item = FormItem;
|
|
|
|
|
|
|
|
static create = (options?: FormCreateOption): ComponentDecorator => {
|
|
|
|
const formWrapper = createDOMForm(assign({}, options, {
|
|
|
|
fieldNameProp: 'id',
|
|
|
|
fieldMetaProp: FIELD_META_PROP,
|
|
|
|
}));
|
|
|
|
|
|
|
|
/* eslint-disable react/prefer-es6-class */
|
|
|
|
return (Component) => formWrapper(React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
form: PropTypes.object.isRequired,
|
|
|
|
},
|
|
|
|
childContextTypes: {
|
|
|
|
form: PropTypes.object.isRequired,
|
|
|
|
},
|
|
|
|
getChildContext() {
|
|
|
|
return {
|
|
|
|
form: this.props.form,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
render() {
|
2016-09-01 11:48:38 +08:00
|
|
|
const getFieldProps = this.props.form.getFieldProps;
|
|
|
|
function deprecatedGetFieldProps(name, option) {
|
|
|
|
warning(
|
|
|
|
false,
|
|
|
|
'`getFieldProps` is deprecated and will be removed in future, please use `getFieldDecorator` instead'
|
|
|
|
);
|
|
|
|
return getFieldProps(name, option);
|
|
|
|
}
|
|
|
|
this.props.form.getFieldProps = deprecatedGetFieldProps;
|
2016-09-14 10:25:44 +08:00
|
|
|
|
|
|
|
const withRef: any = {};
|
|
|
|
if (options && options.withRef) {
|
|
|
|
withRef.ref = 'formWrappedComponent';
|
|
|
|
}
|
|
|
|
return <Component {...this.props} {...withRef} />;
|
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);
|
|
|
|
}
|
|
|
|
|
2015-10-09 13:53:04 +08:00
|
|
|
render() {
|
2016-08-04 17:45:15 +08:00
|
|
|
const { prefixCls, className, inline, horizontal, vertical } = this.props;
|
2015-12-25 14:30:28 +08:00
|
|
|
const formClassName = classNames({
|
2016-08-10 14:38:35 +08:00
|
|
|
[`${prefixCls}`]: true,
|
2016-07-07 12:06:05 +08:00
|
|
|
[`${prefixCls}-horizontal`]: horizontal,
|
2016-08-04 17:45:15 +08:00
|
|
|
[`${prefixCls}-vertical`]: vertical,
|
2016-07-07 12:06:05 +08:00
|
|
|
[`${prefixCls}-inline`]: inline,
|
2016-03-30 10:58:37 +08:00
|
|
|
[className]: !!className,
|
2015-12-25 14:30:28 +08:00
|
|
|
});
|
2015-10-09 13:53:04 +08:00
|
|
|
|
2016-07-07 12:06:05 +08:00
|
|
|
const formProps = omit(this.props, [
|
|
|
|
'prefixCls',
|
|
|
|
'className',
|
|
|
|
'inline',
|
|
|
|
'horizontal',
|
2016-08-04 17:45:15 +08:00
|
|
|
'vertical',
|
2016-07-07 12:06:05 +08:00
|
|
|
'form',
|
|
|
|
]);
|
|
|
|
|
|
|
|
return <form {...formProps} className={formClassName} />;
|
2015-10-09 13:53:04 +08:00
|
|
|
}
|
|
|
|
}
|