ant-design/components/form/Form.tsx

133 lines
4.1 KiB
TypeScript
Raw Normal View History

2016-07-07 20:25:03 +08:00
import * as React from 'react';
import classNames from 'classnames';
import PureRenderMixin from 'react-addons-pure-render-mixin';
2016-07-07 12:06:05 +08:00
import omit from 'object.omit';
2016-07-29 13:59:37 +08:00
import warning from 'warning';
import FormItem from './FormItem';
2015-10-09 13:53:04 +08:00
2016-08-19 16:53:27 +08:00
export interface FormCreateOption {
onFieldsChange?: (props: any, fields: Array<any>) => void;
/** 把 props 转为对应的值,可用于把 Redux store 中的值读出 */
mapPropsToFields?: (props: any) => void;
}
2016-08-19 16:53:27 +08:00
export interface FormProps {
prefixCls?: string;
/** 水平排列布局*/
horizontal?: boolean;
/** 行内排列布局*/
inline?: boolean;
/** 经 `Form.create()` 包装过的组件会自带 `this.props.form` 属性,直接传给 Form 即可*/
form?: Object;
/** 数据验证成功后回调事件*/
onSubmit?: React.FormEventHandler;
style?: React.CSSProperties;
className?: string;
vertical?: boolean;
}
// function create
2016-08-22 17:26:14 +08:00
export type WrappedFormUtils = {
/** 获取一组输入控件的值,如不传入参数,则获取全部组件的值*/
2016-08-22 17:26:14 +08:00
getFieldsValue(fieldNames?: Array<string>): Object;
/** 获取一个输入控件的值*/
2016-08-22 17:26:14 +08:00
getFieldValue(fieldName: string): any;
/** 设置一组输入控件的值*/
2016-08-22 17:26:14 +08:00
setFieldsValue(obj: Object): void;
/** 设置一组输入控件的值*/
2016-08-22 17:26:14 +08:00
setFields(obj: Object): void;
/** 校验并获取一组输入域的值与 Error*/
2016-08-22 17:26:14 +08:00
validateFields(fieldNames?: Array<string>, options?: Object, callback?: (erros: any, values: any) => void): any;
/** 与 `validateFields` 相似,但校验完后,如果校验不通过的菜单域不在可见范围内,则自动滚动进可见范围 */
2016-08-22 17:26:14 +08:00
validateFieldsAndScroll(
fieldNames?: Array<string>,
options?: Object,
callback?: (erros: any, values: any) => void
2016-08-22 17:26:14 +08:00
): void;
/** 获取某个输入控件的 Error */
2016-08-22 17:26:14 +08:00
getFieldError(name: string): Object[];
/** 判断一个输入控件是否在校验状态*/
2016-08-22 17:26:14 +08:00
isFieldValidating(name: string): boolean;
/**重置一组输入控件的值与状态,如不传入参数,则重置所有组件*/
2016-08-22 17:26:14 +08:00
resetFields(names?: Array<string>): void;
2016-08-22 17:26:14 +08:00
getFieldProps(id: string, options: {
/** 子节点的值的属性,如 Checkbox 的是 'checked'*/
valuePropName?: string;
/** 子节点的初始值,类型、可选值均由子节点决定*/
initialValue?: any;
/** 收集子节点的值的时机*/
trigger?: string;
/** 校验子节点值的时机*/
validateTrigger?: string;
/** 校验规则,参见 [async-validator](https://github.com/yiminghe/async-validator) */
rules?: Array<any>;
/** 必填输入控件唯一标志*/
id?: string;
2016-08-22 17:26:14 +08:00
}): Array<any>;
}
2016-08-19 16:53:27 +08:00
export interface FormComponentProps {
2016-08-22 17:26:14 +08:00
form: WrappedFormUtils;
}
export class FormComponent extends React.Component<FormComponentProps, {}> {
}
2016-08-19 16:53:27 +08:00
export interface ComponentDecorator {
<T extends (typeof FormComponent)>(component: T): T;
}
export default class Form extends React.Component<FormProps, any> {
static Item: typeof FormItem;
static defaultProps = {
prefixCls: 'ant-form',
onSubmit(e) {
e.preventDefault();
},
2016-07-13 11:14:24 +08:00
};
static propTypes = {
prefixCls: React.PropTypes.string,
2016-08-04 17:45:15 +08:00
vertical: React.PropTypes.bool,
horizontal: React.PropTypes.bool,
inline: React.PropTypes.bool,
children: React.PropTypes.any,
onSubmit: React.PropTypes.func,
2016-07-13 11:14:24 +08:00
};
static create(options?: FormCreateOption): ComponentDecorator;
2016-07-29 13:59:37 +08:00
constructor(props) {
super(props);
warning(!props.form, 'It is unnecessary to pass `form` to `Form` after antd@1.7.0.');
2016-07-29 13:59:37 +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;
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-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
}
}