Skip to content

Commit 5f07f2c

Browse files
authored
docs(material/stepper): add live example of responsive stepper (#22401)
We recently added support for controlling the orientation of a stepper dynamically. These changes include a live of example of a responsive stepper since it's the most common use case.
1 parent 6720405 commit 5f07f2c

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

src/components-examples/material/stepper/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ng_module(
1414
]),
1515
module_name = "@angular/components-examples/material/stepper",
1616
deps = [
17+
"//src/cdk/layout",
1718
"//src/cdk/stepper",
1819
"//src/cdk/testing",
1920
"//src/cdk/testing/testbed",
@@ -22,6 +23,7 @@ ng_module(
2223
"//src/material/input",
2324
"//src/material/stepper",
2425
"//src/material/stepper/testing",
26+
"@npm//@angular/common",
2527
"@npm//@angular/forms",
2628
"@npm//@angular/platform-browser",
2729
"@npm//@angular/platform-browser-dynamic",

src/components-examples/material/stepper/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {NgModule} from '@angular/core';
22
import {ReactiveFormsModule} from '@angular/forms';
3+
import {CommonModule} from '@angular/common';
34
import {MatButtonModule} from '@angular/material/button';
45
import {MatIconModule} from '@angular/material/icon';
56
import {MatInputModule} from '@angular/material/input';
@@ -15,6 +16,7 @@ import {StepperStatesExample} from './stepper-states/stepper-states-example';
1516
import {StepperVerticalExample} from './stepper-vertical/stepper-vertical-example';
1617
import {StepperHarnessExample} from './stepper-harness/stepper-harness-example';
1718
import {StepperLazyContentExample} from './stepper-lazy-content/stepper-lazy-content-example';
19+
import {StepperResponsiveExample} from './stepper-responsive/stepper-responsive-example';
1820

1921
export {
2022
StepperEditableExample,
@@ -26,6 +28,7 @@ export {
2628
StepperStatesExample,
2729
StepperVerticalExample,
2830
StepperLazyContentExample,
31+
StepperResponsiveExample,
2932
};
3033

3134
const EXAMPLES = [
@@ -38,6 +41,7 @@ const EXAMPLES = [
3841
StepperStatesExample,
3942
StepperVerticalExample,
4043
StepperLazyContentExample,
44+
StepperResponsiveExample,
4145
];
4246

4347
@NgModule({
@@ -47,6 +51,7 @@ const EXAMPLES = [
4751
MatInputModule,
4852
MatStepperModule,
4953
ReactiveFormsModule,
54+
CommonModule,
5055
],
5156
declarations: EXAMPLES,
5257
exports: EXAMPLES,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.example-stepper {
2+
margin-top: 8px;
3+
}
4+
5+
.mat-form-field {
6+
margin-top: 16px;
7+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<ng-container [ngSwitch]="stepperOrientation | async">
2+
<div *ngSwitchCase="'horizontal'">Make your screen smaller to see a vertical stepper</div>
3+
<div *ngSwitchCase="'vertical'">Make your screen larger to see a horizontal stepper</div>
4+
</ng-container>
5+
6+
<mat-stepper
7+
class="example-stepper"
8+
[orientation]="(stepperOrientation | async)!">
9+
<mat-step [stepControl]="firstFormGroup" label="Fill out your name">
10+
<form [formGroup]="firstFormGroup">
11+
<mat-form-field>
12+
<mat-label>Name</mat-label>
13+
<input matInput placeholder="Last name, First name" formControlName="firstCtrl" required>
14+
</mat-form-field>
15+
<div>
16+
<button mat-button matStepperNext>Next</button>
17+
</div>
18+
</form>
19+
</mat-step>
20+
<mat-step [stepControl]="secondFormGroup" label="Fill out your address">
21+
<form [formGroup]="secondFormGroup">
22+
<mat-form-field>
23+
<mat-label>Address</mat-label>
24+
<input matInput formControlName="secondCtrl" placeholder="Ex. 1 Main St, New York, NY"
25+
required>
26+
</mat-form-field>
27+
<div>
28+
<button mat-button matStepperPrevious>Back</button>
29+
<button mat-button matStepperNext>Next</button>
30+
</div>
31+
</form>
32+
</mat-step>
33+
<mat-step [stepControl]="thirdFormGroup" label="Fill out your phone number">
34+
<form [formGroup]="thirdFormGroup">
35+
<mat-form-field>
36+
<mat-label>Phone number</mat-label>
37+
<input matInput formControlName="thirdCtrl" placeholder="Ex. 12345678" required>
38+
</mat-form-field>
39+
<div>
40+
<button mat-button matStepperPrevious>Back</button>
41+
<button mat-button matStepperNext>Next</button>
42+
</div>
43+
</form>
44+
</mat-step>
45+
<mat-step>
46+
<ng-template matStepLabel>Done</ng-template>
47+
<p>You are now done.</p>
48+
<div>
49+
<button mat-button matStepperPrevious>Back</button>
50+
</div>
51+
</mat-step>
52+
</mat-stepper>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {Component} from '@angular/core';
2+
import {FormBuilder, Validators} from '@angular/forms';
3+
import {BreakpointObserver} from '@angular/cdk/layout';
4+
import {StepperOrientation} from '@angular/material/stepper';
5+
import {Observable} from 'rxjs';
6+
import {map} from 'rxjs/operators';
7+
8+
/**
9+
* @title Stepper responsive
10+
*/
11+
@Component({
12+
selector: 'stepper-responsive-example',
13+
templateUrl: 'stepper-responsive-example.html',
14+
styleUrls: ['stepper-responsive-example.css'],
15+
})
16+
export class StepperResponsiveExample {
17+
firstFormGroup = this._formBuilder.group({
18+
firstCtrl: ['', Validators.required]
19+
});
20+
secondFormGroup = this._formBuilder.group({
21+
secondCtrl: ['', Validators.required]
22+
});
23+
thirdFormGroup = this._formBuilder.group({
24+
thirdCtrl: ['', Validators.required]
25+
});
26+
stepperOrientation: Observable<StepperOrientation>;
27+
28+
constructor(private _formBuilder: FormBuilder, breakpointObserver: BreakpointObserver) {
29+
this.stepperOrientation = breakpointObserver.observe('(min-width: 800px)')
30+
.pipe(map(({matches}) => matches ? 'horizontal' : 'vertical'));
31+
}
32+
}

src/material/stepper/stepper.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ an `ng-template` with the `matStepContent` attribute.
187187

188188
<!-- example(stepper-lazy-content) -->
189189

190+
### Responsive stepper
191+
If your app supports a wide variety of screens and a stepper's layout doesn't fit a particular
192+
screen size, you can control its `orientation` dynamically to change the layout based on the
193+
viewport.
194+
195+
<!-- example(stepper-responsive) -->
196+
190197
### Keyboard interaction
191198
- <kbd>LEFT_ARROW</kbd>: Focuses the previous step header
192199
- <kbd>RIGHT_ARROW</kbd>: Focuses the next step header

0 commit comments

Comments
 (0)