-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
feat(tabs): adds focus/select events #649
Merged
jelbourn
merged 1 commit into
angular:master
from
robertmesserle:fix/569/tabs-change-event
Jun 15, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,39 @@ | ||
import {Component, Input, ViewChildren, NgZone} from '@angular/core'; | ||
import { | ||
ContentChild, | ||
Directive, | ||
Component, | ||
Input, | ||
Output, | ||
ViewChildren, | ||
NgZone, | ||
EventEmitter | ||
} from '@angular/core'; | ||
import {QueryList} from '@angular/core'; | ||
import {ContentChildren} from '@angular/core'; | ||
import {PortalHostDirective} from '@angular2-material/core/portal/portal-directives'; | ||
import {MdTabLabel} from './tab-label'; | ||
import {MdTabContent} from './tab-content'; | ||
import {MdTabLabelWrapper} from './tab-label-wrapper'; | ||
import {MdInkBar} from './ink-bar'; | ||
import {Observable} from 'rxjs/Observable'; | ||
|
||
/** Used to generate unique ID's for each tab component */ | ||
let nextId = 0; | ||
|
||
/** A simple change event emitted on focus or selection changes. */ | ||
export class MdTabChangeEvent { | ||
index: number; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably include a reference to the |
||
tab: MdTab; | ||
} | ||
|
||
@Directive({ | ||
selector: 'md-tab' | ||
}) | ||
export class MdTab { | ||
@ContentChild(MdTabLabel) label: MdTabLabel; | ||
@ContentChild(MdTabContent) content: MdTabContent; | ||
} | ||
|
||
/** | ||
* Material design tab-group component. Supports basic tab pairs (label + content) and includes | ||
* animated ink-bar, keyboard navigation, and screen reader. | ||
|
@@ -24,15 +48,35 @@ let nextId = 0; | |
}) | ||
export class MdTabGroup { | ||
/** @internal */ | ||
@ContentChildren(MdTabLabel) labels: QueryList<MdTabLabel>; | ||
|
||
/** @internal */ | ||
@ContentChildren(MdTabContent) contents: QueryList<MdTabContent>; | ||
@ContentChildren(MdTab) tabs: QueryList<MdTab>; | ||
|
||
@ViewChildren(MdTabLabelWrapper) private _labelWrappers: QueryList<MdTabLabelWrapper>; | ||
@ViewChildren(MdInkBar) private _inkBar: QueryList<MdInkBar>; | ||
|
||
@Input() selectedIndex: number = 0; | ||
private _isInitialized: boolean = false; | ||
|
||
private _selectedIndex: number = 0; | ||
@Input() | ||
set selectedIndex(value: number) { | ||
this._selectedIndex = value; | ||
|
||
if (this._isInitialized) { | ||
this._onSelectChange.emit(this._createChangeEvent(value)); | ||
} | ||
} | ||
get selectedIndex(): number { | ||
return this._selectedIndex; | ||
} | ||
|
||
private _onFocusChange: EventEmitter<MdTabChangeEvent> = new EventEmitter<MdTabChangeEvent>(); | ||
@Output('focusChange') get focusChange(): Observable<MdTabChangeEvent> { | ||
return this._onFocusChange.asObservable(); | ||
} | ||
|
||
private _onSelectChange: EventEmitter<MdTabChangeEvent> = new EventEmitter<MdTabChangeEvent>(); | ||
@Output('selectChange') get selectChange(): Observable<MdTabChangeEvent> { | ||
return this._onSelectChange.asObservable(); | ||
} | ||
|
||
private _focusIndex: number = 0; | ||
private _groupId: number; | ||
|
@@ -52,6 +96,7 @@ export class MdTabGroup { | |
this._updateInkBar(); | ||
}); | ||
}); | ||
this._isInitialized = true; | ||
} | ||
|
||
/** Tells the ink-bar to align itself to the current label wrapper */ | ||
|
@@ -77,11 +122,25 @@ export class MdTabGroup { | |
/** When the focus index is set, we must manually send focus to the correct label */ | ||
set focusIndex(value: number) { | ||
this._focusIndex = value; | ||
|
||
if (this._isInitialized) { | ||
this._onFocusChange.emit(this._createChangeEvent(value)); | ||
} | ||
|
||
if (this._labelWrappers && this._labelWrappers.length) { | ||
this._labelWrappers.toArray()[value].focus(); | ||
} | ||
} | ||
|
||
private _createChangeEvent(index: number): MdTabChangeEvent { | ||
const event = new MdTabChangeEvent; | ||
event.index = index; | ||
if (this.tabs && this.tabs.length) { | ||
event.tab = this.tabs.toArray()[index]; | ||
} | ||
return event; | ||
} | ||
|
||
/** | ||
* Returns a unique id for each tab label element | ||
* @internal | ||
|
@@ -113,4 +172,4 @@ export class MdTabGroup { | |
} | ||
} | ||
|
||
export const MD_TABS_DIRECTIVES = [MdTabGroup, MdTabLabel, MdTabContent]; | ||
export const MD_TABS_DIRECTIVES = [MdTabGroup, MdTabLabel, MdTabContent, MdTab]; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like nothing is currently testing that the event object has the correct data.