@@ -91,7 +91,7 @@ function evaluateFieldDependents(field: FormField, values: any, context: FormCon
9191 // evaluate hide
9292 if ( dependent . hide ) {
9393 const targetSection = findFieldSection ( formJson , dependent ) ;
94- // console.log('targetSection', targetSection );
94+ const isSectionVisible = targetSection ?. questions . some ( ( question ) => ! question . isHidden ) ;
9595
9696 evaluateHide (
9797 { value : dependent , type : 'field' } ,
@@ -102,14 +102,24 @@ function evaluateFieldDependents(field: FormField, values: any, context: FormCon
102102 evaluateExpression ,
103103 updateFormField ,
104104 ) ;
105- // console.log('dependent', dependent);
106- let hasVisibleFields = targetSection ?. questions . some ( ( question ) => ! question . isHidden ) ;
107- // console.log('hasVisibleFields', hasVisibleFields);
108- // console.log('dependent.isHidden', dependent.isHidden);
109- if ( ! hasVisibleFields && ! dependent . isHidden ) {
110- // console.log('FORCE RE-RENDER');
105+ targetSection . questions = targetSection . questions . map ( ( question ) => {
106+ if ( question . id === dependent . id ) {
107+ return dependent ;
108+ }
109+ return question ;
110+ } ) ;
111+
112+ const isDependentFieldHidden = dependent . isHidden ;
113+ const sectionHasVisibleFieldAfterEvaluation = [ ...targetSection . questions , dependent ] . some (
114+ ( field ) => ! field . isHidden ,
115+ ) ;
116+
117+ if ( ! isSectionVisible && ! isDependentFieldHidden ) {
111118 targetSection . isHidden = false ;
112119 shouldUpdateForm = true ;
120+ } else if ( isSectionVisible && ! sectionHasVisibleFieldAfterEvaluation ) {
121+ targetSection . isHidden = true ;
122+ shouldUpdateForm = true ;
113123 }
114124 }
115125 // evaluate disabled
@@ -201,8 +211,6 @@ function evaluateFieldDependents(field: FormField, values: any, context: FormCon
201211 } ,
202212 ) ;
203213 }
204- // shouldUpdateForm = true;
205- // console.log('shouldUpdateForm', shouldUpdateForm);
206214 updateFormField ( dependent ) ;
207215 } ) ;
208216 }
@@ -247,7 +255,6 @@ function evaluateFieldDependents(field: FormField, values: any, context: FormCon
247255 }
248256
249257 if ( shouldUpdateForm ) {
250- // console.log('updating form', formJson);
251258 setForm ( formJson ) ;
252259 }
253260}
0 commit comments