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