@@ -6,52 +6,56 @@ import get from 'lodash/get';
6
6
7
7
const isEmptyValue = ( value ) => ( typeof value === 'number' || value === true ? false : lodashIsEmpty ( value ) ) ;
8
8
9
- const Condition = ( { condition, children } ) => {
10
- const fieldCondition = ( value , { is, isNotEmpty, isEmpty, pattern, notMatch, flags } ) => {
11
- if ( isNotEmpty ) {
12
- return ! isEmptyValue ( value ) ;
13
- }
14
-
15
- if ( isEmpty ) {
16
- return isEmptyValue ( value ) ;
17
- }
18
-
19
- if ( pattern ) {
20
- const regExpPattern = RegExp ( pattern , flags ) ;
21
-
22
- return notMatch ? ! regExpPattern . test ( value ) : regExpPattern . test ( value ) ;
23
- }
24
-
25
- const isMatched = Array . isArray ( is ) ? ! ! is . includes ( value ) : value === is ;
26
-
27
- return notMatch ? ! isMatched : isMatched ;
28
- } ;
29
-
30
- const shouldRender = ( values , conditionItem ) => {
31
- if ( typeof conditionItem . when === 'string' ) {
32
- return fieldCondition ( get ( values , conditionItem . when ) , conditionItem ) ;
33
- }
34
-
35
- if ( Array . isArray ( conditionItem . when ) ) {
36
- return conditionItem . when . map ( ( fieldName ) => fieldCondition ( get ( values , fieldName ) , conditionItem ) ) . find ( ( condition ) => ! ! condition ) ;
37
- }
38
-
39
- return false ;
40
- } ;
41
-
42
- return (
43
- < FormSpy >
44
- { ( { values } ) => {
45
- const visible = Array . isArray ( condition )
46
- ? ! condition . map ( ( conditionItem ) => ! ! shouldRender ( values , conditionItem ) ) . some ( ( result ) => result === false )
47
- : shouldRender ( values , condition ) ;
48
-
49
- return visible ? children : null ;
50
- } }
51
- </ FormSpy >
52
- ) ;
9
+ const fieldCondition = ( value , { is, isNotEmpty, isEmpty, pattern, notMatch, flags } ) => {
10
+ if ( isNotEmpty ) {
11
+ return ! isEmptyValue ( value ) ;
12
+ }
13
+
14
+ if ( isEmpty ) {
15
+ return isEmptyValue ( value ) ;
16
+ }
17
+
18
+ if ( pattern ) {
19
+ const regExpPattern = RegExp ( pattern , flags ) ;
20
+
21
+ return notMatch ? ! regExpPattern . test ( value ) : regExpPattern . test ( value ) ;
22
+ }
23
+
24
+ const isMatched = Array . isArray ( is ) ? ! ! is . includes ( value ) : value === is ;
25
+
26
+ return notMatch ? ! isMatched : isMatched ;
27
+ } ;
28
+
29
+ export const parseCondition = ( condition , values ) => {
30
+ if ( Array . isArray ( condition ) ) {
31
+ return ! condition . map ( ( condition ) => parseCondition ( condition , values ) ) . some ( ( result ) => result === false ) ;
32
+ }
33
+
34
+ if ( condition . and ) {
35
+ return condition . and . map ( ( condition ) => parseCondition ( condition , values ) ) . every ( ( result ) => result === true ) ;
36
+ }
37
+
38
+ if ( condition . or ) {
39
+ return condition . or . map ( ( condition ) => parseCondition ( condition , values ) ) . some ( ( result ) => result === true ) ;
40
+ }
41
+
42
+ if ( condition . not ) {
43
+ return ! parseCondition ( condition . not , values ) ;
44
+ }
45
+
46
+ if ( typeof condition . when === 'string' ) {
47
+ return fieldCondition ( get ( values , condition . when ) , condition ) ;
48
+ }
49
+
50
+ if ( Array . isArray ( condition . when ) ) {
51
+ return ! ! condition . when . map ( ( fieldName ) => fieldCondition ( get ( values , fieldName ) , condition ) ) . find ( ( condition ) => ! ! condition ) ;
52
+ }
53
+
54
+ return false ;
53
55
} ;
54
56
57
+ const Condition = ( { condition, children } ) => < FormSpy > { ( { values } ) => ( parseCondition ( condition , values ) ? children : null ) } </ FormSpy > ;
58
+
55
59
const conditionProps = {
56
60
when : PropTypes . string . isRequired ,
57
61
is : PropTypes . oneOfType ( [ PropTypes . array , PropTypes . string , PropTypes . object , PropTypes . number , PropTypes . bool ] ) . isRequired ,
0 commit comments