Skip to content

Commit 4728389

Browse files
committed
fix(stepper): unable to internationalize labels
Adds a provider that allows for labels inside the stepper to be internationalized.
1 parent 53c42a4 commit 4728389

File tree

7 files changed

+91
-17
lines changed

7 files changed

+91
-17
lines changed

src/lib/stepper/public_api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ export * from './step-label';
1111
export * from './stepper';
1212
export * from './stepper-button';
1313
export * from './step-header';
14-
14+
export * from './stepper-intl';

src/lib/stepper/step-header.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
<!-- It there is no label template, fall back to the text label. -->
1414
<div class="mat-step-text-label" *ngIf="_stringLabel()">{{label}}</div>
1515

16-
<div class="mat-step-optional" *ngIf="optional">Optional</div>
16+
<div class="mat-step-optional" *ngIf="optional">{{_intl.optionalLabel}}</div>
1717
</div>
1818

src/lib/stepper/step-header.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@
77
*/
88

99
import {coerceBooleanProperty, coerceNumberProperty} from '@angular/cdk/coercion';
10-
import {Component, Input, ViewEncapsulation} from '@angular/core';
10+
import {
11+
Component,
12+
Input,
13+
ViewEncapsulation,
14+
ChangeDetectorRef,
15+
OnDestroy,
16+
} from '@angular/core';
17+
import {MATERIAL_COMPATIBILITY_MODE} from '@angular/material/core';
1118
import {MatStepLabel} from './step-label';
19+
import {MatStepperIntl} from './stepper-intl';
20+
import {Subscription} from 'rxjs/Subscription';
1221

1322

