/
stepper.ts
634 lines (540 loc) · 21 KB
/
stepper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {FocusableOption, FocusKeyManager} from '@angular/cdk/a11y';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {
BooleanInput,
coerceBooleanProperty,
coerceNumberProperty,
NumberInput
} from '@angular/cdk/coercion';
import {ENTER, hasModifierKey, SPACE} from '@angular/cdk/keycodes';
import {DOCUMENT} from '@angular/common';
import {
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChild,
ContentChildren,
Directive,
ElementRef,
EventEmitter,
forwardRef,
Inject,
InjectionToken,
Input,
OnChanges,
OnDestroy,
Optional,
Output,
QueryList,
TemplateRef,
ViewChild,
ViewEncapsulation,
AfterContentInit,
} from '@angular/core';
import {_getFocusedElementPierceShadowDom} from '@angular/cdk/platform';
import {Observable, of as observableOf, Subject} from 'rxjs';
import {startWith, takeUntil} from 'rxjs/operators';
import {CdkStepHeader} from './step-header';
import {CdkStepLabel} from './step-label';
/** Used to generate unique ID for each stepper component. */
let nextId = 0;
/**
* Position state of the content of each step in stepper that is used for transitioning
* the content into correct position upon step selection change.
*/
export type StepContentPositionState = 'previous'|'current'|'next';
/** Possible orientation of a stepper. */
export type StepperOrientation = 'horizontal'|'vertical';
/** Change event emitted on selection changes. */
export class StepperSelectionEvent {
/** Index of the step now selected. */
selectedIndex: number;
/** Index of the step previously selected. */
previouslySelectedIndex: number;
/** The step instance now selected. */
selectedStep: CdkStep;
/** The step instance previously selected. */
previouslySelectedStep: CdkStep;
}
/** The state of each step. */
export type StepState = 'number'|'edit'|'done'|'error'|string;
/** Enum to represent the different states of the steps. */
export const STEP_STATE = {
NUMBER: 'number',
EDIT: 'edit',
DONE: 'done',
ERROR: 'error'
};
/** InjectionToken that can be used to specify the global stepper options. */
export const STEPPER_GLOBAL_OPTIONS = new InjectionToken<StepperOptions>('STEPPER_GLOBAL_OPTIONS');
/** Configurable options for stepper. */
export interface StepperOptions {
/**
* Whether the stepper should display an error state or not.
* Default behavior is assumed to be false.
*/
showError?: boolean;
/**
* Whether the stepper should display the default indicator type
* or not.
* Default behavior is assumed to be true.
*/
displayDefaultIndicatorType?: boolean;
}
@Component({
selector: 'cdk-step',
exportAs: 'cdkStep',
template: '<ng-template><ng-content></ng-content></ng-template>',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CdkStep implements OnChanges {
private _stepperOptions: StepperOptions;
_displayDefaultIndicatorType: boolean;
/** Template for step label if it exists. */
@ContentChild(CdkStepLabel) stepLabel: CdkStepLabel;
/** Template for step content. */
@ViewChild(TemplateRef, {static: true}) content: TemplateRef<any>;
/** The top level abstract control of the step. */
@Input() stepControl: AbstractControlLike;
/** Whether user has attempted to move away from the step. */
interacted = false;
/** Emits when the user has attempted to move away from the step. */
@Output('interacted')
readonly interactedStream: EventEmitter<CdkStep> = new EventEmitter<CdkStep>();
/** Plain text label of the step. */
@Input() label: string;
/** Error message to display when there's an error. */
@Input() errorMessage: string;
/** Aria label for the tab. */
@Input('aria-label') ariaLabel: string;
/**
* Reference to the element that the tab is labelled by.
* Will be cleared if `aria-label` is set at the same time.
*/
@Input('aria-labelledby') ariaLabelledby: string;
/** State of the step. */
@Input() state: StepState;
/** Whether the user can return to this step once it has been marked as completed. */
@Input()
get editable(): boolean {
return this._editable;
}
set editable(value: boolean) {
this._editable = coerceBooleanProperty(value);
}
private _editable = true;
/** Whether the completion of step is optional. */
@Input()
get optional(): boolean {
return this._optional;
}
set optional(value: boolean) {
this._optional = coerceBooleanProperty(value);
}
private _optional = false;
/** Whether step is marked as completed. */
@Input()
get completed(): boolean {
return this._completedOverride == null ? this._getDefaultCompleted() : this._completedOverride;
}
set completed(value: boolean) {
this._completedOverride = coerceBooleanProperty(value);
}
_completedOverride: boolean|null = null;
private _getDefaultCompleted() {
return this.stepControl ? this.stepControl.valid && this.interacted : this.interacted;
}
/** Whether step has an error. */
@Input()
get hasError(): boolean {
return this._customError == null ? this._getDefaultError() : this._customError;
}
set hasError(value: boolean) {
this._customError = coerceBooleanProperty(value);
}
private _customError: boolean|null = null;
private _getDefaultError() {
return this.stepControl && this.stepControl.invalid && this.interacted;
}
constructor(
@Inject(forwardRef(() => CdkStepper)) public _stepper: CdkStepper,
@Optional() @Inject(STEPPER_GLOBAL_OPTIONS) stepperOptions?: StepperOptions) {
this._stepperOptions = stepperOptions ? stepperOptions : {};
this._displayDefaultIndicatorType = this._stepperOptions.displayDefaultIndicatorType !== false;
}
/** Selects this step component. */
select(): void {
this._stepper.selected = this;
}
/** Resets the step to its initial state. Note that this includes resetting form data. */
reset(): void {
this.interacted = false;
if (this._completedOverride != null) {
this._completedOverride = false;
}
if (this._customError != null) {
this._customError = false;
}
if (this.stepControl) {
this.stepControl.reset();
}
}
ngOnChanges() {
// Since basically all inputs of the MatStep get proxied through the view down to the
// underlying MatStepHeader, we have to make sure that change detection runs correctly.
this._stepper._stateChanged();
}
_markAsInteracted() {
if (!this.interacted) {
this.interacted = true;
this.interactedStream.emit(this);
}
}
/** Determines whether the error state can be shown. */
_showError(): boolean {
// We want to show the error state either if the user opted into/out of it using the
// global options, or if they've explicitly set it through the `hasError` input.
return this._stepperOptions.showError ?? this._customError != null;
}
static ngAcceptInputType_editable: BooleanInput;
static ngAcceptInputType_hasError: BooleanInput;
static ngAcceptInputType_optional: BooleanInput;
static ngAcceptInputType_completed: BooleanInput;
}
@Directive({
selector: '[cdkStepper]',
exportAs: 'cdkStepper',
})
export class CdkStepper implements AfterContentInit, AfterViewInit, OnDestroy {
/** Emits when the component is destroyed. */
protected readonly _destroyed = new Subject<void>();
/** Used for managing keyboard focus. */
private _keyManager: FocusKeyManager<FocusableOption>;
/** Full list of steps inside the stepper, including inside nested steppers. */
@ContentChildren(CdkStep, {descendants: true}) _steps: QueryList<CdkStep>;
/** Steps that belong to the current stepper, excluding ones from nested steppers. */
readonly steps: QueryList<CdkStep> = new QueryList<CdkStep>();
/** The list of step headers of the steps in the stepper. */
@ContentChildren(CdkStepHeader, {descendants: true}) _stepHeader: QueryList<CdkStepHeader>;
/** List of step headers sorted based on their DOM order. */
private _sortedHeaders = new QueryList<CdkStepHeader>();
/** Whether the validity of previous steps should be checked or not. */
@Input()
get linear(): boolean {
return this._linear;
}
set linear(value: boolean) {
this._linear = coerceBooleanProperty(value);
}
private _linear = false;
/** The index of the selected step. */
@Input()
get selectedIndex() {
return this._selectedIndex;
}
set selectedIndex(index: number) {
const newIndex = coerceNumberProperty(index);
if (this.steps && this._steps) {
// Ensure that the index can't be out of bounds.
if (!this._isValidIndex(index) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
throw Error('cdkStepper: Cannot assign out-of-bounds value to `selectedIndex`.');
}
this.selected?._markAsInteracted();
if (this._selectedIndex !== newIndex && !this._anyControlsInvalidOrPending(newIndex) &&
(newIndex >= this._selectedIndex || this.steps.toArray()[newIndex].editable)) {
this._updateSelectedItemIndex(index);
}
} else {
this._selectedIndex = newIndex;
}
}
private _selectedIndex = 0;
/** The step that is selected. */
@Input()
get selected(): CdkStep | undefined {
return this.steps ? this.steps.toArray()[this.selectedIndex] : undefined;
}
set selected(step: CdkStep | undefined) {
this.selectedIndex = (step && this.steps) ? this.steps.toArray().indexOf(step) : -1;
}
/** Event emitted when the selected step has changed. */
@Output() readonly selectionChange = new EventEmitter<StepperSelectionEvent>();
/** Used to track unique ID for each stepper component. */
_groupId: number;
/** Orientation of the stepper. */
@Input()
get orientation(): StepperOrientation { return this._orientation; }
set orientation(value: StepperOrientation) {
// This is a protected method so that `MatSteppter` can hook into it.
this._orientation = value;
if (this._keyManager) {
this._keyManager.withVerticalOrientation(value === 'vertical');
}
}
/**
* @deprecated To be turned into a private property. Use `orientation` instead.
* @breaking-change 13.0.0
*/
protected _orientation: StepperOrientation = 'horizontal';
constructor(
@Optional() private _dir: Directionality, private _changeDetectorRef: ChangeDetectorRef,
private _elementRef: ElementRef<HTMLElement>,
/**
* @deprecated No longer in use, to be removed.
* @breaking-change 13.0.0
*/
@Inject(DOCUMENT) _document: any) {
this._groupId = nextId++;
}
ngAfterContentInit() {
this._steps.changes
.pipe(startWith(this._steps), takeUntil(this._destroyed))
.subscribe((steps: QueryList<CdkStep>) => {
this.steps.reset(steps.filter(step => step._stepper === this));
this.steps.notifyOnChanges();
});
}
ngAfterViewInit() {
// If the step headers are defined outside of the `ngFor` that renders the steps, like in the
// Material stepper, they won't appear in the `QueryList` in the same order as they're
// rendered in the DOM which will lead to incorrect keyboard navigation. We need to sort
// them manually to ensure that they're correct. Alternatively, we can change the Material
// template to inline the headers in the `ngFor`, but that'll result in a lot of
// code duplciation. See #23539.
this._stepHeader.changes
.pipe(startWith(this._stepHeader), takeUntil(this._destroyed))
.subscribe((headers: QueryList<CdkStepHeader>) => {
this._sortedHeaders.reset(headers.toArray().sort((a, b) => {
const documentPosition =
a._elementRef.nativeElement.compareDocumentPosition(b._elementRef.nativeElement);
// `compareDocumentPosition` returns a bitmask so we have to use a bitwise operator.
// https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition
// tslint:disable-next-line:no-bitwise
return documentPosition & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1;
}));
this._sortedHeaders.notifyOnChanges();
});
// Note that while the step headers are content children by default, any components that
// extend this one might have them as view children. We initialize the keyboard handling in
// AfterViewInit so we're guaranteed for both view and content children to be defined.
this._keyManager = new FocusKeyManager<FocusableOption>(this._sortedHeaders)
.withWrap()
.withHomeAndEnd()
.withVerticalOrientation(this._orientation === 'vertical');
(this._dir ? (this._dir.change as Observable<Direction>) : observableOf<Direction>())
.pipe(startWith(this._layoutDirection()), takeUntil(this._destroyed))
.subscribe(direction => this._keyManager.withHorizontalOrientation(direction));
this._keyManager.updateActiveItem(this._selectedIndex);
// No need to `takeUntil` here, because we're the ones destroying `steps`.
this.steps.changes.subscribe(() => {
if (!this.selected) {
this._selectedIndex = Math.max(this._selectedIndex - 1, 0);
}
});
// The logic which asserts that the selected index is within bounds doesn't run before the
// steps are initialized, because we don't how many steps there are yet so we may have an
// invalid index on init. If that's the case, auto-correct to the default so we don't throw.
if (!this._isValidIndex(this._selectedIndex)) {
this._selectedIndex = 0;
}
}
ngOnDestroy() {
this.steps.destroy();
this._sortedHeaders.destroy();
this._destroyed.next();
this._destroyed.complete();
}
/** Selects and focuses the next step in list. */
next(): void {
this.selectedIndex = Math.min(this._selectedIndex + 1, this.steps.length - 1);
}
/** Selects and focuses the previous step in list. */
previous(): void {
this.selectedIndex = Math.max(this._selectedIndex - 1, 0);
}
/** Resets the stepper to its initial state. Note that this includes clearing form data. */
reset(): void {
this._updateSelectedItemIndex(0);
this.steps.forEach(step => step.reset());
this._stateChanged();
}
/** Returns a unique id for each step label element. */
_getStepLabelId(i: number): string {
return `cdk-step-label-${this._groupId}-${i}`;
}
/** Returns unique id for each step content element. */
_getStepContentId(i: number): string {
return `cdk-step-content-${this._groupId}-${i}`;
}
/** Marks the component to be change detected. */
_stateChanged() {
this._changeDetectorRef.markForCheck();
}
/** Returns position state of the step with the given index. */
_getAnimationDirection(index: number): StepContentPositionState {
const position = index - this._selectedIndex;
if (position < 0) {
return this._layoutDirection() === 'rtl' ? 'next' : 'previous';
} else if (position > 0) {
return this._layoutDirection() === 'rtl' ? 'previous' : 'next';
}
return 'current';
}
/** Returns the type of icon to be displayed. */
_getIndicatorType(index: number, state: StepState = STEP_STATE.NUMBER): StepState {
const step = this.steps.toArray()[index];
const isCurrentStep = this._isCurrentStep(index);
return step._displayDefaultIndicatorType ? this._getDefaultIndicatorLogic(step, isCurrentStep) :
this._getGuidelineLogic(step, isCurrentStep, state);
}
private _getDefaultIndicatorLogic(step: CdkStep, isCurrentStep: boolean): StepState {
if (step._showError() && step.hasError && !isCurrentStep) {
return STEP_STATE.ERROR;
} else if (!step.completed || isCurrentStep) {
return STEP_STATE.NUMBER;
} else {
return step.editable ? STEP_STATE.EDIT : STEP_STATE.DONE;
}
}
private _getGuidelineLogic(
step: CdkStep, isCurrentStep: boolean, state: StepState = STEP_STATE.NUMBER): StepState {
if (step._showError() && step.hasError && !isCurrentStep) {
return STEP_STATE.ERROR;
} else if (step.completed && !isCurrentStep) {
return STEP_STATE.DONE;
} else if (step.completed && isCurrentStep) {
return state;
} else if (step.editable && isCurrentStep) {
return STEP_STATE.EDIT;
} else {
return state;
}
}
private _isCurrentStep(index: number) {
return this._selectedIndex === index;
}
/** Returns the index of the currently-focused step header. */
_getFocusIndex() {
return this._keyManager ? this._keyManager.activeItemIndex : this._selectedIndex;
}
private _updateSelectedItemIndex(newIndex: number): void {
const stepsArray = this.steps.toArray();
this.selectionChange.emit({
selectedIndex: newIndex,
previouslySelectedIndex: this._selectedIndex,
selectedStep: stepsArray[newIndex],
previouslySelectedStep: stepsArray[this._selectedIndex],
});
// If focus is inside the stepper, move it to the next header, otherwise it may become
// lost when the active step content is hidden. We can't be more granular with the check
// (e.g. checking whether focus is inside the active step), because we don't have a
// reference to the elements that are rendering out the content.
this._containsFocus() ? this._keyManager.setActiveItem(newIndex) :
this._keyManager.updateActiveItem(newIndex);
this._selectedIndex = newIndex;
this._stateChanged();
}
_onKeydown(event: KeyboardEvent) {
const hasModifier = hasModifierKey(event);
const keyCode = event.keyCode;
const manager = this._keyManager;
if (manager.activeItemIndex != null && !hasModifier &&
(keyCode === SPACE || keyCode === ENTER)) {
this.selectedIndex = manager.activeItemIndex;
event.preventDefault();
} else {
manager.onKeydown(event);
}
}
private _anyControlsInvalidOrPending(index: number): boolean {
if (this._linear && index >= 0) {
return this.steps.toArray().slice(0, index).some(step => {
const control = step.stepControl;
const isIncomplete =
control ? (control.invalid || control.pending || !step.interacted) : !step.completed;
return isIncomplete && !step.optional && !step._completedOverride;
});
}
return false;
}
private _layoutDirection(): Direction {
return this._dir && this._dir.value === 'rtl' ? 'rtl' : 'ltr';
}
/** Checks whether the stepper contains the focused element. */
private _containsFocus(): boolean {
const stepperElement = this._elementRef.nativeElement;
const focusedElement = _getFocusedElementPierceShadowDom();
return stepperElement === focusedElement || stepperElement.contains(focusedElement);
}
/** Checks whether the passed-in index is a valid step index. */
private _isValidIndex(index: number): boolean {
return index > -1 && (!this.steps || index < this.steps.length);
}
static ngAcceptInputType_editable: BooleanInput;
static ngAcceptInputType_optional: BooleanInput;
static ngAcceptInputType_completed: BooleanInput;
static ngAcceptInputType_hasError: BooleanInput;
static ngAcceptInputType_linear: BooleanInput;
static ngAcceptInputType_selectedIndex: NumberInput;
}
/**
* Simplified representation of an "AbstractControl" from @angular/forms.
* Used to avoid having to bring in @angular/forms for a single optional interface.
* @docs-private
*/
interface AbstractControlLike {
asyncValidator: ((control: any) => any) | null;
dirty: boolean;
disabled: boolean;
enabled: boolean;
errors: {[key: string]: any} | null;
invalid: boolean;
parent: any;
pending: boolean;
pristine: boolean;
root: AbstractControlLike;
status: string;
readonly statusChanges: Observable<any>;
touched: boolean;
untouched: boolean;
updateOn: any;
valid: boolean;
validator: ((control: any) => any) | null;
value: any;
readonly valueChanges: Observable<any>;
clearAsyncValidators(): void;
clearValidators(): void;
disable(opts?: any): void;
enable(opts?: any): void;
get(path: (string | number)[] | string): AbstractControlLike | null;
getError(errorCode: string, path?: (string | number)[] | string): any;
hasError(errorCode: string, path?: (string | number)[] | string): boolean;
markAllAsTouched(): void;
markAsDirty(opts?: any): void;
markAsPending(opts?: any): void;
markAsPristine(opts?: any): void;
markAsTouched(opts?: any): void;
markAsUntouched(opts?: any): void;
patchValue(value: any, options?: Object): void;
reset(value?: any, options?: Object): void;
setAsyncValidators(newValidator: (control: any) => any |
((control: any) => any)[] | null): void;
setErrors(errors: {[key: string]: any} | null, opts?: any): void;
setParent(parent: any): void;
setValidators(newValidator: (control: any) => any |
((control: any) => any)[] | null): void;
setValue(value: any, options?: Object): void;
updateValueAndValidity(opts?: any): void;
patchValue(value: any, options?: any): void;
reset(formState?: any, options?: any): void;
setValue(value: any, options?: any): void;
}