@@ -5,38 +5,10 @@ import map from 'lodash/map';
5
5
import compact from 'lodash/compact' ;
6
6
import iteratee from 'lodash/iteratee' ;
7
7
import isArray from 'lodash/isArray' ;
8
+ import isPlainObject from 'lodash/isPlainObject' ;
8
9
9
10
import { getFieldFromState } from '../utils' ;
10
11
11
- function createErrorComponent ( component , message , modelValue , key ) {
12
- const messageString = typeof message === 'function'
13
- ? message ( modelValue )
14
- : message ;
15
-
16
- return React . createElement (
17
- component ,
18
- { key } ,
19
- messageString ) ;
20
- }
21
-
22
- function mapErrorMessages ( errors , messages , modelValue , component ) {
23
- return compact ( map ( errors , ( error , key ) => {
24
- const message = messages [ key ] ;
25
-
26
- if ( error ) {
27
- if ( message ) {
28
- return createErrorComponent ( component , message , modelValue , key ) ;
29
- } else if ( typeof error === 'string' ) {
30
- return createErrorComponent ( component , error , modelValue , key ) ;
31
- } else if ( typeof error === 'object' ) {
32
- return mapErrorMessages ( error , messages , modelValue , component ) ;
33
- }
34
- }
35
-
36
- return false ;
37
- } ) ) . reduce ( ( a , b ) => a . concat ( b ) , [ ] ) ;
38
- }
39
-
40
12
function showErrors ( field , show = true ) {
41
13
if ( typeof show === 'function' ) {
42
14
return show ( field ) ;
@@ -56,19 +28,62 @@ class Errors extends Component {
56
28
return fieldValue !== this . props . fieldValue ;
57
29
}
58
30
59
- render ( ) {
31
+ mapErrorMessages ( errors ) {
32
+ const { messages } = this . props ;
33
+
34
+ return compact ( map ( errors , ( error , key ) => {
35
+ const message = messages [ key ] ;
36
+
37
+ if ( error ) {
38
+ if ( message ) {
39
+ return this . renderError ( message , key ) ;
40
+ } else if ( typeof error === 'string' ) {
41
+ return this . renderError ( error , key ) ;
42
+ } else if ( isPlainObject ( error ) ) {
43
+ return this . mapErrorMessages ( error ) ;
44
+ }
45
+ }
46
+
47
+ return false ;
48
+ } ) ) . reduce ( ( a , b ) => a . concat ( b ) , [ ] ) ;
49
+ }
50
+
51
+ renderError ( message , key ) {
60
52
const {
61
- // model,
53
+ component,
54
+ model,
55
+ modelValue,
56
+ fieldValue,
57
+ } = this . props ;
58
+
59
+ const errorProps = {
60
+ key,
61
+ model,
62
62
modelValue,
63
63
fieldValue,
64
+ } ;
65
+
66
+ const messageString = typeof message === 'function'
67
+ ? message ( modelValue )
68
+ : message ;
69
+
70
+ if ( ! messageString ) return null ;
71
+
72
+ return React . createElement (
73
+ component ,
74
+ errorProps ,
75
+ messageString ) ;
76
+ }
77
+
78
+ render ( ) {
79
+ const {
80
+ fieldValue,
64
81
fieldValue : {
65
82
valid,
66
83
errors,
67
84
} ,
68
- messages,
69
85
show,
70
86
wrapper,
71
- component,
72
87
} = this . props ;
73
88
74
89
if ( ! showErrors ( fieldValue , show ) ) {
@@ -77,7 +92,7 @@ class Errors extends Component {
77
92
78
93
const errorMessages = valid
79
94
? null
80
- : mapErrorMessages ( errors , messages , modelValue , component ) ;
95
+ : this . mapErrorMessages ( errors ) ;
81
96
82
97
return React . createElement (
83
98
wrapper ,
@@ -87,13 +102,24 @@ class Errors extends Component {
87
102
}
88
103
89
104
Errors . propTypes = {
90
- model : PropTypes . string . isRequired ,
105
+ // Computed props
91
106
modelValue : PropTypes . any ,
92
107
fieldValue : PropTypes . object ,
93
- messages : PropTypes . object ,
108
+
109
+ // Provided props
110
+ model : PropTypes . string . isRequired ,
111
+ messages : PropTypes . objectOf ( PropTypes . oneOf ( [
112
+ PropTypes . string ,
113
+ PropTypes . func ,
114
+ PropTypes . bool ,
115
+ ] ) ) ,
94
116
show : PropTypes . any ,
95
117
wrapper : PropTypes . string ,
96
- component : PropTypes . string ,
118
+ component : PropTypes . oneOf ( [
119
+ PropTypes . string ,
120
+ PropTypes . func ,
121
+ PropTypes . element ,
122
+ ] ) ,
97
123
} ;
98
124
99
125
Errors . defaultProps = {
0 commit comments