Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(tabs): fix inifinite tab loop #4639 #6663

Merged
merged 5 commits into from
Sep 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/lib/tabs/tab-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ describe('MdTabGroup', () => {
});
}));

it('should set to correct tab on fast change', async(() => {
let component = fixture.componentInstance;
component.selectedIndex = 0;
fixture.detectChanges();

setTimeout(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this setTimeout instead of using fixture.whenStable()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used setTimeout because thats what his demo used.

component.selectedIndex = 1;
fixture.detectChanges();

setTimeout(() => {
component.selectedIndex = 0;
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.selectedIndex).toBe(0);
});
}, 1);
}, 1);
}));

it('should change tabs based on selectedIndex', fakeAsync(() => {
let component = fixture.componentInstance;
let tabComponent = fixture.debugElement.query(By.css('md-tab-group')).componentInstance;
Expand Down
16 changes: 8 additions & 8 deletions src/lib/tabs/tab-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import {
ViewEncapsulation,
} from '@angular/core';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {map} from '@angular/cdk/rxjs';
import {Observable} from 'rxjs/Observable';
import {Subscription} from 'rxjs/Subscription';
import {MdTab} from './tab';
import {merge} from 'rxjs/observable/merge';
Expand Down Expand Up @@ -131,9 +129,7 @@ export class MdTabGroup extends _MdTabGroupMixinBase implements AfterContentInit
private _backgroundColor: ThemePalette;

/** Output to enable support for two-way binding on `[(selectedIndex)]` */
@Output() get selectedIndexChange(): Observable<number> {
return map.call(this.selectChange, event => event.index);
}
@Output() selectedIndexChange: EventEmitter<number> = new EventEmitter();

/** Event emitted when focus has changed within a tab group. */
@Output() focusChange: EventEmitter<MdTabChangeEvent> = new EventEmitter<MdTabChangeEvent>();
Expand All @@ -157,16 +153,20 @@ export class MdTabGroup extends _MdTabGroupMixinBase implements AfterContentInit
* a new selected tab should transition in (from the left or right).
*/
ngAfterContentChecked(): void {
// Clamp the next selected index to the bounds of 0 and the tabs length. Note the `|| 0`, which
// ensures that values like NaN can't get through and which would otherwise throw the
// component into an infinite loop (since Math.max(NaN, 0) === NaN).
// Clamp the next selected index to the boundsof 0 and the tabs length.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the comment change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I had changed it and had to reformat it to fix length but then backed that out and didn't undo the reformat. Sorry.

// Note the `|| 0`, which ensures that values like NaN can't get through
// and which would otherwise throw the component into an infinite loop
// (since Math.max(NaN, 0) === NaN).
let indexToSelect = this._indexToSelect =
Math.min(this._tabs.length - 1, Math.max(this._indexToSelect || 0, 0));

// If there is a change in selected index, emit a change event. Should not trigger if
// the selected index has not yet been initialized.
if (this._selectedIndex != indexToSelect && this._selectedIndex != null) {
this.selectChange.emit(this._createChangeEvent(indexToSelect));
// Emitting this value after change detection has run
// since the checked content may contain this variable'
Promise.resolve().then(() => this.selectedIndexChange.emit(indexToSelect));
}

// Setup the position for each tab and optionally setup an origin on the next selected tab.
Expand Down