1423
@Component({
@@ -23,7 +32,9 @@ import {MatStepLabel} from './step-label';
2332
encapsulation: ViewEncapsulation.None,
2433
preserveWhitespaces: false,
2534
})
26-
export class MatStepHeader {
35+
export class MatStepHeader implements OnDestroy {
36+
private _intlSubscription: Subscription;
37+
2738
/** Icon for the given step. */
2839
@Input() icon: string;
2940

@@ -62,6 +73,14 @@ export class MatStepHeader {
6273
}
6374
private _optional: boolean;
6475

76+
constructor(public _intl: MatStepperIntl, changeDetectorRef: ChangeDetectorRef) {
77+
this._intlSubscription = _intl.changes.subscribe(() => changeDetectorRef.markForCheck());
78+
}
79+
80+
ngOnDestroy() {
81+
this._intlSubscription.unsubscribe();
82+
}
83+
6584
/** Returns string label of given step if it is a text label. */
6685
_stringLabel(): string | null {
6786
return this.label instanceof MatStepLabel ? null : this.label;

src/lib/stepper/stepper-intl.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Injectable} from '@angular/core';
10+
import {Subject} from 'rxjs/Subject';
11+
12+
13+
/** Stepper data that is required for internationalization. */
14+
@Injectable()
15+
export class MatStepperIntl {
16+
/**
17+
* Stream that emits whenever the labels here are changed. Use this to notify
18+
* components if the labels have changed after initialization.
19+
*/
20+
changes: Subject<void> = new Subject<void>();
21+
22+
/** Label that is rendered below optional steps. */
23+
optionalLabel = 'Optional';
24+
}

src/lib/stepper/stepper-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {MatStepHeader} from './step-header';
1717
import {MatStepLabel} from './step-label';
1818
import {MatHorizontalStepper, MatStep, MatStepper, MatVerticalStepper} from './stepper';
1919
import {MatStepperNext, MatStepperPrevious} from './stepper-button';
20+
import {MatStepperIntl} from './stepper-intl';
2021

2122

2223
@NgModule({
@@ -41,5 +42,6 @@ import {MatStepperNext, MatStepperPrevious} from './stepper-button';
4142
],
4243
declarations: [MatHorizontalStepper, MatVerticalStepper, MatStep, MatStepLabel, MatStepper,
4344
MatStepperNext, MatStepperPrevious, MatStepHeader],
45+
providers: [MatStepperIntl],
4446
})
4547
export class MatStepperModule {}

src/lib/stepper/stepper.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ that drives a stepped workflow. Material stepper extends the CDK stepper and has
77
styling.
88

99
### Stepper variants
10-
There are two stepper components: `mat-horizontal-stepper` and `mat-vertical-stepper`. They
11-
can be used the same way. The only difference is the orientation of stepper.
10+
There are two stepper components: `mat-horizontal-stepper` and `mat-vertical-stepper`. They
11+
can be used the same way. The only difference is the orientation of stepper.
1212
`mat-horizontal-stepper` selector can be used to create a horizontal stepper, and
1313
`mat-vertical-stepper` can be used to create a vertical stepper. `mat-step` components need to be
1414
placed inside either one of the two stepper components.
@@ -26,7 +26,7 @@ If a step's label is only text, then the `label` attribute can be used.
2626
</mat-vertical-stepper>
2727
```
2828

29-
For more complex labels, add a template with the `matStepLabel` directive inside the
29+
For more complex labels, add a template with the `matStepLabel` directive inside the
3030
`mat-step`.
3131
```html
3232
<mat-vertical-stepper>
@@ -49,22 +49,22 @@ There are two button directives to support navigation between different steps:
4949
<button mat-button matStepperNext>Next</button>
5050
</div>
5151
</mat-step>
52-
</mat-horizontal-stepper>
52+
</mat-horizontal-stepper>
5353
```
5454

5555
### Linear stepper
5656
The `linear` attribute can be set on `mat-horizontal-stepper` and `mat-vertical-stepper` to create
5757
a linear stepper that requires the user to complete previous steps before proceeding
5858
to following steps. For each `mat-step`, the `stepControl` attribute can be set to the top level
59-
`AbstractControl` that is used to check the validity of the step.
59+
`AbstractControl` that is used to check the validity of the step.
6060

6161
There are two possible approaches. One is using a single form for stepper, and the other is
6262
using a different form for each step.
6363

6464
#### Using a single form
6565
When using a single form for the stepper, `matStepperPrevious` and `matStepperNext` have to be
6666
set to `type="button"` in order to prevent submission of the form before all steps
67-
are completed.
67+
are completed.
6868

6969
```html
7070
<form [formGroup]="formGroup">
@@ -83,7 +83,7 @@ are completed.
8383
</div>
8484
</mat-step>
8585
...
86-
</mat-horizontal-stepper>
86+
</mat-horizontal-stepper>
8787
</form>
8888
```
8989

@@ -106,11 +106,11 @@ are completed.
106106

107107
#### Optional step
108108
If completion of a step in linear stepper is not required, then the `optional` attribute can be set
109-
on `mat-step`.
109+
on `mat-step`.
110110

111111
#### Editable step
112112
By default, steps are editable, which means users can return to previously completed steps and
113-
edit their responses. `editable="true"` can be set on `mat-step` to change the default.
113+
edit their responses. `editable="true"` can be set on `mat-step` to change the default.
114114

115115
#### Completed step
116116
By default, the `completed` attribute of a step returns `true` if the step is valid (in case of
@@ -124,11 +124,25 @@ this default `completed` behavior by setting the `completed` attribute as needed
124124
- <kbd>TAB</kbd>: Focuses the next tabbable element
125125
- <kbd>TAB</kbd>+<kbd>SHIFT</kbd>: Focuses the previous tabbable element
126126

127+
### Localizing labels
128+
Labels used by the stepper are provided through `MatStepperIntl`. Localization of these messages
129+
can be done by providing a subclass with translated values in your application root module.
130+
131+
```ts
132+
@NgModule({
133+
imports: [MatStepperModule],
134+
providers: [
135+
{provide: MatStepperIntl, useClass: MyIntl},
136+
],
137+
})
138+
export class MyApp {}
139+
```
140+
127141
### Accessibility
128142
The stepper is treated as a tabbed view for accessibility purposes, so it is given
129143
`role="tablist"` by default. The header of step that can be clicked to select the step
130144
is given `role="tab"`, and the content that can be expanded upon selection is given
131145
`role="tabpanel"`. `aria-selected` attribute of step header and `aria-expanded` attribute of
132146
step content is automatically set based on step selection change.
133147

134-
The stepper and each step should be given a meaningful label via `aria-label` or `aria-labelledby`.
148+
The stepper and each step should be given a meaningful label via `aria-label` or `aria-labelledby`.

src/lib/stepper/stepper.spec.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import {Directionality} from '@angular/cdk/bidi';
22
import {ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE} from '@angular/cdk/keycodes';
33
import {dispatchKeyboardEvent} from '@angular/cdk/testing';
44
import {Component, DebugElement} from '@angular/core';
5-
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
5+
import {async, ComponentFixture, TestBed, inject} from '@angular/core/testing';
66
import {FormControl, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms';
77
import {By} from '@angular/platform-browser';
88
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
99
import {MatStepperModule} from './index';
1010
import {MatHorizontalStepper, MatStepper, MatVerticalStepper} from './stepper';
1111
import {MatStepperNext, MatStepperPrevious} from './stepper-button';
12-
12+
import {MatStepperIntl} from './stepper-intl';
1313

1414
const VALID_REGEX = /valid/;
1515

@@ -96,6 +96,21 @@ describe('MatHorizontalStepper', () => {
9696
it('should set done icon if step is not editable and is completed', () => {
9797
assertCorrectStepIcon(fixture, false, 'done');
9898
});
99+
100+
it('should re-render when the i18n labels change',
101+
inject([MatStepperIntl], (intl: MatStepperIntl) => {
102+
const header = fixture.debugElement.queryAll(By.css('mat-step-header'))[2].nativeElement;
103+
const optionalLabel = header.querySelector('.mat-step-optional');
104+
105+
expect(optionalLabel).toBeTruthy();
106+
expect(optionalLabel.textContent).toBe('Optional');
107+
108+
intl.optionalLabel = 'Valgfri';
109+
intl.changes.next();
110+
fixture.detectChanges();
111+
112+
expect(optionalLabel.textContent).toBe('Valgfri');
113+
}));
99114
});
100115

101116
describe('RTL', () => {
@@ -684,7 +699,7 @@ function assertCorrectStepIcon(fixture: ComponentFixture<any>,
684699
<button mat-button matStepperNext>Next</button>
685700
</div>
686701
</mat-step>
687-
<mat-step [label]="inputLabel">
702+
<mat-step [label]="inputLabel" optional>
688703
Content 3
689704
<div>
690705
<button mat-button matStepperPrevious>Back</button>

0 commit comments

Comments
 (0)