Skip to content

fix(stepper): not resetting to first step when some of the steps aren't editable #10804

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 1 commit into from
Apr 16, 2018
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
9 changes: 4 additions & 5 deletions src/cdk/stepper/stepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ export class CdkStepper implements AfterViewInit, OnDestroy {
if (this._selectedIndex != index &&
!this._anyControlsInvalidOrPending(index) &&
(index >= this._selectedIndex || this._steps.toArray()[index].editable)) {

this._emitStepperSelectionEvent(index);
this._keyManager.updateActiveItemIndex(this._selectedIndex);
this._updateSelectedItemIndex(index);
}
} else {
this._selectedIndex = index;
Expand Down Expand Up @@ -237,7 +235,7 @@ export class CdkStepper implements AfterViewInit, OnDestroy {

/** Resets the stepper to its initial state. Note that this includes clearing form data. */
reset(): void {
this.selectedIndex = 0;
this._updateSelectedItemIndex(0);
this._steps.forEach(step => step.reset());
this._stateChanged();
}
Expand Down Expand Up @@ -283,14 +281,15 @@ export class CdkStepper implements AfterViewInit, OnDestroy {
return this._keyManager ? this._keyManager.activeItemIndex : this._selectedIndex;
}

private _emitStepperSelectionEvent(newIndex: number): void {
private _updateSelectedItemIndex(newIndex: number): void {
const stepsArray = this._steps.toArray();
this.selectionChange.emit({
selectedIndex: newIndex,
previouslySelectedIndex: this._selectedIndex,
selectedStep: stepsArray[newIndex],
previouslySelectedStep: stepsArray[this._selectedIndex],
});
this._keyManager.updateActiveItemIndex(newIndex);
this._selectedIndex = newIndex;
this._stateChanged();
}
Expand Down
19 changes: 19 additions & 0 deletions src/lib/stepper/stepper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,25 @@ describe('MatStepper', () => {
expect(testComponent.twoGroup.get('twoCtrl')!.valid).toBe(false);
});

it('should reset back to the first step when some of the steps are not editable', () => {
const steps = stepperComponent._steps.toArray();

steps[0].editable = false;

testComponent.oneGroup.get('oneCtrl')!.setValue('value');
fixture.detectChanges();

stepperComponent.next();
fixture.detectChanges();

expect(stepperComponent.selectedIndex).toBe(1);

stepperComponent.reset();
fixture.detectChanges();

expect(stepperComponent.selectedIndex).toBe(0);
});

it('should not clobber the `complete` binding when resetting', () => {
const steps: MatStep[] = stepperComponent._steps.toArray();
const fillOutStepper = () => {
Expand Down