From 7fac91554ef3713f72614dfe1bc36759a67edcdf Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 4 Dec 2018 00:33:05 +0100 Subject: [PATCH] fix(stepper): ignoring custom falsy value for hasError (#14337) The `hasError` input allows people to override the error state of a step, however we currently ignore falsy values because of an incorrect ||. These changes also fix one of the test fixtures where an error was being swallowed silently. Fixes #14333. --- src/cdk/stepper/stepper.ts | 2 +- src/lib/stepper/stepper.spec.ts | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/cdk/stepper/stepper.ts b/src/cdk/stepper/stepper.ts index 17923e62f5e..929a8fe83ec 100644 --- a/src/cdk/stepper/stepper.ts +++ b/src/cdk/stepper/stepper.ts @@ -181,7 +181,7 @@ export class CdkStep implements OnChanges { /** Whether step has an error. */ @Input() get hasError(): boolean { - return this._customError || this._getDefaultError(); + return this._customError == null ? this._getDefaultError() : this._customError; } set hasError(value: boolean) { this._customError = coerceBooleanProperty(value); diff --git a/src/lib/stepper/stepper.spec.ts b/src/lib/stepper/stepper.spec.ts index 9410a0bd8c8..16332fdcf9f 100644 --- a/src/lib/stepper/stepper.spec.ts +++ b/src/lib/stepper/stepper.spec.ts @@ -929,7 +929,7 @@ describe('MatStepper', () => { }); it('should show error state', () => { - let nextButtonNativeEl = fixture.debugElement + const nextButtonNativeEl = fixture.debugElement .queryAll(By.directive(MatStepperNext))[0].nativeElement; stepper.selectedIndex = 1; @@ -939,6 +939,23 @@ describe('MatStepper', () => { expect(stepper._getIndicatorType(0)).toBe(STEP_STATE.ERROR); }); + + it('should respect a custom falsy hasError value', () => { + const nextButtonNativeEl = fixture.debugElement + .queryAll(By.directive(MatStepperNext))[0].nativeElement; + + stepper.selectedIndex = 1; + nextButtonNativeEl.click(); + fixture.detectChanges(); + + expect(stepper._getIndicatorType(0)).toBe(STEP_STATE.ERROR); + + stepper._steps.first.hasError = false; + fixture.detectChanges(); + + expect(stepper._getIndicatorType(0)).not.toBe(STEP_STATE.ERROR); + }); + }); describe('stepper using Material UI Guideline logic', () => { @@ -1138,7 +1155,8 @@ function createComponent(component: Type, template: `
- + Step 1 First name