UNPKG

angular2-wizard-angular-material

Version:

This is an Angular2 Form Wizard component. Just like any form wizard. You can define steps and control how each step works. You can enable/disable navigation button based on validity of the current step. Currently, the component only support basic functio

116 lines (99 loc) 3.83 kB
import { Component, Output, EventEmitter, ContentChildren, QueryList, AfterContentInit } from '@angular/core'; import { WizardStepComponent } from './wizard-step.component'; import { MdCard, MdCardActions, MdCardTitle, MdCardContent, MdButton } from '@angular/material'; @Component({ selector: 'form-wizard', template: `<md-card> <md-card-title> <ul class="nav nav-justified"> <li *ngFor="let step of steps" [ngClass]="{'active': step.isActive, 'enabled': !step.isDisabled, 'disabled': step.isDisabled, 'completed': isCompleted}"> <a (click)="goToStep(step)">{{step.title}}</a> </li> </ul> </md-card-title> <md-card-content> <ng-content></ng-content> </md-card-content> <md-card-actions [hidden]="isCompleted"> <button md-button (click)="previous()" [hidden]="!hasPrevStep || !activeStep.showPrev">Previous</button> <button md-button (click)="next()" [disabled]="!activeStep.isValid" [hidden]="!hasNextStep || !activeStep.showNext">Next</button> <button md-button (click)="complete()" [disabled]="!activeStep.isValid" [hidden]="hasNextStep">Done</button> </md-card-actions > </md-card>` , styles: [ '.card { height: 100%; }', '.card-header { background-color: #fff; padding: 0; font-size: 1.25rem; }', '.card-block { overflow-y: auto; }', '.card-footer { background-color: #fff; border-top: 0 none; }', '.nav-item { padding: 1rem 0rem; border-bottom: 0.5rem solid #ccc; }', '.active { font-weight: bold; color: black; border-bottom-color: #1976D2 !important; }', '.enabled { cursor: pointer; border-bottom-color: rgb(88, 162, 234); }', '.disabled { color: #ccc; }', '.completed { cursor: default; }' ] }) export class WizardComponent implements AfterContentInit { @ContentChildren(WizardStepComponent) wizardSteps: QueryList<WizardStepComponent>; private _steps: Array<WizardStepComponent> = []; private _isCompleted: boolean = false; @Output() 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; } }