/
step-header.ts
146 lines (123 loc) · 3.94 KB
/
step-header.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
/**
* @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 {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
Input,
OnDestroy,
ViewEncapsulation,
TemplateRef,
AfterViewInit,
} from '@angular/core';
import {Subscription} from 'rxjs';
import {MatStepLabel} from './step-label';
import {MatStepperIntl} from './stepper-intl';
import {MatStepperIconContext} from './stepper-icon';
import {CdkStepHeader, StepState} from '@angular/cdk/stepper';
import {CanColorCtor, mixinColor, CanColor} from '@angular/material/core';
// Boilerplate for applying mixins to MatStepHeader.
/** @docs-private */
class MatStepHeaderBase extends CdkStepHeader {
constructor(elementRef: ElementRef) {
super(elementRef);
}
}
const _MatStepHeaderMixinBase: CanColorCtor & typeof MatStepHeaderBase =
mixinColor(MatStepHeaderBase, 'primary');
@Component({
selector: 'mat-step-header',
templateUrl: 'step-header.html',
styleUrls: ['step-header.css'],
inputs: ['color'],
host: {
'class': 'mat-step-header mat-focus-indicator',
'role': 'tab',
},
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MatStepHeader extends _MatStepHeaderMixinBase implements AfterViewInit, OnDestroy,
CanColor {
private _intlSubscription: Subscription;
/** State of the given step. */
@Input() state: StepState;
/** Label of the given step. */
@Input() label: MatStepLabel | string;
/** Error message to display when there's an error. */
@Input() errorMessage: string;
/** Overrides for the header icons, passed in via the stepper. */
@Input() iconOverrides: {[key: string]: TemplateRef<MatStepperIconContext>};
/** Index of the given step. */
@Input() index: number;
/** Whether the given step is selected. */
@Input() selected: boolean;
/** Whether the given step label is active. */
@Input() active: boolean;
/** Whether the given step is optional. */
@Input() optional: boolean;
/** Whether the ripple should be disabled. */
@Input() disableRipple: boolean;
constructor(
public _intl: MatStepperIntl,
private _focusMonitor: FocusMonitor,
_elementRef: ElementRef<HTMLElement>,
changeDetectorRef: ChangeDetectorRef) {
super(_elementRef);
this._intlSubscription = _intl.changes.subscribe(() => changeDetectorRef.markForCheck());
}
ngAfterViewInit() {
this._focusMonitor.monitor(this._elementRef, true);
}
ngOnDestroy() {
this._intlSubscription.unsubscribe();
this._focusMonitor.stopMonitoring(this._elementRef);
}
/** Focuses the step header. */
focus(origin?: FocusOrigin, options?: FocusOptions) {
if (origin) {
this._focusMonitor.focusVia(this._elementRef, origin, options);
} else {
this._elementRef.nativeElement.focus(options);
}
}
/** Returns string label of given step if it is a text label. */
_stringLabel(): string | null {
return this.label instanceof MatStepLabel ? null : this.label;
}
/** Returns MatStepLabel if the label of given step is a template label. */
_templateLabel(): MatStepLabel | null {
return this.label instanceof MatStepLabel ? this.label : null;
}
/** Returns the host HTML element. */
_getHostElement() {
return this._elementRef.nativeElement;
}
/** Template context variables that are exposed to the `matStepperIcon` instances. */
_getIconContext(): MatStepperIconContext {
return {
index: this.index,
active: this.active,
optional: this.optional
};
}
_getDefaultTextForState(state: StepState): string {
if (state == 'number') {
return `${this.index + 1}`;
}
if (state == 'edit') {
return 'create';
}
if (state == 'error') {
return 'warning';
}
return state;
}
}