-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathstep-header.ts
152 lines (128 loc) · 4.42 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
147
148
149
150
151
152
/**
* @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.dev/license
*/
import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Input,
OnDestroy,
ViewEncapsulation,
TemplateRef,
AfterViewInit,
inject,
} 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 {_StructuralStylesLoader, MatRipple, ThemePalette} from '../core';
import {MatIcon} from '../icon';
import {NgTemplateOutlet} from '@angular/common';
import {_CdkPrivateStyleLoader, _VisuallyHiddenLoader} from '@angular/cdk/private';
@Component({
selector: 'mat-step-header',
templateUrl: 'step-header.html',
styleUrl: 'step-header.css',
host: {
'class': 'mat-step-header',
'[class]': '"mat-" + (color || "primary")',
'role': 'button',
},
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [MatRipple, NgTemplateOutlet, MatIcon],
})
export class MatStepHeader extends CdkStepHeader implements AfterViewInit, OnDestroy {
_intl = inject(MatStepperIntl);
private _focusMonitor = inject(FocusMonitor);
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;
/**
* Theme color of the step header. This API is supported in M2 themes only, it
* has no effect in M3 themes. For color customization in M3, see https://material.angular.io/components/stepper/styling.
*
* For information on applying color variants in M3, see
* https://material.angular.io/guide/material-2-theming#optional-add-backwards-compatibility-styles-for-color-variants
*/
@Input() color: ThemePalette;
constructor(...args: unknown[]);
constructor() {
super();
const styleLoader = inject(_CdkPrivateStyleLoader);
styleLoader.load(_StructuralStylesLoader);
styleLoader.load(_VisuallyHiddenLoader);
const changeDetectorRef = inject(ChangeDetectorRef);
this._intlSubscription = this._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. */
override 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;
}
}