77 */
88
99import {
10- Component , ContentChildren , EventEmitter , Input , Output , QueryList , OnInit ,
11- AfterViewChecked , AfterViewInit , Directive , ElementRef , ViewChild , ViewChildren
10+ ContentChildren ,
11+ EventEmitter ,
12+ Input ,
13+ Output ,
14+ QueryList ,
15+ Directive ,
16+ ViewChildren ,
17+ // tslint doesn't recognize `ElementRef` is used since it's only used as a generic.
18+ // tslint:disable-next-line
19+ ElementRef
1220} from '@angular/core' ;
1321import { CdkStep } from './step' ;
14- import { Observable } from 'rxjs/Observable' ;
15- import { map } from 'rxjs/operator/map' ;
1622import { LEFT_ARROW , RIGHT_ARROW , ENTER , TAB } from '../keyboard/keycodes' ;
23+ import { coerceNumberProperty } from '../coercion/number-property' ;
1724
1825/** Used to generate unique ID for each stepper component. */
1926let nextId = 0 ;
2027
21- /** Change event emitted on focus or selection changes. */
22- export class CdkStepEvent {
28+ /** Change event emitted on selection changes. */
29+ export class CdkStepperSelectionEvent {
2330 index : number ;
2431 step : CdkStep ;
2532}
2633
2734@Directive ( {
2835 selector : '[cdkStepper]' ,
2936 host : {
30- '(keydown)' : '_onKeydown($event)'
37+ '(focus)' : '_setStepfocus()' ,
38+ '(keydown)' : '_onKeydown($event)' ,
3139 } ,
3240} )
3341export class CdkStepper {
42+ /** The list of step components that the stepper is holding. */
3443 @ContentChildren ( CdkStep ) _steps : QueryList < CdkStep > ;
3544
45+ /** The list of step headers of the steps in the stepper. */
3646 @ViewChildren ( 'stepHeader' ) _stepHeader : QueryList < ElementRef > ;
3747
3848 /** The index of the currently selected step. */
3949 @Input ( )
40- set selectedIndex ( value : number ) {
41- this . _selectedIndex = value ;
50+ get selectedIndex ( ) { return this . _selectedIndex ; }
51+ set selectedIndex ( value : any ) {
52+ this . _selectedIndex = coerceNumberProperty ( value ) ;
4253 }
43- get selectedIndex ( ) : number { return this . _selectedIndex ; }
4454 private _selectedIndex : number ;
4555
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-
5856 /** 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 ;
57+ @Output ( ) selectionChange = new EventEmitter < CdkStepperSelectionEvent > ( ) ;
6958
7059 /** The index of the step that the focus is currently on. */
71- get focusIndex ( ) : number { return this . _focusIndex ; }
72- private _focusIndex : number = 0 ;
60+ _focusIndex : number = 0 ;
7361
7462 private _groupId : number ;
7563
@@ -78,28 +66,28 @@ export class CdkStepper {
7866 }
7967
8068 /** Selects and focuses the provided step. */
81- selectStep ( step : CdkStep ) : void {
69+ select ( step : CdkStep ) : void {
8270 let stepsArray = this . _steps . toArray ( ) ;
8371 this . _selectedIndex = stepsArray . indexOf ( step ) ;
84- this . stepEvent . emit ( this . _emitStepEvent ( this . _selectedIndex ) ) ;
72+ this . selectionChange . emit ( this . _createStepperSelectionEvent ( this . _selectedIndex ) ) ;
8573 this . _focusIndex = this . _selectedIndex ;
8674 this . _setStepFocus ( ) ;
8775 }
8876
8977 /** Selects and focuses the next step in list. */
90- nextStep ( ) : void {
78+ next ( ) : void {
9179 if ( this . _selectedIndex == this . _steps . length - 1 ) { return ; }
9280 this . _selectedIndex ++ ;
93- this . stepEvent . emit ( this . _emitStepEvent ( this . _selectedIndex ) ) ;
81+ this . selectionChange . emit ( this . _createStepperSelectionEvent ( this . _selectedIndex ) ) ;
9482 this . _focusIndex = this . _selectedIndex ;
9583 this . _setStepFocus ( ) ;
9684 }
9785
9886 /** Selects and focuses the previous step in list. */
99- previousStep ( ) : void {
87+ previous ( ) : void {
10088 if ( this . _selectedIndex == 0 ) { return ; }
10189 this . _selectedIndex -- ;
102- this . stepEvent . emit ( this . _emitStepEvent ( this . _selectedIndex ) ) ;
90+ this . selectionChange . emit ( this . _createStepperSelectionEvent ( this . _selectedIndex ) ) ;
10391 this . _focusIndex = this . _selectedIndex ;
10492 this . _setStepFocus ( ) ;
10593 }
@@ -114,11 +102,10 @@ export class CdkStepper {
114102 return `mat-step-content-${ this . _groupId } -${ i } ` ;
115103 }
116104
117- private _emitStepEvent ( index : number ) : CdkStepEvent {
118- const event = new CdkStepEvent ( ) ;
105+ private _createStepperSelectionEvent ( index : number ) : CdkStepperSelectionEvent {
106+ const event = new CdkStepperSelectionEvent ( ) ;
119107 event . index = index ;
120108 event . step = this . _steps . toArray ( ) [ this . _selectedIndex ] ;
121- this . _selectedStep = event . step ;
122109 return event ;
123110 }
124111
@@ -138,16 +125,15 @@ export class CdkStepper {
138125 break ;
139126 case ENTER :
140127 this . _selectedIndex = this . _focusIndex ;
141- this . _emitStepEvent ( this . _selectedIndex ) ;
128+ this . _createStepperSelectionEvent ( this . _selectedIndex ) ;
142129 break ;
143130 }
144131 if ( event . keyCode != TAB ) {
145132 event . preventDefault ( ) ;
146133 }
147134 }
148135
149- _setStepFocus ( ) {
136+ private _setStepFocus ( ) {
150137 this . _stepHeader . toArray ( ) [ this . _focusIndex ] . nativeElement . focus ( ) ;
151- this . focusChange . emit ( this . _emitStepEvent ( this . _selectedIndex ) ) ;
152138 }
153139}
0 commit comments