@@ -14,13 +14,14 @@ import {
1414 QueryList ,
1515 Directive ,
1616 ViewChildren ,
17- // tslint doesn't recognize `ElementRef` is used since it's only used as a generic.
18- // tslint:disable-next-line
19- ElementRef
17+ // This import is only used to define a generic type. The current TypeScript version incorrectly
18+ // considers such imports as unused (https://github.com/Microsoft/TypeScript/issues/14953)
19+ // tslint:disable-next-line:no-unused-variable
20+ ElementRef , Component , ContentChild , ViewChild , TemplateRef
2021} from '@angular/core' ;
21- import { CdkStep } from './step' ;
2222import { LEFT_ARROW , RIGHT_ARROW , ENTER , SPACE } from '../keyboard/keycodes' ;
2323import { coerceNumberProperty } from '../coercion/number-property' ;
24+ import { CdkStepLabel } from './step-label' ;
2425
2526/** Used to generate unique ID for each stepper component. */
2627let nextId = 0 ;
@@ -33,12 +34,38 @@ export class CdkStepperSelectionEvent {
3334 /** The index of the step that was previously selected. */
3435 oldIndex : number ;
3536
36- /** The step component that is selected ruing this change event. */
37- step : CdkStep ;
37+ /** The new step component that is selected ruing this change event. */
38+ newStep : CdkStep ;
39+
40+ /** The step component that was previously selected. */
41+ oldStep : CdkStep ;
42+ }
43+
44+ @Component ( {
45+ selector : 'cdk-step' ,
46+ templateUrl : 'step.html' ,
47+ } )
48+ export class CdkStep {
49+ /** Template for step label if it exists. */
50+ @ContentChild ( CdkStepLabel ) stepLabel : CdkStepLabel ;
51+
52+ /** Template for step content. */
53+ @ViewChild ( TemplateRef ) content : TemplateRef < any > ;
54+
55+ /** Label of the step. */
56+ @Input ( )
57+ label : string ;
58+
59+ constructor ( private _stepper : CdkStepper ) { }
60+
61+ /** Selects this step component. */
62+ select ( ) : void {
63+ this . _stepper . select ( this ) ;
64+ }
3865}
3966
4067@Directive ( {
41- selector : 'cdk-stepper ' ,
68+ selector : 'cdkStepper ' ,
4269 host : {
4370 '(focus)' : '_setStepfocused()' ,
4471 '(keydown)' : '_onKeydown($event)' ,
@@ -62,9 +89,10 @@ export class CdkStepper {
6289 /** Event emitted when the selected step has changed. */
6390 @Output ( ) selectionChange = new EventEmitter < CdkStepperSelectionEvent > ( ) ;
6491
65- /** The index of the step that the focus is currently on . */
92+ /** The index of the step that the focus can be set . */
6693 _focusIndex : number = 0 ;
6794
95+ /** Used to track unique ID for each stepper component. */
6896 private _groupId : number ;
6997
7098 constructor ( ) {
@@ -74,28 +102,25 @@ export class CdkStepper {
74102 /** Selects and focuses the provided step. */
75103 select ( step : CdkStep | number ) : void {
76104 if ( typeof step == 'number' ) {
77- this . selectionChange . emit ( this . _createStepperSelectionEvent ( step , this . _selectedIndex ) ) ;
105+ this . _emitStepperSelectionEvent ( step , this . _selectedIndex ) ;
78106 } else {
79107 let stepsArray = this . _steps . toArray ( ) ;
80- this . selectionChange . emit (
81- this . _createStepperSelectionEvent ( stepsArray . indexOf ( step ) , this . _selectedIndex ) ) ;
108+ this . _emitStepperSelectionEvent ( stepsArray . indexOf ( step ) , this . _selectedIndex ) ;
82109 }
83110 this . _setStepFocused ( this . _selectedIndex ) ;
84111 }
85112
86113 /** Selects and focuses the next step in list. */
87114 next ( ) : void {
88115 if ( this . _selectedIndex == this . _steps . length - 1 ) { return ; }
89- this . selectionChange . emit (
90- this . _createStepperSelectionEvent ( this . _selectedIndex + 1 , this . _selectedIndex ) ) ;
116+ this . _emitStepperSelectionEvent ( this . _selectedIndex + 1 , this . _selectedIndex ) ;
91117 this . _setStepFocused ( this . _selectedIndex ) ;
92118 }
93119
94120 /** Selects and focuses the previous step in list. */
95121 previous ( ) : void {
96122 if ( this . _selectedIndex == 0 ) { return ; }
97- this . selectionChange . emit (
98- this . _createStepperSelectionEvent ( this . _selectedIndex - 1 , this . _selectedIndex ) ) ;
123+ this . _emitStepperSelectionEvent ( this . _selectedIndex - 1 , this . _selectedIndex ) ;
99124 this . _setStepFocused ( this . _selectedIndex ) ;
100125 }
101126
@@ -104,36 +129,41 @@ export class CdkStepper {
104129 return `mat-step-label-${ this . _groupId } -${ i } ` ;
105130 }
106131
107- /** Returns a unique id for each step content element. */
132+ /** Returns nique id for each step content element. */
108133 _getStepContentId ( i : number ) : string {
109134 return `mat-step-content-${ this . _groupId } -${ i } ` ;
110135 }
111136
112- private _createStepperSelectionEvent ( newIndex : number ,
113- oldIndex : number ) : CdkStepperSelectionEvent {
137+ private _emitStepperSelectionEvent ( newIndex : number ,
138+ oldIndex : number ) : void {
114139 this . _selectedIndex = newIndex ;
115140 const event = new CdkStepperSelectionEvent ( ) ;
116141 event . newIndex = newIndex ;
117142 event . oldIndex = oldIndex ;
118- event . step = this . _steps . toArray ( ) [ this . _selectedIndex ] ;
119- return event ;
143+ event . oldStep = this . _steps . toArray ( ) [ oldIndex ] ;
144+ event . newStep = this . _steps . toArray ( ) [ this . _selectedIndex ] ;
145+ this . selectionChange . emit ( event ) ;
120146 }
121147
122148 _onKeydown ( event : KeyboardEvent ) {
123149 switch ( event . keyCode ) {
124150 case RIGHT_ARROW :
125151 if ( this . _focusIndex != this . _steps . length - 1 ) {
126152 this . _setStepFocused ( this . _focusIndex + 1 ) ;
153+ } else {
154+ this . _setStepFocused ( 0 ) ;
127155 }
128156 break ;
129157 case LEFT_ARROW :
130158 if ( this . _focusIndex != 0 ) {
131159 this . _setStepFocused ( this . _focusIndex - 1 ) ;
160+ } else {
161+ this . _setStepFocused ( this . _steps . length - 1 ) ;
132162 }
133163 break ;
134164 case SPACE :
135165 case ENTER :
136- this . _createStepperSelectionEvent ( this . _focusIndex , this . _selectedIndex ) ;
166+ this . _emitStepperSelectionEvent ( this . _focusIndex , this . _selectedIndex ) ;
137167 break ;
138168 default :
139169 return ;
0 commit comments