Skip to content

Commit 5df8d01

Browse files
crisbetommalerba
authored andcommitted
fix(stepper): not resetting to first step when some of the steps aren't editable (#10804)
Fixes the stepper not going back to the first step when one or more of its steps aren't editable. Fixes #10801.
1 parent 358a12d commit 5df8d01

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/cdk/stepper/stepper.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ export class CdkStepper implements AfterViewInit, OnDestroy {
179179
if (this._selectedIndex != index &&
180180
!this._anyControlsInvalidOrPending(index) &&
181181
(index >= this._selectedIndex || this._steps.toArray()[index].editable)) {
182-
183-
this._emitStepperSelectionEvent(index);
184-
this._keyManager.updateActiveItemIndex(this._selectedIndex);
182+
this._updateSelectedItemIndex(index);
185183
}
186184
} else {
187185
this._selectedIndex = index;
@@ -237,7 +235,7 @@ export class CdkStepper implements AfterViewInit, OnDestroy {
237235

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

286-
private _emitStepperSelectionEvent(newIndex: number): void {
284+
private _updateSelectedItemIndex(newIndex: number): void {
287285
const stepsArray = this._steps.toArray();
288286
this.selectionChange.emit({
289287
selectedIndex: newIndex,
290288
previouslySelectedIndex: this._selectedIndex,
291289
selectedStep: stepsArray[newIndex],
292290
previouslySelectedStep: stepsArray[this._selectedIndex],
293291
});
292+
this._keyManager.updateActiveItemIndex(newIndex);
294293
this._selectedIndex = newIndex;
295294
this._stateChanged();
296295
}

src/lib/stepper/stepper.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,25 @@ describe('MatStepper', () => {
588588
expect(testComponent.twoGroup.get('twoCtrl')!.valid).toBe(false);
589589
});
590590

591+
it('should reset back to the first step when some of the steps are not editable', () => {
592+
const steps = stepperComponent._steps.toArray();
593+
594+
steps[0].editable = false;
595+
596+
testComponent.oneGroup.get('oneCtrl')!.setValue('value');
597+
fixture.detectChanges();
598+
599+
stepperComponent.next();
600+
fixture.detectChanges();
601+
602+
expect(stepperComponent.selectedIndex).toBe(1);
603+
604+
stepperComponent.reset();
605+
fixture.detectChanges();
606+
607+
expect(stepperComponent.selectedIndex).toBe(0);
608+
});
609+
591610
it('should not clobber the `complete` binding when resetting', () => {
592611
const steps: MatStep[] = stepperComponent._steps.toArray();
593612
const fillOutStepper = () => {

0 commit comments

Comments
 (0)