1 | import _extends from 'babel-runtime/helpers/extends';
|
2 | import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
|
3 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
4 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
5 | import _inherits from 'babel-runtime/helpers/inherits';
|
6 |
|
7 | var _class, _temp2;
|
8 |
|
9 | import React from 'react';
|
10 | import PropTypes from 'prop-types';
|
11 | import classNames from 'classnames';
|
12 | import ConfigProvider from '../config-provider';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | var 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 |
|
104 |
|
105 | field: PropTypes.object,
|
106 | style: PropTypes.object,
|
107 | className: PropTypes.string,
|
108 | |
109 |
|
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);
|
118 | Error.displayName = 'Error';
|
119 |
|
120 |
|
121 | export default ConfigProvider.config(Error); |
\ | No newline at end of file |