Skip to content

Commit 7fac915

Browse files
crisbetommalerba
authored andcommittedDec 3, 2018
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.
1 parent b584888 commit 7fac915

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed
 

‎src/cdk/stepper/stepper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export class CdkStep implements OnChanges {
181181
/** Whether step has an error. */
182182
@Input()
183183
get hasError(): boolean {
184-
return this._customError || this._getDefaultError();
184+
return this._customError == null ? this._getDefaultError() : this._customError;
185185
}
186186
set hasError(value: boolean) {
187187
this._customError = coerceBooleanProperty(value);

‎src/lib/stepper/stepper.spec.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ describe('MatStepper', () => {
929929
});
930930

931931
it('should show error state', () => {
932-
let nextButtonNativeEl = fixture.debugElement
932+
const nextButtonNativeEl = fixture.debugElement
933933
.queryAll(By.directive(MatStepperNext))[0].nativeElement;
934934

935935
stepper.selectedIndex = 1;
@@ -939,6 +939,23 @@ describe('MatStepper', () => {
939939

940940
expect(stepper._getIndicatorType(0)).toBe(STEP_STATE.ERROR);
941941
});
942+
943+
it('should respect a custom falsy hasError value', () => {
944+
const nextButtonNativeEl = fixture.debugElement
945+
.queryAll(By.directive(MatStepperNext))[0].nativeElement;
946+
947+
stepper.selectedIndex = 1;
948+
nextButtonNativeEl.click();
949+
fixture.detectChanges();
950+
951+
expect(stepper._getIndicatorType(0)).toBe(STEP_STATE.ERROR);
952+
953+
stepper._steps.first.hasError = false;
954+
fixture.detectChanges();
955+
956+
expect(stepper._getIndicatorType(0)).not.toBe(STEP_STATE.ERROR);
957+
});
958+
942959
});
943960

944961
describe('stepper using Material UI Guideline logic', () => {
@@ -1138,7 +1155,8 @@ function createComponent<T>(component: Type<T>,
11381155
template: `
11391156
<form [formGroup]="formGroup">
11401157
<mat-horizontal-stepper>
1141-
<mat-step errorMessage="This field is required" [stepControl]="formArray?.get([0])">
1158+
<mat-step errorMessage="This field is required"
1159+
[stepControl]="formGroup.get('firstNameCtrl')">
11421160
<ng-template matStepLabel>Step 1</ng-template>
11431161
<mat-form-field>
11441162
<mat-label>First name</mat-label>

0 commit comments

Comments
 (0)
Please sign in to comment.