Skip to content

fix(cdk-stepper): coercing selectedIndex value to a Number #14817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/cdk/stepper/stepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {FocusableOption, FocusKeyManager} from '@angular/cdk/a11y';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {coerceBooleanProperty, coerceNumberProperty} from '@angular/cdk/coercion';
import {END, ENTER, HOME, SPACE, hasModifierKey} from '@angular/cdk/keycodes';
import {
AfterViewInit,
Expand Down Expand Up @@ -276,19 +276,21 @@ export class CdkStepper implements AfterViewInit, OnDestroy {
@Input()
get selectedIndex() { return this._selectedIndex; }
set selectedIndex(index: number) {
const newIndex = coerceNumberProperty(index);

if (this.steps) {
// Ensure that the index can't be out of bounds.
if (index < 0 || index > this.steps.length - 1) {
if (newIndex < 0 || newIndex > this.steps.length - 1) {
throw Error('cdkStepper: Cannot assign out-of-bounds value to `selectedIndex`.');
}

if (this._selectedIndex != index &&
!this._anyControlsInvalidOrPending(index) &&
(index >= this._selectedIndex || this.steps.toArray()[index].editable)) {
if (this._selectedIndex != newIndex &&
!this._anyControlsInvalidOrPending(newIndex) &&
(newIndex >= this._selectedIndex || this.steps.toArray()[newIndex].editable)) {
this._updateSelectedItemIndex(index);
}
} else {
this._selectedIndex = index;
this._selectedIndex = newIndex;
}
}
private _selectedIndex = 0;
Expand Down
13 changes: 13 additions & 0 deletions src/lib/stepper/stepper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,13 +705,26 @@ describe('MatStepper', () => {

describe('linear stepper with a pre-defined selectedIndex', () => {
let preselectedFixture: ComponentFixture<SimplePreselectedMatHorizontalStepperApp>;
let stepper: MatHorizontalStepper;

beforeEach(() => {
preselectedFixture = createComponent(SimplePreselectedMatHorizontalStepperApp);
preselectedFixture.detectChanges();
stepper = preselectedFixture.debugElement
.query(By.directive(MatHorizontalStepper)).componentInstance;
});

it('should not throw', () => {
expect(() => preselectedFixture.detectChanges()).not.toThrow();
});

it('selectedIndex should be typeof number', () => {
expect(typeof stepper.selectedIndex).toBe('number');
});

it('value of selectedIndex should be the pre-defined value', () => {
expect(stepper.selectedIndex).toBe(0);
});
});

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