File tree Expand file tree Collapse file tree 3 files changed +54
-1
lines changed
material-experimental/mdc-tabs Expand file tree Collapse file tree 3 files changed +54
-1
lines changed Original file line number Diff line number Diff line change @@ -308,6 +308,28 @@ describe('MDC-based MatTabHeader', () => {
308
308
. withContext ( 'Expected no ripple to show up after mousedown' )
309
309
. toBe ( 0 ) ;
310
310
} ) ;
311
+
312
+ it ( 'should update the scroll distance if a tab is removed and no tabs are selected' , fakeAsync ( ( ) => {
313
+ appComponent . selectedIndex = 0 ;
314
+ appComponent . addTabsForScrolling ( ) ;
315
+ fixture . detectChanges ( ) ;
316
+
317
+ // Focus the last tab so the header scrolls to the end.
318
+ appComponent . tabHeader . focusIndex = appComponent . tabs . length - 1 ;
319
+ fixture . detectChanges ( ) ;
320
+ expect ( appComponent . tabHeader . scrollDistance ) . toBe (
321
+ appComponent . tabHeader . _getMaxScrollDistance ( ) ,
322
+ ) ;
323
+
324
+ // Remove the first two tabs which includes the selected tab.
325
+ appComponent . tabs = appComponent . tabs . slice ( 2 ) ;
326
+ fixture . detectChanges ( ) ;
327
+ tick ( ) ;
328
+
329
+ expect ( appComponent . tabHeader . scrollDistance ) . toBe (
330
+ appComponent . tabHeader . _getMaxScrollDistance ( ) ,
331
+ ) ;
332
+ } ) ) ;
311
333
} ) ;
312
334
313
335
describe ( 'rtl' , ( ) => {
Original file line number Diff line number Diff line change @@ -213,7 +213,16 @@ export abstract class MatPaginatedTabHeader
213
213
// We need to defer this to give the browser some time to recalculate
214
214
// the element dimensions. The call has to be wrapped in `NgZone.run`,
215
215
// because the viewport change handler runs outside of Angular.
216
- this . _ngZone . run ( ( ) => Promise . resolve ( ) . then ( realign ) ) ;
216
+ this . _ngZone . run ( ( ) => {
217
+ Promise . resolve ( ) . then ( ( ) => {
218
+ // Clamp the scroll distance, because it can change with the number of tabs.
219
+ this . _scrollDistance = Math . max (
220
+ 0 ,
221
+ Math . min ( this . _getMaxScrollDistance ( ) , this . _scrollDistance ) ,
222
+ ) ;
223
+ realign ( ) ;
224
+ } ) ;
225
+ } ) ;
217
226
this . _keyManager . withHorizontalOrientation ( this . _getLayoutDirection ( ) ) ;
218
227
} ) ;
219
228
Original file line number Diff line number Diff line change @@ -305,6 +305,28 @@ describe('MatTabHeader', () => {
305
305
. withContext ( 'Expected no ripple to show up after mousedown' )
306
306
. toBe ( 0 ) ;
307
307
} ) ;
308
+
309
+ it ( 'should update the scroll distance if a tab is removed and no tabs are selected' , fakeAsync ( ( ) => {
310
+ appComponent . selectedIndex = 0 ;
311
+ appComponent . addTabsForScrolling ( ) ;
312
+ fixture . detectChanges ( ) ;
313
+
314
+ // Focus the last tab so the header scrolls to the end.
315
+ appComponent . tabHeader . focusIndex = appComponent . tabs . length - 1 ;
316
+ fixture . detectChanges ( ) ;
317
+ expect ( appComponent . tabHeader . scrollDistance ) . toBe (
318
+ appComponent . tabHeader . _getMaxScrollDistance ( ) ,
319
+ ) ;
320
+
321
+ // Remove the first two tabs which includes the selected tab.
322
+ appComponent . tabs = appComponent . tabs . slice ( 2 ) ;
323
+ fixture . detectChanges ( ) ;
324
+ tick ( ) ;
325
+
326
+ expect ( appComponent . tabHeader . scrollDistance ) . toBe (
327
+ appComponent . tabHeader . _getMaxScrollDistance ( ) ,
328
+ ) ;
329
+ } ) ) ;
308
330
} ) ;
309
331
310
332
describe ( 'rtl' , ( ) => {
You can’t perform that action at this time.
0 commit comments