UNPKG

3.77 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6
7var _class, _temp2;
8
9import React from 'react';
10import PropTypes from 'prop-types';
11import classNames from 'classnames';
12import ConfigProvider from '../config-provider';
13
14/**
15 * Form.Error
16 * @description 自定义错误展示
17 * @order 4
18 */
19var Error = (_temp2 = _class = function (_React$Component) {
20 _inherits(Error, _React$Component);
21
22 function Error() {
23 var _temp, _this, _ret;
24
25 _classCallCheck(this, Error);
26
27 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
28 args[_key] = arguments[_key];
29 }
30
31 return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.itemRender = function (errors) {
32 return errors.length ? errors : null;
33 }, _temp), _possibleConstructorReturn(_this, _ret);
34 }
35
36 Error.prototype.render = function render() {
37 var _classNames;
38
39 var _props = this.props,
40 children = _props.children,
41 name = _props.name,
42 prefix = _props.prefix,
43 style = _props.style,
44 className = _props.className,
45 _field = _props.field,
46 others = _objectWithoutProperties(_props, ['children', 'name', 'prefix', 'style', 'className', 'field']);
47
48 if (children && typeof children !== 'function') {
49 return React.createElement(
50 'div',
51 { className: prefix + 'form-item-help' },
52 children
53 );
54 }
55
56 var field = this.context._formField || _field;
57
58 if (!field || !name) {
59 return null;
60 }
61
62 var isSingle = typeof name === 'string';
63
64 var names = isSingle ? [name] : name;
65 var errorArr = [];
66
67 if (names.length) {
68 var errors = field.getErrors(names);
69 Object.keys(errors).forEach(function (key) {
70 if (errors[key]) {
71 errorArr.push(errors[key]);
72 }
73 });
74 }
75
76 var result = null;
77 if (typeof children === 'function') {
78 result = children(errorArr, isSingle ? field.getState(name) : undefined);
79 } else {
80 result = this.itemRender(errorArr);
81 }
82
83 if (!result) {
84 return null;
85 }
86
87 var cls = classNames((_classNames = {}, _classNames[prefix + 'form-item-help'] = true, _classNames[className] = className, _classNames));
88
89 return React.createElement(
90 'div',
91 _extends({}, others, { className: cls, style: style }),
92 result
93 );
94 };
95
96 return Error;
97}(React.Component), _class.propTypes = {
98 /**
99 * 表单名
100 */
101 name: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
102 /**
103 * 自定义 field (在 Form 内不需要设置)
104 */
105 field: PropTypes.object,
106 style: PropTypes.object,
107 className: PropTypes.string,
108 /**
109 * 自定义错误渲染, 可以是 node 或者 function(errors, state)
110 */
111 children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
112 prefix: PropTypes.string
113}, _class.defaultProps = {
114 prefix: 'next-'
115}, _class.contextTypes = {
116 _formField: PropTypes.object
117}, _class._typeMark = 'form_error', _temp2);
118Error.displayName = 'Error';
119
120
121export default ConfigProvider.config(Error);
\No newline at end of file