-
Notifications
You must be signed in to change notification settings - Fork 6.8k
docs(cdk-stepper): add guide to create stepper #14710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
guides/creating-a-custom-stepper-using-the-cdk-stepper.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
# Creating a custom stepper using the CDK stepper | ||
|
||
The [CDK stepper](https://material.angular.io/cdk/stepper/overview) allows to build a custom stepper which you can completely style yourself without any specific Material Design styling. | ||
|
||
In this guide, we'll learn how we can build our own custom stepper using the CDK stepper. Here is what we'll build by the end of this guide: | ||
|
||
<!-- example(cdk-custom-stepper-without-form) --> | ||
|
||
## Create our custom stepper component | ||
|
||
Now we are ready to create our custom stepper component. Therefore, we need to create a new Angular component which extends `CdkStepper`: | ||
|
||
**custom-stepper.component.ts** | ||
|
||
```ts | ||
@Component({ | ||
selector: "app-custom-stepper", | ||
templateUrl: "./custom-stepper.component.html", | ||
styleUrls: ["./custom-stepper.component.css"], | ||
// This custom stepper provides itself as CdkStepper so that it can be recognized | ||
// by other components. | ||
providers: [{ provide: CdkStepper, useExisting: CustomStepperComponent }] | ||
}) | ||
export class CustomStepperComponent extends CdkStepper { | ||
/** Whether the validity of previous steps should be checked or not */ | ||
linear: boolean; | ||
|
||
/** The index of the selected step. */ | ||
selectedIndex: number; | ||
|
||
/** The list of step components that the stepper is holding. */ | ||
steps: QueryList<CdkStep>; | ||
|
||
onClick(index: number): void { | ||
this.selectedIndex = index; | ||
} | ||
} | ||
``` | ||
|
||
After we've extended our component class from `CdkStepper` we can now access different properties from this class like `linear`, `selectedIndex` and `steps` which are defined in the [API documentation](https://material.angular.io/cdk/stepper/api#CdkStepper). | ||
|
||
This is the HTML template of our custom stepper component: | ||
|
||
**custom-stepper.component.html** | ||
|
||
```html | ||
<section class="container"> | ||
<header><h2>Step {{selectedIndex + 1}}/{{steps.length}}</h2></header> | ||
|
||
<section *ngFor="let step of steps; let i = index;"> | ||
<div [style.display]="selectedIndex === i ? 'block' : 'none'"> | ||
<!-- Content from the CdkStep is projected here --> | ||
<ng-container [ngTemplateOutlet]="step.content"></ng-container> | ||
</div> | ||
</section> | ||
|
||
<footer class="step-navigation-bar"> | ||
<button class="nav-button" cdkStepperPrevious>←</button> | ||
<button | ||
class="step" | ||
*ngFor="let step of steps; let i = index;" | ||
[ngClass]="{'active': selectedIndex === i}" | ||
(click)="onClick(i)" | ||
> | ||
Step {{i + 1}} | ||
</button> | ||
<button class="nav-button" cdkStepperNext>→</button> | ||
</footer> | ||
</section> | ||
``` | ||
|
||
In the `app.component.css` file we can now style the stepper however we want: | ||
|
||
**custom-stepper.component.css** | ||
|
||
```css | ||
.example-container { | ||
border: 1px solid black; | ||
padding: 10px; | ||
margin: 10px; | ||
} | ||
|
||
.example-step-navigation-bar { | ||
display: flex; | ||
justify-content: flex-start; | ||
margin-top: 10px; | ||
} | ||
|
||
.example-active { | ||
color: blue; | ||
} | ||
|
||
.example-step { | ||
background: transparent; | ||
border: 0; | ||
margin: 0 10px; | ||
padding: 10px; | ||
color: black; | ||
} | ||
|
||
.example-step.example-active { | ||
color: blue; | ||
border-bottom: 1px solid blue; | ||
} | ||
|
||
.example-nav-button { | ||
background: transparent; | ||
border: 0; | ||
} | ||
``` | ||
|
||
## Using our new custom stepper component | ||
|
||
Now we are ready to use our new custom stepper component and fill it with steps. Therefore we can, for example, add it to our `app.component.html` and define some steps: | ||
|
||
**app.component.html** | ||
|
||
```html | ||
<app-custom-stepper> | ||
<cdk-step><p>This is any content of "Step 1"</p></cdk-step> | ||
<cdk-step><p>This is any content of "Step 2"</p></cdk-step> | ||
</app-custom-stepper> | ||
``` | ||
|
||
As you can see in this example, each step needs to be wrapped inside a `<cdk-step>` tag. | ||
|
||
If you want to iterate over your steps and use your own custom component you can do it, for example, this way: | ||
|
||
```html | ||
<app-custom-stepper> | ||
<cdk-step *ngFor="let step of mySteps; let stepIndex = index"> | ||
<my-step-component [step]="step"></my-step-component> | ||
</cdk-step> | ||
</app-custom-stepper> | ||
``` | ||
|
||
## Linear mode | ||
|
||
The above example allows the user to freely navigate between all steps. The `CdkStepper` additionally provides the linear mode which requires the user to complete previous steps before proceeding. | ||
|
||
A simple example without using forms could look this way: | ||
|
||
**app.component.html** | ||
|
||
```html | ||
<app-custom-stepper linear> | ||
<cdk-step editable="false" [completed]="completed"> | ||
<input type="text" name="a" value="Cannot proceed to next step" /> | ||
<button (click)="completeStep()">Complete step</button> | ||
</cdk-step> | ||
<cdk-step editable="false"> | ||
<input type="text" name="b" value="b" /> | ||
</cdk-step> | ||
</app-custom-stepper> | ||
``` | ||
|
||
**app.component.ts** | ||
|
||
```ts | ||
export class AppComponent { | ||
completed = false; | ||
|
||
completeStep(): void { | ||
this.completed = true; | ||
} | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.