@@ -14,13 +14,14 @@ import {
14
14
QueryList ,
15
15
Directive ,
16
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
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
20
21
} from '@angular/core' ;
21
- import { CdkStep } from './step' ;
22
22
import { LEFT_ARROW , RIGHT_ARROW , ENTER , SPACE } from '../keyboard/keycodes' ;
23
23
import { coerceNumberProperty } from '../coercion/number-property' ;
24
+ import { CdkStepLabel } from './step-label' ;
24
25
25
26
/** Used to generate unique ID for each stepper component. */
26
27
let nextId = 0 ;
@@ -33,12 +34,38 @@ export class CdkStepperSelectionEvent {
33
34
/** The index of the step that was previously selected. */
34
35
oldIndex : number ;
35
36
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
+ }
38
65
}
39
66
40
67
@Directive ( {
41
- selector : 'cdk-stepper ' ,
68
+ selector : 'cdkStepper ' ,
42
69
host : {
43
70
'(focus)' : '_setStepfocused()' ,
44
71
'(keydown)' : '_onKeydown($event)' ,
@@ -62,9 +89,10 @@ export class CdkStepper {
62
89
/** Event emitted when the selected step has changed. */
63
90
@Output ( ) selectionChange = new EventEmitter < CdkStepperSelectionEvent > ( ) ;
64
91
65
- /** The index of the step that the focus is currently on . */
92
+ /** The index of the step that the focus can be set . */
66
93
_focusIndex : number = 0 ;
67
94
95
+ /** Used to track unique ID for each stepper component. */
68
96
private _groupId : number ;
69
97
70
98
constructor ( ) {
@@ -74,28 +102,25 @@ export class CdkStepper {
74
102
/** Selects and focuses the provided step. */
75
103
select ( step : CdkStep | number ) : void {
76
104
if ( typeof step == 'number' ) {
77
- this . selectionChange . emit ( this . _createStepperSelectionEvent ( step , this . _selectedIndex ) ) ;
105
+ this . _emitStepperSelectionEvent ( step , this . _selectedIndex ) ;
78
106
} else {
79
107
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 ) ;
82
109
}
83
110
this . _setStepFocused ( this . _selectedIndex ) ;
84
111
}
85
112
86
113
/** Selects and focuses the next step in list. */
87
114
next ( ) : void {
88
115
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 ) ;
91
117
this . _setStepFocused ( this . _selectedIndex ) ;
92
118
}
93
119
94
120
/** Selects and focuses the previous step in list. */
95
121
previous ( ) : void {
96
122
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 ) ;
99
124
this . _setStepFocused ( this . _selectedIndex ) ;
100
125
}
101
126
@@ -104,36 +129,41 @@ export class CdkStepper {
104
129
return `mat-step-label-${ this . _groupId } -${ i } ` ;
105
130
}
106
131
107
- /** Returns a unique id for each step content element. */
132
+ /** Returns nique id for each step content element. */
108
133
_getStepContentId ( i : number ) : string {
109
134
return `mat-step-content-${ this . _groupId } -${ i } ` ;
110
135
}
111
136
112
- private _createStepperSelectionEvent ( newIndex : number ,
113
- oldIndex : number ) : CdkStepperSelectionEvent {
137
+ private _emitStepperSelectionEvent ( newIndex : number ,
138
+ oldIndex : number ) : void {
114
139
this . _selectedIndex = newIndex ;
115
140
const event = new CdkStepperSelectionEvent ( ) ;
116
141
event . newIndex = newIndex ;
117
142
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 ) ;
120
146
}
121
147
122
148
_onKeydown ( event : KeyboardEvent ) {
123
149
switch ( event . keyCode ) {
124
150
case RIGHT_ARROW :
125
151
if ( this . _focusIndex != this . _steps . length - 1 ) {
126
152
this . _setStepFocused ( this . _focusIndex + 1 ) ;
153
+ } else {
154
+ this . _setStepFocused ( 0 ) ;
127
155
}
128
156
break ;
129
157
case LEFT_ARROW :
130
158
if ( this . _focusIndex != 0 ) {
131
159
this . _setStepFocused ( this . _focusIndex - 1 ) ;
160
+ } else {
161
+ this . _setStepFocused ( this . _steps . length - 1 ) ;
132
162
}
133
163
break ;
134
164
case SPACE :
135
165
case ENTER :
136
- this . _createStepperSelectionEvent ( this . _focusIndex , this . _selectedIndex ) ;
166
+ this . _emitStepperSelectionEvent ( this . _focusIndex , this . _selectedIndex ) ;
137
167
break ;
138
168
default :
139
169
return ;
0 commit comments