Skip to content

Commit 84bfa7c

Browse files
authored
docs(cdk/stepper): add linear stepper example
(#19518)
1 parent 00e2171 commit 84bfa7c

6 files changed

+124
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.example-toggle-linear-button {
2+
margin-left: 10px;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<example-custom-linear-stepper [linear]="isLinear">
2+
<cdk-step [stepControl]="firstFormGroup">
3+
<label for="stepOneInput">Step 1 input</label>
4+
<form [formGroup]="firstFormGroup">
5+
<input placeholder="Input" formControlName="firstControl" id="stepOneInput" required>
6+
</form>
7+
</cdk-step>
8+
<cdk-step [stepControl]="secondFormGroup">
9+
<label for="stepTwoInput">Step 2 input</label>
10+
<form [formGroup]="secondFormGroup">
11+
<input placeholder="Input" formControlName="secondControl" id="stepTwoInput" required>
12+
</form>
13+
</cdk-step>
14+
</example-custom-linear-stepper>
15+
<button class="example-toggle-linear-button" (click)="toggleLinearity()">
16+
{{isLinear ? 'Disable linear mode' : 'Enable linear mode'}}
17+
</button>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {Component} from '@angular/core';
2+
import {CdkStepper} from '@angular/cdk/stepper';
3+
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
4+
5+
/** @title A custom CDK linear stepper with forms */
6+
@Component({
7+
selector: 'cdk-linear-stepper-with-form',
8+
templateUrl: './cdk-linear-stepper-with-form-example.html',
9+
styleUrls: ['./cdk-linear-stepper-with-form-example.css']
10+
})
11+
export class CdkLinearStepperWithFormExample {
12+
isLinear = true;
13+
firstFormGroup: FormGroup;
14+
secondFormGroup: FormGroup;
15+
16+
constructor(private readonly _formBuilder: FormBuilder) {
17+
this.firstFormGroup = this._formBuilder.group({
18+
firstControl: ['', Validators.required]
19+
});
20+
this.secondFormGroup = this._formBuilder.group({
21+
secondControl: ['', Validators.required]
22+
});
23+
}
24+
25+
toggleLinearity() {
26+
this.isLinear = !this.isLinear;
27+
}
28+
}
29+
30+
/** Custom CDK linear stepper component */
31+
@Component({
32+
selector: 'example-custom-linear-stepper',
33+
templateUrl: './example-custom-linear-stepper.html',
34+
styleUrls: ['./example-custom-linear-stepper.css'],
35+
providers: [{provide: CdkStepper, useExisting: CustomLinearStepper}]
36+
})
37+
export class CustomLinearStepper extends CdkStepper {
38+
selectStepByIndex(index: number): void {
39+
this.selectedIndex = index;
40+
}
41+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.example-container {
2+
border: 1px solid black;
3+
padding: 10px;
4+
margin: 10px;
5+
}
6+
7+
.example-step-navigation-bar {
8+
display: flex;
9+
justify-content: flex-start;
10+
margin-top: 10px;
11+
}
12+
13+
.example-step {
14+
background: transparent;
15+
border: 0;
16+
margin: 0 10px;
17+
padding: 10px;
18+
color: black;
19+
}
20+
21+
.example-step.example-active {
22+
border-bottom: 1px solid blue;
23+
color: blue;
24+
}
25+
26+
.example-nav-button {
27+
background: transparent;
28+
border: 0;
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<section class="example-container">
2+
<header>
3+
<h2>Step {{selectedIndex + 1}}/{{steps.length}}</h2>
4+
</header>
5+
6+
<div [ngTemplateOutlet]="selected ? selected.content : null"></div>
7+
8+
<footer class="example-step-navigation-bar">
9+
<button class="example-nav-button" cdkStepperPrevious>&larr;</button>
10+
<button
11+
class="example-step"
12+
[class.example-active]="selectedIndex === i"
13+
*ngFor="let step of steps; let i = index"
14+
(click)="selectStepByIndex(i)"
15+
>
16+
Step {{ i + 1 }}
17+
</button>
18+
<button class="example-nav-button" cdkStepperNext>&rarr;</button>
19+
</footer>
20+
</section>

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,32 @@ import {
55
CdkCustomStepperWithoutFormExample,
66
CustomStepper
77
} from './cdk-custom-stepper-without-form/cdk-custom-stepper-without-form-example';
8+
import {
9+
CdkLinearStepperWithFormExample,
10+
CustomLinearStepper
11+
} from './cdk-linear-stepper-with-form/cdk-linear-stepper-with-form-example';
12+
import {ReactiveFormsModule} from '@angular/forms';
813

914
export {
1015
CdkCustomStepperWithoutFormExample,
1116
CustomStepper,
17+
CdkLinearStepperWithFormExample,
18+
CustomLinearStepper
1219
};
1320

1421
const EXAMPLES = [
1522
CdkCustomStepperWithoutFormExample,
1623
CustomStepper,
24+
CdkLinearStepperWithFormExample,
25+
CustomLinearStepper
1726
];
1827

1928
@NgModule({
20-
imports: [
21-
CdkStepperModule,
22-
CommonModule,
23-
],
29+
imports: [
30+
CdkStepperModule,
31+
CommonModule,
32+
ReactiveFormsModule,
33+
],
2434
declarations: EXAMPLES,
2535
exports: EXAMPLES,
2636
entryComponents: EXAMPLES,

0 commit comments

Comments
 (0)