angular-wizard-form
Version:
<!-- [](https://badge.fury.io/js/angular2-wizard) --> <!-- [](https://travis-ci.org/maiyaporn/angular2-wizard) --
141 lines (120 loc) • 4.06 kB
text/typescript
import { Component, Output, EventEmitter, ContentChildren, QueryList, AfterContentInit, Input } from '@angular/core';
import { WizardStepComponent } from './wizard-step.component';
export class WizardComponent implements AfterContentInit {
wizardSteps: QueryList<WizardStepComponent>;
txtBtnPrevious = 'Previous';
txtBtnNext = 'Next';
txtBtnDone = 'Done';
private _steps: Array<WizardStepComponent> = [];
private _isCompleted = false;
onStepChanged: EventEmitter<WizardStepComponent> = new EventEmitter<WizardStepComponent>();
constructor() { }
ngAfterContentInit() {
this.wizardSteps.forEach(step => this._steps.push(step));
this.steps[0].isActive = true;
}
get steps(): Array<WizardStepComponent> {
return this._steps.filter(step => !step.hidden);
}
get isCompleted(): boolean {
return this._isCompleted;
}
get activeStep(): WizardStepComponent {
return this.steps.find(step => step.isActive);
}
set activeStep(step: WizardStepComponent) {
if (step !== this.activeStep && !step.isDisabled) {
this.activeStep.isActive = false;
step.isActive = true;
this.onStepChanged.emit(step);
}
}
public get activeStepIndex(): number {
return this.steps.indexOf(this.activeStep);
}
get hasNextStep(): boolean {
return this.activeStepIndex < this.steps.length - 1;
}
get hasPrevStep(): boolean {
return this.activeStepIndex > 0;
}
public goToStep(step: WizardStepComponent): void {
if (!this.isCompleted) {
this.activeStep = step;
}
}
public next(): void {
if (this.hasNextStep) {
let nextStep: WizardStepComponent = this.steps[this.activeStepIndex + 1];
this.activeStep.onNext.emit();
nextStep.isDisabled = false;
this.activeStep = nextStep;
}
}
public previous(): void {
if (this.hasPrevStep) {
let prevStep: WizardStepComponent = this.steps[this.activeStepIndex - 1];
this.activeStep.onPrev.emit();
prevStep.isDisabled = false;
this.activeStep = prevStep;
}
}
public complete(): void {
this.activeStep.onComplete.emit();
this._isCompleted = true;
}
}