@@ -31,6 +31,7 @@ import {
31
31
TemplateRef ,
32
32
ViewChild ,
33
33
ViewEncapsulation ,
34
+ InjectionToken ,
34
35
} from '@angular/core' ;
35
36
import { DOCUMENT } from '@angular/common' ;
36
37
import { AbstractControl } from '@angular/forms' ;
@@ -66,7 +67,7 @@ export class StepperSelectionEvent {
66
67
}
67
68
68
69
/** The state of each step. */
69
- export type StepState = 'number' | 'edit' | 'done' | 'error' ;
70
+ export type StepState = 'number' | 'edit' | 'done' | 'error' | string ;
70
71
71
72
/** Enum to represent the different states of the steps. */
72
73
export const STEP_STATE = {
@@ -76,6 +77,26 @@ export const STEP_STATE = {
76
77
ERROR : 'error'
77
78
} ;
78
79
80
+ /** InjectionToken that can be used to specify the global stepper options. */
81
+ export const MAT_STEPPER_GLOBAL_OPTIONS =
82
+ new InjectionToken < StepperOptions > ( 'mat-stepper-global-options' ) ;
83
+
84
+ /** Configurable options for floating labels. */
85
+ export interface StepperOptions {
86
+ /**
87
+ * Whether the stepper should display an error state or not.
88
+ * Default behavior is assumed to be false.
89
+ */
90
+ showError ?: boolean ;
91
+
92
+ /**
93
+ * Whether the stepper should use the Material UI guidelines when
94
+ * displaying the icons or not.
95
+ * Default behavior is assumed to be false.
96
+ */
97
+ useGuidelines ?: boolean ;
98
+ }
99
+
79
100
@Component ( {
80
101
moduleId : module . id ,
81
102
selector : 'cdk-step' ,
@@ -85,6 +106,10 @@ export const STEP_STATE = {
85
106
changeDetection : ChangeDetectionStrategy . OnPush ,
86
107
} )
87
108
export class CdkStep implements OnChanges {
109
+ private _stepperOptions : StepperOptions ;
110
+ _showError : boolean ;
111
+ _useGuidelines : boolean ;
112
+
88
113
/** Template for step label if it exists. */
89
114
@ContentChild ( CdkStepLabel ) stepLabel : CdkStepLabel ;
90
115
@@ -112,6 +137,9 @@ export class CdkStep implements OnChanges {
112
137
*/
113
138
@Input ( 'aria-labelledby' ) ariaLabelledby : string ;
114
139
140
+ /** State of the step. */
141
+ @Input ( ) state : StepState ;
142
+
115
143
/** Whether the user can return to this step once it has been marked as complted. */
116
144
@Input ( )
117
145
get editable ( ) : boolean { return this . _editable ; }
@@ -128,51 +156,42 @@ export class CdkStep implements OnChanges {
128
156
}
129
157
private _optional = false ;
130
158
131
- /** State of the step. */
132
- @Input ( )
133
- get state ( ) : StepState | string | null { return this . _state ; }
134
- set state ( value : StepState | string | null ) {
135
- this . _state = value ;
136
- }
137
- private _state : StepState | string | null = null ;
138
-
139
- /** Whether to show the step is in an error state. */
140
- @Input ( )
141
- get showError ( ) : boolean { return this . _showError ; }
142
- set showError ( value : boolean ) {
143
- this . _showError = value ;
144
- }
145
- private _showError : boolean = false ;
146
-
147
159
/** Whether step is marked as completed. */
148
160
@Input ( )
149
161
get completed ( ) : boolean {
150
- return this . _customCompleted == null ? this . _defaultCompleted ( ) : this . _customCompleted ;
162
+ return this . _customCompleted == null ? this . _getDefaultCompleted ( ) : this . _customCompleted ;
151
163
}
152
164
set completed ( value : boolean ) {
153
165
this . _customCompleted = coerceBooleanProperty ( value ) ;
154
166
}
155
167
private _customCompleted : boolean | null = null ;
156
168
157
- private _defaultCompleted ( ) {
169
+ private _getDefaultCompleted ( ) {
158
170
return this . stepControl ? this . stepControl . valid && this . interacted : this . interacted ;
159
171
}
160
172
161
173
/** Whether step has error. */
162
174
@Input ( )
163
175
get hasError ( ) : boolean {
164
- return this . _customError == null ? this . _getDefaultError : this . _customError ;
176
+ return this . _customError || this . _getDefaultError ( ) ;
165
177
}
166
178
set hasError ( value : boolean ) {
167
179
this . _customError = coerceBooleanProperty ( value ) ;
168
180
}
169
181
private _customError : boolean | null = null ;
170
182
171
- private get _getDefaultError ( ) {
183
+ private _getDefaultError ( ) {
172
184
return this . stepControl && this . stepControl . invalid ;
173
185
}
174
186
175
- constructor ( @Inject ( forwardRef ( ( ) => CdkStepper ) ) private _stepper : CdkStepper ) { }
187
+ constructor (
188
+ @Inject ( forwardRef ( ( ) => CdkStepper ) ) private _stepper : CdkStepper ,
189
+ @Optional ( ) @Inject ( MAT_STEPPER_GLOBAL_OPTIONS ) stepperOptions : StepperOptions
190
+ ) {
191
+ this . _stepperOptions = stepperOptions ? stepperOptions : { } ;
192
+ this . _showError = ! ! this . _stepperOptions . showError ;
193
+ this . _useGuidelines = ! ! this . _stepperOptions . useGuidelines ;
194
+ }
176
195
177
196
/** Selects this step component. */
178
197
select ( ) : void {
@@ -343,20 +362,41 @@ export class CdkStepper implements AfterViewInit, OnDestroy {
343
362
}
344
363
345
364
/** Returns the type of icon to be displayed. */
346
- _getIndicatorType ( index : number , state : StepState | string | null = null ) : StepState | string {
365
+ _getIndicatorType ( index : number , state : StepState = STEP_STATE . NUMBER ) : StepState {
347
366
const step = this . _steps . toArray ( ) [ index ] ;
348
367
const isCurrentStep = this . _isCurrentStep ( index ) ;
349
368
350
- if ( step . showError && step . hasError && ! isCurrentStep ) {
369
+ if ( step . _useGuidelines ) {
370
+ return this . _getGuidelineLogic ( step , isCurrentStep , state ) ;
371
+ } else {
372
+ return this . _getDefaultIndicatorLogic ( step , isCurrentStep ) ;
373
+ }
374
+ }
375
+
376
+ private _getDefaultIndicatorLogic ( step : CdkStep , isCurrentStep : boolean ) : StepState {
377
+ if ( step . _showError && step . hasError && ! isCurrentStep ) {
378
+ return STEP_STATE . ERROR ;
379
+ } else if ( ! step . completed || isCurrentStep ) {
380
+ return STEP_STATE . NUMBER ;
381
+ } else {
382
+ return step . editable ? STEP_STATE . EDIT : STEP_STATE . DONE ;
383
+ }
384
+ }
385
+
386
+ private _getGuidelineLogic (
387
+ step : CdkStep ,
388
+ isCurrentStep : boolean ,
389
+ state : StepState = STEP_STATE . NUMBER ) : StepState {
390
+ if ( step . _showError && step . hasError && ! isCurrentStep ) {
351
391
return STEP_STATE . ERROR ;
352
392
} else if ( step . completed && ! isCurrentStep ) {
353
393
return STEP_STATE . DONE ;
354
394
} else if ( step . completed && isCurrentStep ) {
355
- return state || STEP_STATE . NUMBER ;
395
+ return state ;
356
396
} else if ( step . editable && isCurrentStep ) {
357
397
return STEP_STATE . EDIT ;
358
398
} else {
359
- return state || STEP_STATE . NUMBER ;
399
+ return state ;
360
400
}
361
401
}
362
402
0 commit comments