Skip to content

Commit e0b0674

Browse files
committed
Implement @input() stepControl for each step
1 parent 5e57f17 commit e0b0674

File tree

4 files changed

+14
-28
lines changed

4 files changed

+14
-28
lines changed

src/cdk/stepper/stepper.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import {LEFT_ARROW, RIGHT_ARROW, ENTER, SPACE} from '@angular/cdk/keyboard';
2626
import {CdkStepLabel} from './step-label';
2727
import {coerceBooleanProperty} from '@angular/cdk/coercion';
28+
import {AbstractControl} from '@angular/forms';
2829

2930
/** Used to generate unique ID for each stepper component. */
3031
let nextId = 0;
@@ -55,14 +56,13 @@ export class CdkStep {
5556
/** Template for step content. */
5657
@ViewChild(TemplateRef) content: TemplateRef<any>;
5758

58-
// TODO(jwshin): use disabled mixin when moved to cdk.
59-
/** Whether step is disabled or not. */
59+
/** The top level abstract control of the step. */
6060
@Input()
61-
get disabled() { return this._disabled; }
62-
set disabled(value: any) {
63-
this._disabled = coerceBooleanProperty(value);
61+
get stepControl() { return this._stepControl; }
62+
set stepControl(control: AbstractControl) {
63+
this._stepControl = control;
6464
}
65-
private _disabled = false;
65+
private _stepControl: AbstractControl;
6666

6767
/** Whether user has seen the expanded step content or not . */
6868
interacted = false;
@@ -98,7 +98,10 @@ export class CdkStepper {
9898
get selectedIndex() { return this._selectedIndex; }
9999
set selectedIndex(index: number) {
100100
this._steps.toArray()[this._selectedIndex].interacted = true;
101-
if (this._selectedIndex != index && !this._steps.toArray()[index].disabled) {
101+
for (let i = 0; i < index; i++) {
102+
if (!this._steps.toArray()[i].stepControl.valid) { return; }
103+
}
104+
if (this._selectedIndex != index) {
102105
this._emitStepperSelectionEvent(index);
103106
this._focusStep(this._selectedIndex);
104107
}

src/demo-app/stepper/stepper-demo.html

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<h2>Linear Vertical Stepper Demo</h2>
22
<form [formGroup]="formGroup">
33
<md-vertical-stepper formArrayName="formArray">
4-
<md-step formGroupName="0">
4+
<md-step formGroupName="0" [stepControl]="formArray.get([0])">
55
<ng-template mdStepLabel>Fill out your name</ng-template>
66
<md-input-container>
77
<input mdInput placeholder="First Name" formControlName="firstNameFormCtrl" required>
@@ -17,9 +17,7 @@ <h2>Linear Vertical Stepper Demo</h2>
1717
</div>
1818
</md-step>
1919

20-
<md-step formGroupName="1"
21-
[disabled]="formArray.hasError('invalid step') ?
22-
formArray.getError('invalid step').index <= 1 : false">
20+
<md-step formGroupName="1" [stepControl]="formArray.get([1])">
2321
<ng-template mdStepLabel>
2422
<div>Fill out your phone number</div>
2523
</ng-template>
@@ -33,9 +31,7 @@ <h2>Linear Vertical Stepper Demo</h2>
3331
</div>
3432
</md-step>
3533

36-
<md-step
37-
[disabled]="formArray.hasError('invalid step') ?
38-
formArray.getError('invalid step').index <= 2 : false">
34+
<md-step>
3935
<ng-template mdStepLabel>Confirm your information</ng-template>
4036
Everything seems correct.
4137
<div>

src/demo-app/stepper/stepper-demo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class StepperDemo {
3333
this._formBuilder.group({
3434
phoneFormCtrl: [''],
3535
})
36-
], stepValidator)
36+
])
3737
});
3838
}
3939
}

src/lib/stepper/stepper.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,6 @@ import {
3333
ValidatorFn
3434
} from '@angular/forms';
3535

36-
/**
37-
* Form array validator to check if all form groups in form array are valid.
38-
* If not, it will return the index of the first invalid form group.
39-
*/
40-
export const stepValidator: ValidatorFn = (formArray: FormArray): ValidationErrors | null => {
41-
for (let i = 0; i < formArray.length; i++) {
42-
if (formArray.at(i).invalid) {
43-
return {'invalid step': {'index': i}};
44-
}
45-
}
46-
return null;
47-
};
48-
4936
/**
5037
* Form array validator to check if all form groups in form array are valid.
5138
* If not, it will return the index of the first invalid form group.

0 commit comments

Comments
 (0)