@@ -6,53 +6,60 @@ import get from 'lodash/get';
66
77const isEmptyValue = ( value ) => typeof value === 'number' || value === true ? false : lodashIsEmpty ( value ) ;
88
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+ }
1413
15- if ( isEmpty ) {
16- return isEmptyValue ( value ) ;
17- }
14+ if ( isEmpty ) {
15+ return isEmptyValue ( value ) ;
16+ }
1817
19- if ( pattern ) {
20- const regExpPattern = RegExp ( pattern , flags ) ;
18+ if ( pattern ) {
19+ const regExpPattern = RegExp ( pattern , flags ) ;
2120
22- return notMatch ? ! regExpPattern . test ( value ) : regExpPattern . test ( value ) ;
23- }
21+ return notMatch ? ! regExpPattern . test ( value ) : regExpPattern . test ( value ) ;
22+ }
2423
25- const isMatched = Array . isArray ( is ) ? ! ! is . includes ( value ) : value === is ;
24+ const isMatched = Array . isArray ( is ) ? ! ! is . includes ( value ) : value === is ;
2625
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+ }
2933
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+ }
3437
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+ }
3841
39- return false ;
40- } ;
42+ if ( condition . not ) {
43+ return ! parseCondition ( condition . not , values ) ;
44+ }
4145
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+ }
4849
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+ }
5353
54+ return false ;
5455} ;
5556
57+ const Condition = ( { condition, children } ) => (
58+ < FormSpy >
59+ { ( { values } ) => parseCondition ( condition , values ) ? children : null }
60+ </ FormSpy >
61+ ) ;
62+
5663const conditionProps = {
5764 when : PropTypes . string . isRequired ,
5865 is : PropTypes . oneOfType ( [
0 commit comments