|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + ContentChildren, |
| 11 | + EventEmitter, |
| 12 | + Input, |
| 13 | + Output, |
| 14 | + QueryList, |
| 15 | + Directive, |
| 16 | + // This import is only used to define a generic type. The current TypeScript version incorrectly |
| 17 | + // considers such imports as unused (https://github.com/Microsoft/TypeScript/issues/14953) |
| 18 | + // tslint:disable-next-line:no-unused-variable |
| 19 | + ElementRef, |
| 20 | + Component, |
| 21 | + ContentChild, |
| 22 | + ViewChild, |
| 23 | + TemplateRef |
| 24 | +} from '@angular/core'; |
| 25 | +import {LEFT_ARROW, RIGHT_ARROW, ENTER, SPACE} from '../keyboard/keycodes'; |
| 26 | +import {CdkStepLabel} from './step-label'; |
| 27 | + |
| 28 | +/** Used to generate unique ID for each stepper component. */ |
| 29 | +let nextId = 0; |
| 30 | + |
| 31 | +/** Change event emitted on selection changes. */ |
| 32 | +export class CdkStepperSelectionEvent { |
| 33 | + /** Index of the step now selected. */ |
| 34 | + selectedIndex: number; |
| 35 | + |
| 36 | + /** Index of the step previously selected. */ |
| 37 | + previouslySelectedIndex: number; |
| 38 | + |
| 39 | + /** The step instance now selected. */ |
| 40 | + selectedStep: CdkStep; |
| 41 | + |
| 42 | + /** The step instance previously selected. */ |
| 43 | + previouslySelectedStep: CdkStep; |
| 44 | +} |
| 45 | + |
| 46 | +@Component({ |
| 47 | + selector: 'cdk-step', |
| 48 | + templateUrl: 'step.html', |
| 49 | +}) |
| 50 | +export class CdkStep { |
| 51 | + /** Template for step label if it exists. */ |
| 52 | + @ContentChild(CdkStepLabel) stepLabel: CdkStepLabel; |
| 53 | + |
| 54 | + /** Template for step content. */ |
| 55 | + @ViewChild(TemplateRef) content: TemplateRef<any>; |
| 56 | + |
| 57 | + /** Label of the step. */ |
| 58 | + @Input() |
| 59 | + label: string; |
| 60 | + |
| 61 | + constructor(private _stepper: CdkStepper) { } |
| 62 | + |
| 63 | + /** Selects this step component. */ |
| 64 | + select(): void { |
| 65 | + this._stepper.selected = this; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +@Directive({ |
| 70 | + selector: 'cdk-stepper', |
| 71 | + host: { |
| 72 | + '(focus)': '_focusStep()', |
| 73 | + '(keydown)': '_onKeydown($event)', |
| 74 | + }, |
| 75 | +}) |
| 76 | +export class CdkStepper { |
| 77 | + /** The list of step components that the stepper is holding. */ |
| 78 | + @ContentChildren(CdkStep) _steps: QueryList<CdkStep>; |
| 79 | + |
| 80 | + /** The list of step headers of the steps in the stepper. */ |
| 81 | + _stepHeader: QueryList<ElementRef>; |
| 82 | + |
| 83 | + /** The index of the selected step. */ |
| 84 | + @Input() |
| 85 | + get selectedIndex() { return this._selectedIndex; } |
| 86 | + set selectedIndex(index: number) { |
| 87 | + if (this._selectedIndex != index) { |
| 88 | + this._emitStepperSelectionEvent(index); |
| 89 | + this._focusStep(this._selectedIndex); |
| 90 | + } |
| 91 | + } |
| 92 | + private _selectedIndex: number = 0; |
| 93 | + |
| 94 | + /** The step that is selected. */ |
| 95 | + @Input() |
| 96 | + get selected() { return this._steps[this.selectedIndex]; } |
| 97 | + set selected(step: CdkStep) { |
| 98 | + let index = this._steps.toArray().indexOf(step); |
| 99 | + this.selectedIndex = index; |
| 100 | + } |
| 101 | + |
| 102 | + /** Event emitted when the selected step has changed. */ |
| 103 | + @Output() selectionChange = new EventEmitter<CdkStepperSelectionEvent>(); |
| 104 | + |
| 105 | + /** The index of the step that the focus can be set. */ |
| 106 | + _focusIndex: number = 0; |
| 107 | + |
| 108 | + /** Used to track unique ID for each stepper component. */ |
| 109 | + private _groupId: number; |
| 110 | + |
| 111 | + constructor() { |
| 112 | + this._groupId = nextId++; |
| 113 | + } |
| 114 | + |
| 115 | + /** Selects and focuses the next step in list. */ |
| 116 | + next(): void { |
| 117 | + this.selectedIndex = Math.min(this._selectedIndex + 1, this._steps.length - 1); |
| 118 | + } |
| 119 | + |
| 120 | + /** Selects and focuses the previous step in list. */ |
| 121 | + previous(): void { |
| 122 | + this.selectedIndex = Math.max(this._selectedIndex - 1, 0); |
| 123 | + } |
| 124 | + |
| 125 | + /** Returns a unique id for each step label element. */ |
| 126 | + _getStepLabelId(i: number): string { |
| 127 | + return `mat-step-label-${this._groupId}-${i}`; |
| 128 | + } |
| 129 | + |
| 130 | + /** Returns nique id for each step content element. */ |
| 131 | + _getStepContentId(i: number): string { |
| 132 | + return `mat-step-content-${this._groupId}-${i}`; |
| 133 | + } |
| 134 | + |
| 135 | + private _emitStepperSelectionEvent(newIndex: number): void { |
| 136 | + const stepsArray = this._steps.toArray(); |
| 137 | + this.selectionChange.emit({ |
| 138 | + selectedIndex: newIndex, |
| 139 | + previouslySelectedIndex: this._selectedIndex, |
| 140 | + selectedStep: stepsArray[newIndex], |
| 141 | + previouslySelectedStep: stepsArray[this._selectedIndex], |
| 142 | + }); |
| 143 | + this._selectedIndex = newIndex; |
| 144 | + } |
| 145 | + |
| 146 | + _onKeydown(event: KeyboardEvent) { |
| 147 | + switch (event.keyCode) { |
| 148 | + case RIGHT_ARROW: |
| 149 | + this._focusStep((this._focusIndex + 1) % this._steps.length); |
| 150 | + break; |
| 151 | + case LEFT_ARROW: |
| 152 | + this._focusStep((this._focusIndex + this._steps.length - 1) % this._steps.length); |
| 153 | + break; |
| 154 | + case SPACE: |
| 155 | + case ENTER: |
| 156 | + this._emitStepperSelectionEvent(this._focusIndex); |
| 157 | + break; |
| 158 | + default: |
| 159 | + // Return to avoid calling preventDefault on keys that are not explicitly handled. |
| 160 | + return; |
| 161 | + } |
| 162 | + event.preventDefault(); |
| 163 | + } |
| 164 | + |
| 165 | + private _focusStep(index: number) { |
| 166 | + this._focusIndex = index; |
| 167 | + this._stepHeader.toArray()[this._focusIndex].nativeElement.focus(); |
| 168 | + } |
| 169 | +} |
0 commit comments