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