Skip to content

Commit 5ad5176

Browse files
Michael Hoffmannmmalerba
authored andcommitted
Update creating-a-custom-stepper-using-the-cdk-stepper.md (#14907)
* Update creating-a-custom-stepper-using-the-cdk-stepper.md Simplified step content projection * Update creating-a-custom-stepper-using-the-cdk-stepper.md * Updated example
1 parent 34025d8 commit 5ad5176

File tree

4 files changed

+25
-49
lines changed

4 files changed

+25
-49
lines changed

guides/creating-a-custom-stepper-using-the-cdk-stepper.md

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,6 @@ Now we are ready to create our custom stepper component. Therefore, we need to c
2222
providers: [{ provide: CdkStepper, useExisting: CustomStepperComponent }]
2323
})
2424
export class CustomStepperComponent extends CdkStepper {
25-
/** Whether the validity of previous steps should be checked or not */
26-
linear: boolean;
27-
28-
/** The index of the selected step. */
29-
selectedIndex: number;
30-
31-
/** The list of step components that the stepper is holding. */
32-
steps: QueryList<CdkStep>;
33-
3425
onClick(index: number): void {
3526
this.selectedIndex = index;
3627
}
@@ -47,12 +38,10 @@ This is the HTML template of our custom stepper component:
4738
<section class="container">
4839
<header><h2>Step {{selectedIndex + 1}}/{{steps.length}}</h2></header>
4940

50-
<section *ngFor="let step of steps; let i = index;">
51-
<div [style.display]="selectedIndex === i ? 'block' : 'none'">
52-
<!-- Content from the CdkStep is projected here -->
53-
<ng-container [ngTemplateOutlet]="step.content"></ng-container>
54-
</div>
55-
</section>
41+
<div [style.display]="selected ? 'block' : 'none'">
42+
<!-- Content from the CdkStep is projected here -->
43+
<ng-container [ngTemplateOutlet]="selected.content"></ng-container>
44+
</div>
5645

5746
<footer class="step-navigation-bar">
5847
<button class="nav-button" cdkStepperPrevious>&larr;</button>
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
<example-custom-stepper>
2-
<cdk-step>
3-
<p>This is any content of "Step 1"</p>
4-
</cdk-step>
5-
<cdk-step>
6-
<p>This is any content of "Step 2"</p>
7-
</cdk-step>
8-
</example-custom-stepper>
2+
<cdk-step> <p>This is any content of "Step 1"</p> </cdk-step>
3+
<cdk-step> <p>This is any content of "Step 2"</p> </cdk-step>
4+
</example-custom-stepper>

src/material-examples/cdk-custom-stepper-without-form/cdk-custom-stepper-without-form-example.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import {Component, QueryList, ChangeDetectorRef} from '@angular/core';
2-
import {CdkStepper, CdkStep} from '@angular/cdk/stepper';
3-
import {Directionality} from '@angular/cdk/bidi';
1+
import {Component} from '@angular/core';
2+
import {CdkStepper} from '@angular/cdk/stepper';
43

54
/** @title A custom CDK stepper without a form */
65
@Component({
@@ -15,22 +14,9 @@ export class CdkCustomStepperWithoutFormExample {}
1514
selector: 'example-custom-stepper',
1615
templateUrl: './example-custom-stepper.html',
1716
styleUrls: ['./example-custom-stepper.css'],
18-
providers: [{ provide: CdkStepper, useExisting: CustomStepper }],
17+
providers: [{ provide: CdkStepper, useExisting: CustomStepper }]
1918
})
2019
export class CustomStepper extends CdkStepper {
21-
/** Whether the validity of previous steps should be checked or not */
22-
linear: boolean;
23-
24-
/** The index of the selected step. */
25-
selectedIndex: number;
26-
27-
/** The list of step components that the stepper is holding. */
28-
steps: QueryList<CdkStep>;
29-
30-
constructor(dir: Directionality, changeDetectorRef: ChangeDetectorRef) {
31-
super(dir, changeDetectorRef);
32-
}
33-
3420
onClick(index: number): void {
3521
this.selectedIndex = index;
3622
}
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
<section class="example-container">
22
<header>
3-
<h2>Step {{selectedIndex + 1}}/{{steps.length}}</h2>
3+
<h2>Step {{ selectedIndex + 1 }}/{{ steps.length }}</h2>
44
</header>
5-
6-
<section *ngFor="let step of steps; let i = index; let isLast = last">
7-
<div [style.display]="selectedIndex === i ? 'block' : 'none'">
8-
<ng-container [ngTemplateOutlet]="step.content"></ng-container>
9-
</div>
10-
</section>
11-
5+
6+
<div [style.display]="selected ? 'block' : 'none'">
7+
<ng-container [ngTemplateOutlet]="selected.content"></ng-container>
8+
</div>
9+
1210
<footer class="example-step-navigation-bar">
1311
<button class="example-nav-button" cdkStepperPrevious>&larr;</button>
14-
<button class="example-step" *ngFor="let step of steps; let i = index;" [ngClass]="{'example-active': selectedIndex === i}" (click)="onClick(i)">Step {{i + 1}}</button>
12+
<button
13+
class="example-step"
14+
*ngFor="let step of steps; let i = index"
15+
[ngClass]="{ 'example-active': selectedIndex === i }"
16+
(click)="onClick(i)"
17+
>
18+
Step {{ i + 1 }}
19+
</button>
1520
<button class="example-nav-button" cdkStepperNext>&rarr;</button>
1621
</footer>
17-
</section>
22+
</section>

0 commit comments

Comments
 (0)