Skip to content

Commit 7e352ce

Browse files
crisbetotinayuangao
authored andcommitted
fix(stepper): overriding default completed logic when resetting (#9650)
Something that came up while looking another issue. Currently the stepper supports two ways of determining whether a step is completed: the default one that checks against the passed-in `FormControl` and the custom one where the consumer can pass in a value through the `completed` input. Currently when the stepper is reset via the `reset` method, the step sets the `completed` property which means that for any subsequent runs, that step will always be considered as incomplete. These changes switch to resetting the `completed` input only if it was being used beforehand.
1 parent 0a1904d commit 7e352ce

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/cdk/stepper/stepper.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ export class CdkStep implements OnChanges {
126126
/** Resets the step to its initial state. Note that this includes resetting form data. */
127127
reset(): void {
128128
this.interacted = false;
129-
this.completed = false;
129+
130+
if (this._customCompleted != null) {
131+
this._customCompleted = false;
132+
}
130133

131134
if (this.stepControl) {
132135
this.stepControl.reset();

src/lib/stepper/stepper.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ describe('MatHorizontalStepper', () => {
318318
it('should be able to reset the stepper to its initial state', () => {
319319
assertLinearStepperResetable(fixture);
320320
});
321+
322+
it('should not clobber the `complete` binding when resetting', () => {
323+
assertLinearStepperResetComplete(fixture);
324+
});
321325
});
322326
});
323327

@@ -493,6 +497,10 @@ describe('MatVerticalStepper', () => {
493497
it('should be able to reset the stepper to its initial state', () => {
494498
assertLinearStepperResetable(fixture);
495499
});
500+
501+
it('should not clobber the `complete` binding when resetting', () => {
502+
assertLinearStepperResetComplete(fixture);
503+
});
496504
});
497505
});
498506

@@ -971,6 +979,38 @@ function assertLinearStepperResetable(
971979
}
972980

973981

982+
/** Asserts that the `complete` binding is being reset correctly. */
983+
function assertLinearStepperResetComplete(
984+
fixture: ComponentFixture<LinearMatHorizontalStepperApp|LinearMatVerticalStepperApp>) {
985+
986+
const testComponent = fixture.componentInstance;
987+
const stepperComponent = fixture.debugElement.query(By.directive(MatStepper)).componentInstance;
988+
const steps: MatStep[] = stepperComponent._steps.toArray();
989+
const fillOutStepper = () => {
990+
testComponent.oneGroup.get('oneCtrl')!.setValue('input');
991+
testComponent.twoGroup.get('twoCtrl')!.setValue('input');
992+
testComponent.threeGroup.get('threeCtrl')!.setValue('valid');
993+
testComponent.validationTrigger.next();
994+
stepperComponent.selectedIndex = 2;
995+
fixture.detectChanges();
996+
stepperComponent.selectedIndex = 3;
997+
fixture.detectChanges();
998+
};
999+
1000+
fillOutStepper();
1001+
1002+
expect(steps[2].completed)
1003+
.toBe(true, 'Expected third step to be considered complete after the first run through.');
1004+
1005+
stepperComponent.reset();
1006+
fixture.detectChanges();
1007+
fillOutStepper();
1008+
1009+
expect(steps[2].completed)
1010+
.toBe(true, 'Expected third step to be considered complete when doing a run after a reset.');
1011+
}
1012+
1013+
9741014
@Component({
9751015
template: `
9761016
<mat-horizontal-stepper>

0 commit comments

Comments
 (0)