@@ -606,16 +606,103 @@ const checkType = (value: string, type: string | undefined): string => {
606606
607607 const removeComponent = ( indexToRemove : number ) => {
608608 if ( indexToRemove === - 1 ) return ;
609- setFilteredOptions ( { } ) ;
610- setFilteredValueOptions ( { } ) ;
611- const childToRemove = children [ indexToRemove ] ;
612- const newFilter = props . source . filter ?. replace ( childToRemove . filter , '' ) . trim ( ) ;
613- setSource ( prevSource => {
614- const newSource = { ...prevSource , filter : newFilter } ;
615- onSourceChange ( newSource , partId ) ;
616- return newSource ;
617- } ) ;
618- setChildren ( prevChildren => prevChildren . filter ( ( _ , index ) => index !== indexToRemove ) ) ;
609+ if ( groupingEnabled ) {
610+ let clonedNewGroups : Group [ ] = JSON . parse ( JSON . stringify ( groups ) ) ;
611+ const selectedItems = items . filter ( ( item , index ) => selectedIndices . includes ( index ) ) ;
612+ const groupIndex = groups . findIndex ( group =>
613+ group . children ?. some ( child =>
614+ child . items . some ( item =>
615+ JSON . stringify ( item ) === JSON . stringify ( items [ selectedIndices [ 0 ] ] )
616+ )
617+ ) || group . items ?. some ( item =>
618+ JSON . stringify ( item ) === JSON . stringify ( items [ selectedIndices [ 0 ] ] )
619+ )
620+ ) ;
621+ const childIndex = groupIndex !== - 1 ? groups [ groupIndex ] . children . findIndex ( child =>
622+ child . items . some ( item =>
623+ JSON . stringify ( item ) === JSON . stringify ( items [ selectedIndices [ 0 ] ] )
624+ )
625+ ) : - 1 ;
626+
627+ const ifGroupItem = groups . some ( group => isGroupItem ( group , items [ selectedIndices [ 0 ] ] ) ) ;
628+ const ifGroupChild = groups . some ( group => isGroupChild ( group , items [ selectedIndices [ 0 ] ] ) ) ;
629+
630+ if ( ifGroupItem && groups [ groupIndex ] . items [ indexToRemove ?? 0 ] ) {
631+ console . log ( "it's a group item" , groups [ groupIndex ] . items [ indexToRemove ?? 0 ] ) ;
632+ if ( groups [ groupIndex ] . children . length > 0 ) { return ; }
633+ const childItems = selectedItems ;
634+ const filterItems = ( groupIndex : number ) => {
635+ clonedNewGroups [ groupIndex ] . items = clonedNewGroups [ groupIndex ] . items . filter ( ( item : { attribute : string ; equalityOperator : string ; value : string ; andOr : string ; } ) =>
636+ ! childItems . some ( childItem => item . attribute === childItem . attribute && item . equalityOperator === childItem . equalityOperator && item . value === childItem . value && item . andOr === childItem . andOr ) ) ;
637+ if ( clonedNewGroups [ groupIndex ] . items . length === 0 ) {
638+ clonedNewGroups [ groupIndex ] . andOr = "" ;
639+ //if last group, delete andOr from previous group / previous group's last child
640+ if ( groupIndex === groups . length - 1 ) {
641+ const previousGroupIndex = groupIndex - 1 ;
642+ if ( previousGroupIndex >= 0 ) {
643+ const prevGroupChildren = clonedNewGroups [ previousGroupIndex ] . children ;
644+ if ( prevGroupChildren . length > 0 ) {
645+ clonedNewGroups [ previousGroupIndex ] . children [ clonedNewGroups [ previousGroupIndex ] . children . length - 1 ] . andOr = "" ;
646+ }
647+ else {
648+ clonedNewGroups [ previousGroupIndex ] . andOr = "" ;
649+ }
650+ }
651+ }
652+ }
653+ } ;
654+ filterItems ( groupIndex ) ;
655+ }
656+
657+ if ( ifGroupChild && groups [ groupIndex ] . children [ childIndex ] . items [ indexToRemove ?? 0 ] ) {
658+ console . log ( "it's a child item" , groups [ groupIndex ] . children [ childIndex ] . items [ indexToRemove ?? 0 ] ) ;
659+ const childItems = selectedItems ;
660+ const filterChildren = ( groupIndex : number , childIndex : number ) => {
661+ clonedNewGroups [ groupIndex ] . children [ childIndex ] . items = clonedNewGroups [ groupIndex ] . children [ childIndex ] . items . filter ( ( item : { attribute : string ; equalityOperator : string ; value : string ; andOr : string ; } ) =>
662+ ! childItems . some ( childItem => item . attribute === childItem . attribute && item . equalityOperator === childItem . equalityOperator && item . value === childItem . value && item . andOr === childItem . andOr ) ) ;
663+ if ( clonedNewGroups [ groupIndex ] . children [ childIndex ] . items . length === 0 ) {
664+ clonedNewGroups [ groupIndex ] . children [ childIndex ] . andOr = "" ;
665+ if ( groupIndex === groups . length - 1 && childIndex === clonedNewGroups [ groupIndex ] . children . length - 1 ) {
666+ //if it's last group and last child, delete andOr from previous child / current group
667+ const previousChildIndex = childIndex - 1 ;
668+ if ( previousChildIndex >= 0 ) {
669+ clonedNewGroups [ groupIndex ] . children [ previousChildIndex ] . andOr = "" ;
670+ }
671+ else {
672+ clonedNewGroups [ groupIndex ] . andOr = "" ;
673+ }
674+ }
675+ }
676+ } ;
677+ filterChildren ( groupIndex , childIndex ) ;
678+ }
679+
680+ clonedNewGroups = clonedNewGroups . filter ( ( group : { items : any [ ] ; children : any [ ] ; } ) =>
681+ group . items . length > 0 || group . children . some ( ( child : { items : any [ ] ; } ) => child . items . length > 0 )
682+ ) ;
683+
684+ clonedNewGroups . forEach ( ( group : { children : any [ ] ; andOr : string } ) => {
685+ group . children = group . children . filter ( ( child : { items : any [ ] ; } ) => child . items . length > 0 ) ;
686+ } ) ;
687+
688+ setGroups ( clonedNewGroups ) ;
689+ getGroupLabels ( clonedNewGroups ) ;
690+ setSelectedIndices ( [ ] ) ;
691+ selection . setAllSelected ( false ) ;
692+ }
693+ else
694+ {
695+ setFilteredOptions ( { } ) ;
696+ setFilteredValueOptions ( { } ) ;
697+ const childToRemove = children [ indexToRemove ] ;
698+ const newFilter = props . source . filter ?. replace ( childToRemove . filter , '' ) . trim ( ) ;
699+ setSource ( prevSource => {
700+ const newSource = { ...prevSource , filter : newFilter } ;
701+ onSourceChange ( newSource , partId ) ;
702+ return newSource ;
703+ } ) ;
704+ setChildren ( prevChildren => prevChildren . filter ( ( _ , index ) => index !== indexToRemove ) ) ;
705+ }
619706 } ;
620707
621708 const yesNoOptions : IChoiceGroupOption [ ] = [
@@ -1339,14 +1426,14 @@ const checkType = (value: string, type: string | undefined): string => {
13391426 ) ;
13401427 case 'remove' :
13411428 return (
1342- ! groupingEnabled ? (
1429+
13431430 < ActionButton
13441431 className = { classNames . removeButton }
13451432 iconProps = { { iconName : "Blocked2" } }
13461433 onClick = { ( ) => removeComponent ( index ?? - 1 ) } >
13471434 { strings . remove }
13481435 </ ActionButton >
1349- ) : ( < div /> )
1436+
13501437 ) ;
13511438 default :
13521439 return (
0 commit comments