Created
September 11, 2021 09:39
-
-
Save FFKL/595fbe4c5dceb614866eff5fe9fab715 to your computer and use it in GitHub Desktop.
Stepper component
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
import { ChangeDetectionStrategy, Component, TemplateRef, ViewChild, ViewEncapsulation } from '@angular/core'; | |
@Component({ | |
selector: 'app-step', | |
template: '<ng-template><ng-content></ng-content></ng-template>', | |
encapsulation: ViewEncapsulation.None, | |
changeDetection: ChangeDetectionStrategy.OnPush | |
}) | |
export class StepComponent { | |
@ViewChild(TemplateRef, { static: true }) content!: TemplateRef<any>; | |
} |
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
import { ChangeDetectionStrategy, Component, ContentChildren, Input, QueryList, ViewEncapsulation } from '@angular/core'; | |
import { StepComponent } from './step.component'; | |
@Component({ | |
selector: 'app-stepper', | |
templateUrl: '<ng-container *ngIf="this.getCurrentStepComponent() as stepCmp" [ngTemplateOutlet]="stepCmp.content"></ng-container> | |
', | |
encapsulation: ViewEncapsulation.None, | |
changeDetection: ChangeDetectionStrategy.OnPush | |
}) | |
export class StepperComponent { | |
@ContentChildren(StepComponent) steps!: QueryList<StepComponent>; | |
@Input() currentStep = 1; | |
getCurrentStepComponent() { | |
return this.steps.find((_, idx) => idx === this.getCurrentStepIndex()); | |
} | |
private getCurrentStepIndex() { | |
return this.currentStep - 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment