77 */
88
99import {
10- Component , ContentChildren , EventEmitter , Input , Output , QueryList , OnInit ,
11- AfterViewChecked , AfterViewInit , Directive , ElementRef , ViewChild , ViewChildren
10+ Component , ContentChildren , EventEmitter , Input , Output , QueryList , OnInit ,
11+ AfterViewChecked , AfterViewInit , Directive , ElementRef , ViewChild , ViewChildren
1212} from '@angular/core' ;
1313import { CdkStep } from './step' ;
1414import { Observable } from 'rxjs/Observable' ;
@@ -20,134 +20,134 @@ let nextId = 0;
2020
2121/** Change event emitted on focus or selection changes. */
2222export class CdkStepEvent {
23- index : number ;
24- step : CdkStep ;
23+ index : number ;
24+ step : CdkStep ;
2525}
2626
2727@Directive ( {
28- selector : '[cdkStepper]' ,
29- host : {
30- '(keydown)' : '_onKeydown($event)'
31- } ,
28+ selector : '[cdkStepper]' ,
29+ host : {
30+ '(keydown)' : '_onKeydown($event)'
31+ } ,
3232} )
3333export class CdkStepper {
34- @ContentChildren ( CdkStep ) _steps : QueryList < CdkStep > ;
35-
36- @ViewChildren ( 'stepHeader' ) _stepHeader : QueryList < ElementRef > ;
37-
38- /** The index of the currently selected step. */
39- @Input ( )
40- set selectedIndex ( value : number ) {
41- this . _selectedIndex = value ;
42- }
43- get selectedIndex ( ) : number { return this . _selectedIndex ; }
44- private _selectedIndex : number ;
45-
46- /** Optional input to support both linear and non-linear stepper component. */
47- @Input ( ) linear : boolean = true ;
48-
49- /** Output to enable support for two-way binding on `[(selectedIndex)]` */
50- @Output ( ) get selectedIndexChange ( ) : Observable < number > {
51- return map . call ( this . stepEvent , event => event . index ) ;
52- }
53-
54- // @Output () get focusIndexChange(): Observable<number> {
55- // return map.call(this.focusChange, event => event.index);
56- // }
57-
58- /** Event emitted when the selected step has changed. */
59- @Output ( ) stepEvent = new EventEmitter < CdkStepEvent > ( ) ;
60-
61- /** Event emitted when the focused step has changed. */
62- @Output ( ) focusChange = new EventEmitter < CdkStepEvent > ( ) ;
63-
64- /** The step that is currently selected. */
65- get selectedStep ( ) : CdkStep {
66- return this . _steps . toArray ( ) [ this . _selectedIndex ] ;
67- }
68- private _selectedStep : CdkStep ;
69-
70- /** The index of the step that the focus is currently on. */
71- get focusIndex ( ) : number { return this . _focusIndex ; }
72- private _focusIndex : number = 0 ;
73-
74- private _groupId : number ;
75-
76- constructor ( ) {
77- this . _groupId = nextId ++ ;
78- }
79-
80- /** Selects and focuses the provided step. */
81- selectStep ( step : CdkStep ) : void {
82- let stepsArray = this . _steps . toArray ( ) ;
83- this . _selectedIndex = stepsArray . indexOf ( step ) ;
84- this . stepEvent . emit ( this . _emitStepEvent ( this . _selectedIndex ) ) ;
85- this . _focusIndex = this . _selectedIndex ;
86- this . _setStepFocus ( ) ;
87- }
88-
89- /** Selects and focuses the next step in list. */
90- nextStep ( ) : void {
91- if ( this . _selectedIndex == this . _steps . length - 1 ) { return ; }
92- this . _selectedIndex ++ ;
93- this . stepEvent . emit ( this . _emitStepEvent ( this . _selectedIndex ) ) ;
94- this . _focusIndex = this . _selectedIndex ;
95- this . _setStepFocus ( ) ;
96- }
97-
98- /** Selects and focuses the previous step in list. */
99- previousStep ( ) : void {
100- if ( this . _selectedIndex == 0 ) { return ; }
101- this . _selectedIndex -- ;
102- this . stepEvent . emit ( this . _emitStepEvent ( this . _selectedIndex ) ) ;
103- this . _focusIndex = this . _selectedIndex ;
104- this . _setStepFocus ( ) ;
105- }
106-
107- /** Returns a unique id for each step label element. */
108- _getStepLabelId ( i : number ) : string {
109- return `mat-step-label-${ this . _groupId } -${ i } ` ;
110- }
111-
112- /** Returns a unique id for each step content element. */
113- _getStepContentId ( i : number ) : string {
114- return `mat-step-content-${ this . _groupId } -${ i } ` ;
115- }
116-
117- private _emitStepEvent ( index : number ) : CdkStepEvent {
118- const event = new CdkStepEvent ( ) ;
119- event . index = index ;
120- event . step = this . _steps . toArray ( ) [ this . _selectedIndex ] ;
121- this . _selectedStep = event . step ;
122- return event ;
123- }
124-
125- _onKeydown ( event : KeyboardEvent ) {
126- switch ( event . keyCode ) {
127- case RIGHT_ARROW :
128- if ( this . _focusIndex != this . _steps . length - 1 ) {
129- this . _focusIndex ++ ;
130- this . _setStepFocus ( ) ;
131- }
132- break ;
133- case LEFT_ARROW :
134- if ( this . _focusIndex != 0 ) {
135- this . _focusIndex -- ;
136- this . _setStepFocus ( ) ;
137- }
138- break ;
139- case ENTER :
140- this . _selectedIndex = this . _focusIndex ;
141- this . _emitStepEvent ( this . _selectedIndex ) ;
142- break ;
34+ @ContentChildren ( CdkStep ) _steps : QueryList < CdkStep > ;
35+
36+ @ViewChildren ( 'stepHeader' ) _stepHeader : QueryList < ElementRef > ;
37+
38+ /** The index of the currently selected step. */
39+ @Input ( )
40+ set selectedIndex ( value : number ) {
41+ this . _selectedIndex = value ;
42+ }
43+ get selectedIndex ( ) : number { return this . _selectedIndex ; }
44+ private _selectedIndex : number ;
45+
46+ /** Optional input to support both linear and non-linear stepper component. */
47+ @Input ( ) linear : boolean = true ;
48+
49+ /** Output to enable support for two-way binding on `[(selectedIndex)]` */
50+ @Output ( ) get selectedIndexChange ( ) : Observable < number > {
51+ return map . call ( this . stepEvent , event => event . index ) ;
52+ }
53+
54+ // @Output () get focusIndexChange(): Observable<number> {
55+ // return map.call(this.focusChange, event => event.index);
56+ // }
57+
58+ /** Event emitted when the selected step has changed. */
59+ @Output ( ) stepEvent = new EventEmitter < CdkStepEvent > ( ) ;
60+
61+ /** Event emitted when the focused step has changed. */
62+ @Output ( ) focusChange = new EventEmitter < CdkStepEvent > ( ) ;
63+
64+ /** The step that is currently selected. */
65+ get selectedStep ( ) : CdkStep {
66+ return this . _steps . toArray ( ) [ this . _selectedIndex ] ;
67+ }
68+ private _selectedStep : CdkStep ;
69+
70+ /** The index of the step that the focus is currently on. */
71+ get focusIndex ( ) : number { return this . _focusIndex ; }
72+ private _focusIndex : number = 0 ;
73+
74+ private _groupId : number ;
75+
76+ constructor ( ) {
77+ this . _groupId = nextId ++ ;
78+ }
79+
80+ /** Selects and focuses the provided step. */
81+ selectStep ( step : CdkStep ) : void {
82+ let stepsArray = this . _steps . toArray ( ) ;
83+ this . _selectedIndex = stepsArray . indexOf ( step ) ;
84+ this . stepEvent . emit ( this . _emitStepEvent ( this . _selectedIndex ) ) ;
85+ this . _focusIndex = this . _selectedIndex ;
86+ this . _setStepFocus ( ) ;
87+ }
88+
89+ /** Selects and focuses the next step in list. */
90+ nextStep ( ) : void {
91+ if ( this . _selectedIndex == this . _steps . length - 1 ) { return ; }
92+ this . _selectedIndex ++ ;
93+ this . stepEvent . emit ( this . _emitStepEvent ( this . _selectedIndex ) ) ;
94+ this . _focusIndex = this . _selectedIndex ;
95+ this . _setStepFocus ( ) ;
96+ }
97+
98+ /** Selects and focuses the previous step in list. */
99+ previousStep ( ) : void {
100+ if ( this . _selectedIndex == 0 ) { return ; }
101+ this . _selectedIndex -- ;
102+ this . stepEvent . emit ( this . _emitStepEvent ( this . _selectedIndex ) ) ;
103+ this . _focusIndex = this . _selectedIndex ;
104+ this . _setStepFocus ( ) ;
105+ }
106+
107+ /** Returns a unique id for each step label element. */
108+ _getStepLabelId ( i : number ) : string {
109+ return `mat-step-label-${ this . _groupId } -${ i } ` ;
110+ }
111+
112+ /** Returns a unique id for each step content element. */
113+ _getStepContentId ( i : number ) : string {
114+ return `mat-step-content-${ this . _groupId } -${ i } ` ;
115+ }
116+
117+ private _emitStepEvent ( index : number ) : CdkStepEvent {
118+ const event = new CdkStepEvent ( ) ;
119+ event . index = index ;
120+ event . step = this . _steps . toArray ( ) [ this . _selectedIndex ] ;
121+ this . _selectedStep = event . step ;
122+ return event ;
123+ }
124+
125+ _onKeydown ( event : KeyboardEvent ) {
126+ switch ( event . keyCode ) {
127+ case RIGHT_ARROW :
128+ if ( this . _focusIndex != this . _steps . length - 1 ) {
129+ this . _focusIndex ++ ;
130+ this . _setStepFocus ( ) ;
143131 }
144- if ( event . keyCode != TAB ) {
145- event . preventDefault ( ) ;
132+ break ;
133+ case LEFT_ARROW :
134+ if ( this . _focusIndex != 0 ) {
135+ this . _focusIndex -- ;
136+ this . _setStepFocus ( ) ;
146137 }
138+ break ;
139+ case ENTER :
140+ this . _selectedIndex = this . _focusIndex ;
141+ this . _emitStepEvent ( this . _selectedIndex ) ;
142+ break ;
147143 }
148-
149- _setStepFocus ( ) {
150- this . _stepHeader . toArray ( ) [ this . _focusIndex ] . nativeElement . focus ( ) ;
151- this . focusChange . emit ( this . _emitStepEvent ( this . _selectedIndex ) ) ;
144+ if ( event . keyCode != TAB ) {
145+ event . preventDefault ( ) ;
152146 }
147+ }
148+
149+ _setStepFocus ( ) {
150+ this . _stepHeader . toArray ( ) [ this . _focusIndex ] . nativeElement . focus ( ) ;
151+ this . focusChange . emit ( this . _emitStepEvent ( this . _selectedIndex ) ) ;
152+ }
153153}
0 commit comments