@@ -301,14 +301,20 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
301301 public selectedIndexes : number [ ] = [ ] ;
302302
303303 protected buttonClickNotifier$ = new Subject < boolean > ( ) ;
304- protected buttonSelectedNotifier$ = new Subject < boolean > ( ) ;
305304 protected queryListNotifier$ = new Subject < boolean > ( ) ;
306305
307306 private _isVertical : boolean ;
308307 private _itemContentCssClass : string ;
309308 private _disabled = false ;
310309 private _selectionMode : 'single' | 'singleRequired' | 'multi' = 'single' ;
311310
311+ private mutationObserver : MutationObserver ;
312+ private observerConfig : MutationObserverInit = {
313+ attributeFilter : [ "data-selected" ] ,
314+ childList : true ,
315+ subtree : true ,
316+ } ;
317+
312318 constructor (
313319 private _cdr : ChangeDetectorRef ,
314320 private _renderer : Renderer2 ,
@@ -350,6 +356,8 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
350356 return ;
351357 }
352358
359+ this . updateSelected ( index ) ;
360+
353361 const button = this . buttons [ index ] ;
354362 button . select ( ) ;
355363 }
@@ -365,25 +373,21 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
365373 this . selectedIndexes . push ( index ) ;
366374 }
367375
368- if ( button . selected ) {
369- this . _renderer . setAttribute ( button . nativeElement , 'aria-pressed' , 'true' ) ;
370- this . _renderer . addClass ( button . nativeElement , 'igx-button-group__item--selected' ) ;
376+ this . _renderer . setAttribute ( button . nativeElement , 'aria-pressed' , 'true' ) ;
377+ this . _renderer . addClass ( button . nativeElement , 'igx-button-group__item--selected' ) ;
371378
372- const indexInViewButtons = this . viewButtons . toArray ( ) . indexOf ( button ) ;
373- if ( indexInViewButtons !== - 1 ) {
379+ const indexInViewButtons = this . viewButtons . toArray ( ) . indexOf ( button ) ;
380+ if ( indexInViewButtons !== - 1 ) {
374381 this . values [ indexInViewButtons ] . selected = true ;
375- }
382+ }
376383
377- // deselect other buttons if selectionMode is not multi
378- if ( this . selectionMode !== 'multi' && this . selectedIndexes . length > 1 ) {
379- this . buttons . forEach ( ( _ , i ) => {
380- if ( i !== index && this . selectedIndexes . indexOf ( i ) !== - 1 ) {
381- this . deselectButton ( i ) ;
382- }
383- } ) ;
384- }
385- } else {
386- this . deselectButton ( index ) ;
384+ // deselect other buttons if selectionMode is not multi
385+ if ( this . selectionMode !== 'multi' && this . selectedIndexes . length > 1 ) {
386+ this . buttons . forEach ( ( _ , i ) => {
387+ if ( i !== index && this . selectedIndexes . indexOf ( i ) !== - 1 ) {
388+ this . deselectButton ( i ) ;
389+ }
390+ } ) ;
387391 }
388392 }
389393
@@ -453,9 +457,6 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
453457 }
454458
455459 button . buttonClick . pipe ( takeUntil ( this . buttonClickNotifier$ ) ) . subscribe ( ( _ ) => this . _clickHandler ( index ) ) ;
456- button . buttonSelected
457- . pipe ( takeUntil ( this . buttonSelectedNotifier$ ) )
458- . subscribe ( ( _ ) => this . updateSelected ( index ) ) ;
459460 } ) ;
460461 } ;
461462
@@ -464,6 +465,10 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
464465 initButtons ( ) ;
465466
466467 this . _cdr . detectChanges ( ) ;
468+
469+ this . mutationObserver = this . setMutationsObserver ( ) ;
470+
471+ this . mutationObserver . observe ( this . _el . nativeElement , this . observerConfig ) ;
467472 }
468473
469474 /**
@@ -473,17 +478,18 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
473478 this . buttonClickNotifier$ . next ( ) ;
474479 this . buttonClickNotifier$ . complete ( ) ;
475480
476- this . buttonSelectedNotifier$ . next ( ) ;
477- this . buttonSelectedNotifier$ . complete ( ) ;
478-
479481 this . queryListNotifier$ . next ( ) ;
480482 this . queryListNotifier$ . complete ( ) ;
483+
484+ this . mutationObserver . disconnect ( ) ;
481485 }
482486
483487 /**
484488 * @hidden
485489 */
486490 public _clickHandler ( index : number ) {
491+ this . mutationObserver . disconnect ( ) ;
492+
487493 const button = this . buttons [ index ] ;
488494 const args : IButtonGroupEventArgs = { owner : this , button, index } ;
489495
@@ -504,6 +510,54 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
504510 this . deselected . emit ( args ) ;
505511 }
506512 }
513+
514+ this . mutationObserver . observe ( this . _el . nativeElement , this . observerConfig ) ;
515+ }
516+
517+ private setMutationsObserver ( ) {
518+ return new MutationObserver ( ( records , observer ) => {
519+ // Stop observing while handling changes
520+ observer . disconnect ( ) ;
521+
522+ const updatedButtons = this . getUpdatedButtons ( records ) ;
523+
524+ if ( updatedButtons . length > 0 ) {
525+ updatedButtons . forEach ( ( button ) => {
526+ const index = this . buttons . map ( ( b ) => b . nativeElement ) . indexOf ( button ) ;
527+ const args : IButtonGroupEventArgs = { owner : this , button : this . buttons [ index ] , index } ;
528+
529+ this . updateButtonSelectionState ( index , args ) ;
530+ } ) ;
531+ }
532+
533+ // Watch for changes again
534+ observer . observe ( this . _el . nativeElement , this . observerConfig ) ;
535+ } ) ;
536+ }
537+
538+ private getUpdatedButtons ( records : MutationRecord [ ] ) {
539+ const updated : HTMLButtonElement [ ] = [ ] ;
540+
541+ records
542+ . filter ( ( x ) => x . type === 'attributes' )
543+ . reduce ( ( prev , curr ) => {
544+ prev . push (
545+ curr . target as HTMLButtonElement
546+ ) ;
547+ return prev ;
548+ } , updated ) ;
549+
550+ return updated ;
551+ }
552+
553+ private updateButtonSelectionState ( index : number , args : IButtonGroupEventArgs ) {
554+ if ( this . selectedIndexes . indexOf ( index ) === - 1 ) {
555+ this . selectButton ( index ) ;
556+ this . selected . emit ( args ) ;
557+ } else {
558+ this . deselectButton ( index ) ;
559+ this . deselected . emit ( args ) ;
560+ }
507561 }
508562}
509563
0 commit comments