@@ -17,10 +17,13 @@ import {
1717 // This import is only used to define a generic type. The current TypeScript version incorrectly
1818 // considers such imports as unused (https://github.com/Microsoft/TypeScript/issues/14953)
1919 // tslint:disable-next-line:no-unused-variable
20- ElementRef , Component , ContentChild , ViewChild , TemplateRef
20+ ElementRef ,
21+ Component ,
22+ ContentChild ,
23+ ViewChild ,
24+ TemplateRef
2125} from '@angular/core' ;
2226import { LEFT_ARROW , RIGHT_ARROW , ENTER , SPACE } from '../keyboard/keycodes' ;
23- import { coerceNumberProperty } from '../coercion/number-property' ;
2427import { CdkStepLabel } from './step-label' ;
2528
2629/** Used to generate unique ID for each stepper component. */
@@ -60,12 +63,12 @@ export class CdkStep {
6063
6164 /** Selects this step component. */
6265 select ( ) : void {
63- this . _stepper . select ( this ) ;
66+ this . _stepper . selected = this ;
6467 }
6568}
6669
6770@Directive ( {
68- selector : 'cdkStepper ' ,
71+ selector : 'cdk-stepper ' ,
6972 host : {
7073 '(focus)' : '_setStepfocused()' ,
7174 '(keydown)' : '_onKeydown($event)' ,
@@ -78,13 +81,22 @@ export class CdkStepper {
7881 /** The list of step headers of the steps in the stepper. */
7982 @ViewChildren ( 'stepHeader' ) _stepHeader : QueryList < ElementRef > ;
8083
81- /** The index of the currently selected step. */
82- @Input ( )
84+ /** The index of the selected step. */
8385 get selectedIndex ( ) { return this . _selectedIndex ; }
84- set selectedIndex ( value : any ) {
85- this . _selectedIndex = coerceNumberProperty ( value ) ;
86+ set selectedIndex ( index : number ) {
87+ if ( this . _selectedIndex == index ) { return ; }
88+ this . _emitStepperSelectionEvent ( index ) ;
89+ this . _setStepFocused ( this . _selectedIndex ) ;
90+ }
91+ private _selectedIndex : number = 0 ;
92+
93+ /** Returns the step that is selected. */
94+ get selected ( ) { return this . _steps [ this . selectedIndex ] ; }
95+ /** Sets selectedIndex as the index of the provided step. */
96+ set selected ( step : CdkStep ) {
97+ let index = this . _steps . toArray ( ) . indexOf ( step ) ;
98+ this . selectedIndex = index ;
8699 }
87- private _selectedIndex : number ;
88100
89101 /** Event emitted when the selected step has changed. */
90102 @Output ( ) selectionChange = new EventEmitter < CdkStepperSelectionEvent > ( ) ;
@@ -99,29 +111,16 @@ export class CdkStepper {
99111 this . _groupId = nextId ++ ;
100112 }
101113
102- /** Selects and focuses the provided step. */
103- select ( step : CdkStep | number ) : void {
104- if ( typeof step == 'number' ) {
105- this . _emitStepperSelectionEvent ( step , this . _selectedIndex ) ;
106- } else {
107- let stepsArray = this . _steps . toArray ( ) ;
108- this . _emitStepperSelectionEvent ( stepsArray . indexOf ( step ) , this . _selectedIndex ) ;
109- }
110- this . _setStepFocused ( this . _selectedIndex ) ;
111- }
112-
113114 /** Selects and focuses the next step in list. */
114115 next ( ) : void {
115116 if ( this . _selectedIndex == this . _steps . length - 1 ) { return ; }
116- this . _emitStepperSelectionEvent ( this . _selectedIndex + 1 , this . _selectedIndex ) ;
117- this . _setStepFocused ( this . _selectedIndex ) ;
117+ this . selectedIndex ++ ;
118118 }
119119
120120 /** Selects and focuses the previous step in list. */
121121 previous ( ) : void {
122122 if ( this . _selectedIndex == 0 ) { return ; }
123- this . _emitStepperSelectionEvent ( this . _selectedIndex - 1 , this . _selectedIndex ) ;
124- this . _setStepFocused ( this . _selectedIndex ) ;
123+ this . selectedIndex -- ;
125124 }
126125
127126 /** Returns a unique id for each step label element. */
@@ -134,38 +133,31 @@ export class CdkStepper {
134133 return `mat-step-content-${ this . _groupId } -${ i } ` ;
135134 }
136135
137- private _emitStepperSelectionEvent ( newIndex : number ,
138- oldIndex : number ) : void {
139- this . _selectedIndex = newIndex ;
136+ private _emitStepperSelectionEvent ( newIndex : number ) : void {
140137 const event = new CdkStepperSelectionEvent ( ) ;
138+ event . oldIndex = this . _selectedIndex ;
141139 event . newIndex = newIndex ;
142- event . oldIndex = oldIndex ;
143- event . oldStep = this . _steps . toArray ( ) [ oldIndex ] ;
144- event . newStep = this . _steps . toArray ( ) [ this . _selectedIndex ] ;
140+ let stepsArray = this . _steps . toArray ( ) ;
141+ event . oldStep = stepsArray [ this . _selectedIndex ] ;
142+ event . newStep = stepsArray [ newIndex ] ;
143+ this . _selectedIndex = newIndex ;
145144 this . selectionChange . emit ( event ) ;
146145 }
147146
148147 _onKeydown ( event : KeyboardEvent ) {
149148 switch ( event . keyCode ) {
150149 case RIGHT_ARROW :
151- if ( this . _focusIndex != this . _steps . length - 1 ) {
152- this . _setStepFocused ( this . _focusIndex + 1 ) ;
153- } else {
154- this . _setStepFocused ( 0 ) ;
155- }
150+ this . _setStepFocused ( ( this . _focusIndex + 1 ) % this . _steps . length ) ;
156151 break ;
157152 case LEFT_ARROW :
158- if ( this . _focusIndex != 0 ) {
159- this . _setStepFocused ( this . _focusIndex - 1 ) ;
160- } else {
161- this . _setStepFocused ( this . _steps . length - 1 ) ;
162- }
153+ this . _setStepFocused ( ( this . _focusIndex + this . _steps . length - 1 ) % this . _steps . length ) ;
163154 break ;
164155 case SPACE :
165156 case ENTER :
166- this . _emitStepperSelectionEvent ( this . _focusIndex , this . _selectedIndex ) ;
157+ this . _emitStepperSelectionEvent ( this . _focusIndex ) ;
167158 break ;
168159 default :
160+ // Return to avoid calling preventDefault on keys that are not explicitly handled.
169161 return ;
170162 }
171163 event . preventDefault ( ) ;
0 commit comments