File tree 3 files changed +52
-0
lines changed
material-experimental/mdc-tabs
3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -589,6 +589,26 @@ describe('MDC-based MatTabGroup', () => {
589
589
590
590
expect ( fixture . componentInstance . handleSelection ) . not . toHaveBeenCalled ( ) ;
591
591
} ) ) ;
592
+
593
+ it ( 'should update the newly-selected tab if the previously-selected tab is replaced' , fakeAsync ( ( ) => {
594
+ const component : MatTabGroup = fixture . debugElement . query (
595
+ By . css ( 'mat-tab-group' ) ,
596
+ ) ! . componentInstance ;
597
+
598
+ spyOn ( fixture . componentInstance , 'handleSelection' ) ;
599
+
600
+ fixture . componentInstance . tabs [ fixture . componentInstance . selectedIndex ] = {
601
+ label : 'New' ,
602
+ content : 'New' ,
603
+ } ;
604
+ fixture . detectChanges ( ) ;
605
+ tick ( ) ;
606
+
607
+ expect ( component . _tabs . get ( 1 ) ?. isActive ) . toBe ( true ) ;
608
+ expect ( fixture . componentInstance . handleSelection ) . toHaveBeenCalledWith (
609
+ jasmine . objectContaining ( { index : 1 } ) ,
610
+ ) ;
611
+ } ) ) ;
592
612
} ) ;
593
613
594
614
describe ( 'async tabs' , ( ) => {
Original file line number Diff line number Diff line change @@ -588,6 +588,26 @@ describe('MatTabGroup', () => {
588
588
589
589
expect ( fixture . componentInstance . handleSelection ) . not . toHaveBeenCalled ( ) ;
590
590
} ) ) ;
591
+
592
+ it ( 'should update the newly-selected tab if the previously-selected tab is replaced' , fakeAsync ( ( ) => {
593
+ const component : MatTabGroup = fixture . debugElement . query (
594
+ By . css ( 'mat-tab-group' ) ,
595
+ ) ! . componentInstance ;
596
+
597
+ spyOn ( fixture . componentInstance , 'handleSelection' ) ;
598
+
599
+ fixture . componentInstance . tabs [ fixture . componentInstance . selectedIndex ] = {
600
+ label : 'New' ,
601
+ content : 'New' ,
602
+ } ;
603
+ fixture . detectChanges ( ) ;
604
+ tick ( ) ;
605
+
606
+ expect ( component . _tabs . get ( 1 ) ?. isActive ) . toBe ( true ) ;
607
+ expect ( fixture . componentInstance . handleSelection ) . toHaveBeenCalledWith (
608
+ jasmine . objectContaining ( { index : 1 } ) ,
609
+ ) ;
610
+ } ) ) ;
591
611
} ) ;
592
612
593
613
describe ( 'async tabs' , ( ) => {
Original file line number Diff line number Diff line change @@ -284,16 +284,28 @@ export abstract class _MatTabGroupBase
284
284
// explicit change that selects a different tab.
285
285
if ( indexToSelect === this . _selectedIndex ) {
286
286
const tabs = this . _tabs . toArray ( ) ;
287
+ let selectedTab : MatTab | undefined ;
287
288
288
289
for ( let i = 0 ; i < tabs . length ; i ++ ) {
289
290
if ( tabs [ i ] . isActive ) {
290
291
// Assign both to the `_indexToSelect` and `_selectedIndex` so we don't fire a changed
291
292
// event, otherwise the consumer may end up in an infinite loop in some edge cases like
292
293
// adding a tab within the `selectedIndexChange` event.
293
294
this . _indexToSelect = this . _selectedIndex = i ;
295
+ selectedTab = tabs [ i ] ;
294
296
break ;
295
297
}
296
298
}
299
+
300
+ // If we haven't found an active tab and a tab exists at the selected index, it means
301
+ // that the active tab was swapped out. Since this won't be picked up by the rendering
302
+ // loop in `ngAfterContentChecked`, we need to sync it up manually.
303
+ if ( ! selectedTab && tabs [ indexToSelect ] ) {
304
+ Promise . resolve ( ) . then ( ( ) => {
305
+ tabs [ indexToSelect ] . isActive = true ;
306
+ this . selectedTabChange . emit ( this . _createChangeEvent ( indexToSelect ) ) ;
307
+ } ) ;
308
+ }
297
309
}
298
310
299
311
this . _changeDetectorRef . markForCheck ( ) ;
You can’t perform that action at this time.
0 commit comments