Skip to content
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

fix(stepper): error when selectedIndex is pre-set #8035

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
25 changes: 16 additions & 9 deletions src/cdk/stepper/stepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,17 @@ export class CdkStepper {
@Input()
get selectedIndex() { return this._selectedIndex; }
set selectedIndex(index: number) {
if (this._anyControlsInvalid(index)
|| index < this._selectedIndex && !this._steps.toArray()[index].editable) {
// remove focus from clicked step header if the step is not able to be selected
this._stepHeader.toArray()[index].nativeElement.blur();
} else if (this._selectedIndex != index) {
this._emitStepperSelectionEvent(index);
this._focusIndex = this._selectedIndex;
if (this._steps) {
if (this._anyControlsInvalid(index) || index < this._selectedIndex &&
!this._steps.toArray()[index].editable) {
// remove focus from clicked step header if the step is not able to be selected
this._stepHeader.toArray()[index].nativeElement.blur();
} else if (this._selectedIndex != index) {
this._emitStepperSelectionEvent(index);
this._focusIndex = this._selectedIndex;
}
} else {
this._selectedIndex = this._focusIndex = index;
}
}
private _selectedIndex: number = 0;
Expand Down Expand Up @@ -280,9 +284,12 @@ export class CdkStepper {
}

private _anyControlsInvalid(index: number): boolean {
this._steps.toArray()[this._selectedIndex].interacted = true;
const steps = this._steps.toArray();

steps[this._selectedIndex].interacted = true;

if (this._linear && index >= 0) {
return this._steps.toArray().slice(0, index).some(step => step.stepControl.invalid);
return steps.slice(0, index).some(step => step.stepControl && step.stepControl.invalid);
}
return false;
}
Expand Down
26 changes: 26 additions & 0 deletions src/lib/stepper/stepper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('MatHorizontalStepper', () => {
imports: [MatStepperModule, NoopAnimationsModule, ReactiveFormsModule],
declarations: [
SimpleMatHorizontalStepperApp,
SimplePreselectedMatHorizontalStepperApp,
LinearMatHorizontalStepperApp
],
providers: [
Expand Down Expand Up @@ -168,6 +169,18 @@ describe('MatHorizontalStepper', () => {
it('should be able to move to next step even when invalid if current step is optional', () => {
assertOptionalStepValidity(testComponent, fixture);
});

it('should not throw when there is a pre-defined selectedIndex', () => {
fixture.destroy();

let preselectedFixture = TestBed.createComponent(SimplePreselectedMatHorizontalStepperApp);
let debugElement = preselectedFixture.debugElement;

expect(() => preselectedFixture.detectChanges()).not.toThrow();

let stepHeaders = debugElement.queryAll(By.css('.mat-horizontal-stepper-header'));
assertSelectionChangeOnHeaderClick(preselectedFixture, stepHeaders);
});
});
});

Expand Down Expand Up @@ -874,3 +887,16 @@ class LinearMatVerticalStepperApp {
});
}
}

@Component({
template: `
<mat-horizontal-stepper [linear]="true" [selectedIndex]="index">
<mat-step label="One"></mat-step>
<mat-step label="Two"></mat-step>
<mat-step label="Three"></mat-step>
</mat-horizontal-stepper>
`
})
class SimplePreselectedMatHorizontalStepperApp {
index = 0;
}