Skip to content

Commit b64c08d

Browse files
s2-abdommalerba
authored andcommittedFeb 4, 2019
fix(cdk-stepper): coercing selectedIndex value to a Number (#14817)
* fix(cdk-stepper): coercing selectedIndex value to a Number * test(cdk-stepper): preselected value should be number
1 parent 2782273 commit b64c08d

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed
 

‎src/cdk/stepper/stepper.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {FocusableOption, FocusKeyManager} from '@angular/cdk/a11y';
1010
import {Direction, Directionality} from '@angular/cdk/bidi';
11-
import {coerceBooleanProperty} from '@angular/cdk/coercion';
11+
import {coerceBooleanProperty, coerceNumberProperty} from '@angular/cdk/coercion';
1212
import {END, ENTER, HOME, SPACE, hasModifierKey} from '@angular/cdk/keycodes';
1313
import {
1414
AfterViewInit,
@@ -276,19 +276,21 @@ export class CdkStepper implements AfterViewInit, OnDestroy {
276276
@Input()
277277
get selectedIndex() { return this._selectedIndex; }
278278
set selectedIndex(index: number) {
279+
const newIndex = coerceNumberProperty(index);
280+
279281
if (this.steps) {
280282
// Ensure that the index can't be out of bounds.
281-
if (index < 0 || index > this.steps.length - 1) {
283+
if (newIndex < 0 || newIndex > this.steps.length - 1) {
282284
throw Error('cdkStepper: Cannot assign out-of-bounds value to `selectedIndex`.');
283285
}
284286

285-
if (this._selectedIndex != index &&
286-
!this._anyControlsInvalidOrPending(index) &&
287-
(index >= this._selectedIndex || this.steps.toArray()[index].editable)) {
287+
if (this._selectedIndex != newIndex &&
288+
!this._anyControlsInvalidOrPending(newIndex) &&
289+
(newIndex >= this._selectedIndex || this.steps.toArray()[newIndex].editable)) {
288290
this._updateSelectedItemIndex(index);
289291
}
290292
} else {
291-
this._selectedIndex = index;
293+
this._selectedIndex = newIndex;
292294
}
293295
}
294296
private _selectedIndex = 0;

‎src/lib/stepper/stepper.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -704,13 +704,26 @@ describe('MatStepper', () => {
704704

705705
describe('linear stepper with a pre-defined selectedIndex', () => {
706706
let preselectedFixture: ComponentFixture<SimplePreselectedMatHorizontalStepperApp>;
707+
let stepper: MatHorizontalStepper;
708+
707709
beforeEach(() => {
708710
preselectedFixture = createComponent(SimplePreselectedMatHorizontalStepperApp);
711+
preselectedFixture.detectChanges();
712+
stepper = preselectedFixture.debugElement
713+
.query(By.directive(MatHorizontalStepper)).componentInstance;
709714
});
710715

711716
it('should not throw', () => {
712717
expect(() => preselectedFixture.detectChanges()).not.toThrow();
713718
});
719+
720+
it('selectedIndex should be typeof number', () => {
721+
expect(typeof stepper.selectedIndex).toBe('number');
722+
});
723+
724+
it('value of selectedIndex should be the pre-defined value', () => {
725+
expect(stepper.selectedIndex).toBe(0);
726+
});
714727
});
715728

716729
describe('linear stepper with no `stepControl`', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.