forked from alibaba-fusion/next
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.jsx
More file actions
106 lines (89 loc) · 2.52 KB
/
error.jsx
File metadata and controls
106 lines (89 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import ConfigProvider from '../config-provider';
/**
* Form.Error
* @description 自定义错误展示
* @order 4
*/
class Error extends React.Component {
static propTypes = {
/**
* 表单名
*/
name: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
/**
* 自定义 field (在 Form 内不需要设置)
*/
field: PropTypes.object,
style: PropTypes.object,
className: PropTypes.string,
/**
* 自定义错误渲染, 可以是 node 或者 function(errors, state)
*/
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
prefix: PropTypes.string,
};
static defaultProps = {
prefix: 'next-',
};
static contextTypes = {
_formField: PropTypes.object,
};
static _typeMark = 'form_error';
itemRender = errors => {
return errors.length ? errors : null;
};
render() {
const {
children,
name,
prefix,
style,
className,
field: _field,
...others
} = this.props;
if (children && typeof children !== 'function') {
return <div className={`${prefix}form-item-help`}>{children}</div>;
}
const field = this.context._formField || _field;
if (!field || !name) {
return null;
}
const isSingle = typeof name === 'string';
const names = isSingle ? [name] : name;
const errorArr = [];
if (names.length) {
const errors = field.getErrors(names);
Object.keys(errors).forEach(key => {
if (errors[key]) {
errorArr.push(errors[key]);
}
});
}
let result = null;
if (typeof children === 'function') {
result = children(
errorArr,
isSingle ? field.getState(name) : undefined
);
} else {
result = this.itemRender(errorArr);
}
if (!result) {
return null;
}
const cls = classNames({
[`${prefix}form-item-help`]: true,
[className]: className,
});
return (
<div {...others} className={cls} style={style}>
{result}
</div>
);
}
}
export default ConfigProvider.config(Error);