Skip to content

Commit

Permalink
fix(material/stepper): last step not being marked as interacted (#17976)
Browse files Browse the repository at this point in the history
Fixes the last step in the stepper never being marked as `interacted`.
Also fixes some weird logic where we were changing the step state inside
`_anyControlsInvalidOrPending` which is a getter method.

Fixes #17974.

(cherry picked from commit cd3d0e9)
  • Loading branch information
crisbeto authored and annieyw committed Feb 9, 2021
1 parent e0782e6 commit c0e2195
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/cdk/stepper/stepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,14 @@ export class CdkStepper implements AfterContentInit, AfterViewInit, OnDestroy {
throw Error('cdkStepper: Cannot assign out-of-bounds value to `selectedIndex`.');
}

const selectedStep = this.selected;

if (selectedStep) {
// TODO: this should really be called something like `visited` instead. Just because
// the user has seen the step doesn't guarantee that they've interacted with it.
selectedStep.interacted = true;
}

if (this._selectedIndex !== newIndex && !this._anyControlsInvalidOrPending(newIndex) &&
(newIndex >= this._selectedIndex || this.steps.toArray()[newIndex].editable)) {
this._updateSelectedItemIndex(index);
Expand Down Expand Up @@ -500,12 +508,8 @@ export class CdkStepper implements AfterContentInit, AfterViewInit, OnDestroy {
}

private _anyControlsInvalidOrPending(index: number): boolean {
const steps = this.steps.toArray();

steps[this._selectedIndex].interacted = true;

if (this._linear && index >= 0) {
return steps.slice(0, index).some(step => {
return this.steps.toArray().slice(0, index).some(step => {
const control = step.stepControl;
const isIncomplete =
control ? (control.invalid || control.pending || !step.interacted) : !step.completed;
Expand Down
22 changes: 22 additions & 0 deletions src/material/stepper/stepper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,28 @@ describe('MatStepper', () => {
expect(headers[2].classList.contains('mat-primary')).toBe(true);
expect(headers[1].classList.contains('mat-accent')).toBe(true);
});

it('should be able to mark all steps as interacted', () => {
const fixture = createComponent(SimpleMatHorizontalStepperApp);
fixture.detectChanges();

const stepper: MatStepper =
fixture.debugElement.query(By.directive(MatStepper)).componentInstance;

expect(stepper.steps.map(step => step.interacted)).toEqual([false, false, false]);

stepper.next();
fixture.detectChanges();
expect(stepper.steps.map(step => step.interacted)).toEqual([true, false, false]);

stepper.next();
fixture.detectChanges();
expect(stepper.steps.map(step => step.interacted)).toEqual([true, true, false]);

stepper.next();
fixture.detectChanges();
expect(stepper.steps.map(step => step.interacted)).toEqual([true, true, true]);
});
});

describe('linear stepper with valid step', () => {
Expand Down

0 comments on commit c0e2195

Please sign in to comment.