@@ -257,7 +257,7 @@ const CippStandardAccordion = ({
257257 initialConfigured [ standardName ] = isStandardConfigured (
258258 standardName ,
259259 standard ,
260- currentValues
260+ currentValues ,
261261 ) ;
262262 }
263263 } ) ;
@@ -271,6 +271,48 @@ const CippStandardAccordion = ({
271271 }
272272 } , [ watchedValues , selectedStandards , editMode ] ) ;
273273
274+ // Sync internal state when selectedStandards keys change (e.g., after re-indexing on removal)
275+ useEffect ( ( ) => {
276+ const currentKeys = Object . keys ( selectedStandards ) ;
277+ const stateKeys = Object . keys ( savedValues ) ;
278+ if ( stateKeys . length === 0 ) return ;
279+
280+ const currentSet = new Set ( currentKeys ) ;
281+ const stateSet = new Set ( stateKeys ) ;
282+
283+ const removedKeys = stateKeys . filter ( ( k ) => ! currentSet . has ( k ) ) ;
284+ const addedKeys = currentKeys . filter ( ( k ) => ! stateSet . has ( k ) ) ;
285+
286+ if ( removedKeys . length > 0 || addedKeys . length > 0 ) {
287+ setSavedValues ( ( prev ) => {
288+ const updated = { ...prev } ;
289+ removedKeys . forEach ( ( k ) => delete updated [ k ] ) ;
290+ addedKeys . forEach ( ( k ) => {
291+ const currentValues = _ . get ( watchedValues , k ) ;
292+ if ( currentValues ) {
293+ updated [ k ] = _ . cloneDeep ( currentValues ) ;
294+ }
295+ } ) ;
296+ return updated ;
297+ } ) ;
298+
299+ setConfiguredState ( ( prev ) => {
300+ const updated = { ...prev } ;
301+ removedKeys . forEach ( ( k ) => delete updated [ k ] ) ;
302+ addedKeys . forEach ( ( k ) => {
303+ const baseStandardName = k . split ( "[" ) [ 0 ] ;
304+ const standard = providedStandards . find ( ( s ) => s . name === baseStandardName ) ;
305+ const currentValues = _ . get ( watchedValues , k ) ;
306+ if ( standard && currentValues ) {
307+ updated [ k ] = isStandardConfigured ( k , standard , currentValues ) ;
308+ }
309+ } ) ;
310+ return updated ;
311+ } ) ;
312+ }
313+ // eslint-disable-next-line react-hooks/exhaustive-deps
314+ } , [ selectedStandards ] ) ;
315+
274316 // Save changes for a standard
275317 const handleSave = ( standardName , standard , current ) => {
276318 // Clone the current values to avoid reference issues
@@ -587,8 +629,8 @@ const CippStandardAccordion = ({
587629 const accordionTitle = templateDisplayName
588630 ? `${ standard . label } - ${ templateDisplayName } `
589631 : selectedTemplateName && _ . get ( selectedTemplateName , "label" )
590- ? `${ standard . label } - ${ _ . get ( selectedTemplateName , "label" ) } `
591- : standard . label ;
632+ ? `${ standard . label } - ${ _ . get ( selectedTemplateName , "label" ) } `
633+ : standard . label ;
592634
593635 // Get current values and check if they differ from saved values
594636 const current = _ . get ( watchedValues , standardName ) ;
@@ -598,7 +640,7 @@ const CippStandardAccordion = ({
598640
599641 // Check if all required fields are filled
600642 const requiredFieldsFilled = current
601- ? standard . addedComponent ?. every ( ( component ) => {
643+ ? ( standard . addedComponent ?. every ( ( component ) => {
602644 // Always skip switches regardless of their required property
603645 if ( component . type === "switch" ) return true ;
604646
@@ -630,7 +672,7 @@ const CippStandardAccordion = ({
630672 switch ( compareType ) {
631673 case "valueEq" :
632674 conditionMet = conditionValue . some (
633- ( item ) => item ?. [ propertyName ] === compareValue
675+ ( item ) => item ?. [ propertyName ] === compareValue ,
634676 ) ;
635677 break ;
636678 default :
@@ -658,7 +700,7 @@ const CippStandardAccordion = ({
658700
659701 // For other field types
660702 return ! ! fieldValue ;
661- } ) ?? true
703+ } ) ?? true )
662704 : false ;
663705
664706 // ALWAYS require an action for all standards
@@ -668,7 +710,7 @@ const CippStandardAccordion = ({
668710 const hasRequiredComponents =
669711 standard . addedComponent &&
670712 standard . addedComponent . some (
671- ( comp ) => comp . type !== "switch" && comp . required !== false
713+ ( comp ) => comp . type !== "switch" && comp . required !== false ,
672714 ) ;
673715
674716 // Action is always required and must be an array with at least one element
@@ -904,7 +946,7 @@ const CippStandardAccordion = ({
904946 component = { component }
905947 formControl = { formControl }
906948 />
907- )
949+ ) ,
908950 ) }
909951 </ >
910952 ) }
@@ -962,7 +1004,7 @@ const CippStandardAccordion = ({
9621004 component = { component }
9631005 formControl = { formControl }
9641006 />
965- )
1007+ ) ,
9661008 ) }
9671009 </ Grid >
9681010 </ Grid >
0 commit comments