UNPKG

2.86 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
3import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
4import _inherits from 'babel-runtime/helpers/inherits';
5
6var _class, _temp2;
7
8import React from 'react';
9import PropTypes from 'prop-types';
10import Button from '../button';
11import { func, obj } from '../util';
12
13/**
14 * Form.Submit
15 * @description 继承 Button API
16 * @order 2
17 */
18var Submit = (_temp2 = _class = function (_React$Component) {
19 _inherits(Submit, _React$Component);
20
21 function Submit() {
22 var _temp, _this, _ret;
23
24 _classCallCheck(this, Submit);
25
26 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
27 args[_key] = arguments[_key];
28 }
29
30 return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.handleClick = function () {
31 var _this$props = _this.props,
32 onClick = _this$props.onClick,
33 validate = _this$props.validate;
34
35 var field = _this.context._formField || _this.props.field;
36
37 if (!field) {
38 onClick();
39 return;
40 }
41
42 if (validate === true) {
43 field.validate(function (errors) {
44 onClick(field.getValues(), errors, field);
45 });
46 } else if (Array.isArray(validate)) {
47 field.validate(validate, function (errors) {
48 onClick(field.getValues(), errors, field);
49 });
50 } else {
51 onClick(field.getValues(), null, field);
52 }
53 }, _temp), _possibleConstructorReturn(_this, _ret);
54 }
55
56 Submit.prototype.render = function render() {
57 var children = this.props.children;
58
59
60 return React.createElement(
61 Button,
62 _extends({}, obj.pickOthers(Submit.propTypes, this.props), {
63 onClick: this.handleClick
64 }),
65 children
66 );
67 };
68
69 return Submit;
70}(React.Component), _class.propTypes = {
71 /**
72 * 点击提交后触发
73 * @param {Object} value 数据
74 * @param {Object} errors 错误数据
75 * @param {class} field 实例
76 */
77 onClick: PropTypes.func,
78 /**
79 * 是否校验/需要校验的 name 数组
80 */
81 validate: PropTypes.oneOfType([PropTypes.bool, PropTypes.array]),
82 /**
83 * 自定义 field (在 Form 内不需要设置)
84 */
85 field: PropTypes.object,
86 children: PropTypes.node
87}, _class.defaultProps = {
88 onClick: func.noop
89}, _class.contextTypes = {
90 _formField: PropTypes.object
91}, _temp2);
92Submit.displayName = 'Submit';
93
94
95export default Submit;
\No newline at end of file