mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(stepper): error being thrown if selected step is accessed too early (#11186)
Fixes an error that is thrown if the selected step is accessed before `AfterViewInit`. Fixes #11158.
This commit is contained in:
committed by
Jeremy Elbourn
parent
9e9daf8428
commit
4638833d02
@@ -189,9 +189,12 @@ export class CdkStepper implements AfterViewInit, OnDestroy {
|
||||
|
||||
/** The step that is selected. */
|
||||
@Input()
|
||||
get selected(): CdkStep { return this._steps.toArray()[this.selectedIndex]; }
|
||||
get selected(): CdkStep {
|
||||
// @deletion-target 7.0.0 Change return type to `CdkStep | undefined`.
|
||||
return this._steps ? this._steps.toArray()[this.selectedIndex] : undefined!;
|
||||
}
|
||||
set selected(step: CdkStep) {
|
||||
this.selectedIndex = this._steps.toArray().indexOf(step);
|
||||
this.selectedIndex = this._steps ? this._steps.toArray().indexOf(step) : -1;
|
||||
}
|
||||
|
||||
/** Event emitted when the selected step has changed. */
|
||||
|
||||
@@ -340,6 +340,28 @@ describe('MatStepper', () => {
|
||||
selectionChangeSubscription.unsubscribe();
|
||||
animationDoneSubscription.unsubscribe();
|
||||
}));
|
||||
|
||||
it('should not throw when attempting to get the selected step too early', () => {
|
||||
fixture.destroy();
|
||||
fixture = TestBed.createComponent(SimpleMatVerticalStepperApp);
|
||||
|
||||
const stepperComponent: MatVerticalStepper = fixture.debugElement
|
||||
.query(By.css('mat-vertical-stepper')).componentInstance;
|
||||
|
||||
expect(() => stepperComponent.selected).not.toThrow();
|
||||
});
|
||||
|
||||
it('should not throw when attempting to set the selected step too early', () => {
|
||||
fixture.destroy();
|
||||
fixture = TestBed.createComponent(SimpleMatVerticalStepperApp);
|
||||
|
||||
const stepperComponent: MatVerticalStepper = fixture.debugElement
|
||||
.query(By.css('mat-vertical-stepper')).componentInstance;
|
||||
|
||||
expect(() => stepperComponent.selected = null!).not.toThrow();
|
||||
expect(stepperComponent.selectedIndex).toBe(-1);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('icon overrides', () => {
|
||||
|
||||
Reference in New Issue
Block a user