@@ -19,22 +19,28 @@ import {
1919 ElementRef
2020} from '@angular/core' ;
2121import { CdkStep } from './step' ;
22- import { LEFT_ARROW , RIGHT_ARROW , ENTER , TAB } from '../keyboard/keycodes' ;
22+ import { LEFT_ARROW , RIGHT_ARROW , ENTER , SPACE } from '../keyboard/keycodes' ;
2323import { coerceNumberProperty } from '../coercion/number-property' ;
2424
2525/** Used to generate unique ID for each stepper component. */
2626let nextId = 0 ;
2727
2828/** Change event emitted on selection changes. */
2929export class CdkStepperSelectionEvent {
30- index : number ;
30+ /** The index of the step that is newly selected during this change event. */
31+ newIndex : number ;
32+
33+ /** The index of the step that was previously selected. */
34+ oldIndex : number ;
35+
36+ /** The step component that is selected ruing this change event. */
3137 step : CdkStep ;
3238}
3339
3440@Directive ( {
35- selector : '[cdkStepper] ' ,
41+ selector : 'cdk-stepper ' ,
3642 host : {
37- '(focus)' : '_setStepfocus ()' ,
43+ '(focus)' : '_setStepfocused ()' ,
3844 '(keydown)' : '_onKeydown($event)' ,
3945 } ,
4046} )
@@ -66,30 +72,31 @@ export class CdkStepper {
6672 }
6773
6874 /** Selects and focuses the provided step. */
69- select ( step : CdkStep ) : void {
70- let stepsArray = this . _steps . toArray ( ) ;
71- this . _selectedIndex = stepsArray . indexOf ( step ) ;
72- this . selectionChange . emit ( this . _createStepperSelectionEvent ( this . _selectedIndex ) ) ;
73- this . _focusIndex = this . _selectedIndex ;
74- this . _setStepFocus ( ) ;
75+ select ( step : CdkStep | number ) : void {
76+ if ( typeof step == 'number' ) {
77+ this . selectionChange . emit ( this . _createStepperSelectionEvent ( step , this . _selectedIndex ) ) ;
78+ } else {
79+ let stepsArray = this . _steps . toArray ( ) ;
80+ this . selectionChange . emit (
81+ this . _createStepperSelectionEvent ( stepsArray . indexOf ( step ) , this . _selectedIndex ) ) ;
82+ }
83+ this . _setStepFocused ( this . _selectedIndex ) ;
7584 }
7685
7786 /** Selects and focuses the next step in list. */
7887 next ( ) : void {
7988 if ( this . _selectedIndex == this . _steps . length - 1 ) { return ; }
80- this . _selectedIndex ++ ;
81- this . selectionChange . emit ( this . _createStepperSelectionEvent ( this . _selectedIndex ) ) ;
82- this . _focusIndex = this . _selectedIndex ;
83- this . _setStepFocus ( ) ;
89+ this . selectionChange . emit (
90+ this . _createStepperSelectionEvent ( this . _selectedIndex + 1 , this . _selectedIndex ) ) ;
91+ this . _setStepFocused ( this . _selectedIndex ) ;
8492 }
8593
8694 /** Selects and focuses the previous step in list. */
8795 previous ( ) : void {
8896 if ( this . _selectedIndex == 0 ) { return ; }
89- this . _selectedIndex -- ;
90- this . selectionChange . emit ( this . _createStepperSelectionEvent ( this . _selectedIndex ) ) ;
91- this . _focusIndex = this . _selectedIndex ;
92- this . _setStepFocus ( ) ;
97+ this . selectionChange . emit (
98+ this . _createStepperSelectionEvent ( this . _selectedIndex - 1 , this . _selectedIndex ) ) ;
99+ this . _setStepFocused ( this . _selectedIndex ) ;
93100 }
94101
95102 /** Returns a unique id for each step label element. */
@@ -102,9 +109,12 @@ export class CdkStepper {
102109 return `mat-step-content-${ this . _groupId } -${ i } ` ;
103110 }
104111
105- private _createStepperSelectionEvent ( index : number ) : CdkStepperSelectionEvent {
112+ private _createStepperSelectionEvent ( newIndex : number ,
113+ oldIndex : number ) : CdkStepperSelectionEvent {
114+ this . _selectedIndex = newIndex ;
106115 const event = new CdkStepperSelectionEvent ( ) ;
107- event . index = index ;
116+ event . newIndex = newIndex ;
117+ event . oldIndex = oldIndex ;
108118 event . step = this . _steps . toArray ( ) [ this . _selectedIndex ] ;
109119 return event ;
110120 }
@@ -113,27 +123,26 @@ export class CdkStepper {
113123 switch ( event . keyCode ) {
114124 case RIGHT_ARROW :
115125 if ( this . _focusIndex != this . _steps . length - 1 ) {
116- this . _focusIndex ++ ;
117- this . _setStepFocus ( ) ;
126+ this . _setStepFocused ( this . _focusIndex + 1 ) ;
118127 }
119128 break ;
120129 case LEFT_ARROW :
121130 if ( this . _focusIndex != 0 ) {
122- this . _focusIndex -- ;
123- this . _setStepFocus ( ) ;
131+ this . _setStepFocused ( this . _focusIndex - 1 ) ;
124132 }
125133 break ;
134+ case SPACE :
126135 case ENTER :
127- this . _selectedIndex = this . _focusIndex ;
128- this . _createStepperSelectionEvent ( this . _selectedIndex ) ;
136+ this . _createStepperSelectionEvent ( this . _focusIndex , this . _selectedIndex ) ;
129137 break ;
138+ default :
139+ return ;
130140 }
131- if ( event . keyCode != TAB ) {
132- event . preventDefault ( ) ;
133- }
141+ event . preventDefault ( ) ;
134142 }
135143
136- private _setStepFocus ( ) {
144+ private _setStepFocused ( index : number ) {
145+ this . _focusIndex = index ;
137146 this . _stepHeader . toArray ( ) [ this . _focusIndex ] . nativeElement . focus ( ) ;
138147 }
139148}
0 commit comments