From 4db843d5269502c2c8efa0555b8dcbee1833d51e Mon Sep 17 00:00:00 2001 From: Ji Won Shin Date: Fri, 28 Jul 2017 09:57:45 -0700 Subject: [PATCH 1/5] Add form control - consider each step as its own form group --- src/demo-app/stepper/stepper-demo.html | 46 ++++++++++++++++++++++++++ src/demo-app/stepper/stepper-demo.ts | 21 ++++++++++-- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/demo-app/stepper/stepper-demo.html b/src/demo-app/stepper/stepper-demo.html index f394b0de8a24..11d9242718f4 100644 --- a/src/demo-app/stepper/stepper-demo.html +++ b/src/demo-app/stepper/stepper-demo.html @@ -42,6 +42,52 @@

Linear Vertical Stepper Demo

+

Linear Vertical Stepper Demo

+ + +
+ Fill out your name + + + This field is required + + + + This field is required + +
+ +
+
+
+ + +
+ +
Fill out your phone number
+
+ + + This field is required + +
+ + +
+
+
+ + +
+ Confirm your information + Everything seems correct. +
+ +
+
+
+
+

Vertical Stepper Demo

diff --git a/src/demo-app/stepper/stepper-demo.ts b/src/demo-app/stepper/stepper-demo.ts index cc0bc6673215..25a48f075649 100644 --- a/src/demo-app/stepper/stepper-demo.ts +++ b/src/demo-app/stepper/stepper-demo.ts @@ -1,5 +1,5 @@ import {Component} from '@angular/core'; -import {Validators, FormBuilder, FormGroup} from '@angular/forms'; +import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms'; @Component({ moduleId: module.id, @@ -11,6 +11,9 @@ export class StepperDemo { formGroup: FormGroup; isNonLinear = false; + nameFormGroup: FormGroup; + phoneFormGroup: FormGroup; + steps = [ {label: 'Confirm your name', content: 'Last name, First name.'}, {label: 'Confirm your contact information', content: '123-456-7890'}, @@ -19,9 +22,12 @@ export class StepperDemo { ]; /** Returns a FormArray with the name 'formArray'. */ - get formArray() { return this.formGroup.get('formArray'); } + get formArray() { + return this.formGroup.get('formArray'); + } - constructor(private _formBuilder: FormBuilder) { } + constructor(private _formBuilder: FormBuilder) { + } ngOnInit() { this.formGroup = this._formBuilder.group({ @@ -35,5 +41,14 @@ export class StepperDemo { }) ]) }); + + this.nameFormGroup = new FormGroup({ + firstNameFormCtrl: new FormControl('', Validators.required), + lastNameFormCtrl: new FormControl('', Validators.required) + }); + + this.phoneFormGroup = new FormGroup({ + phoneFormCtrl: new FormControl('', Validators.required) + }); } } From c33e00e6ec84bad7917f3a491d2858b8118a3510 Mon Sep 17 00:00:00 2001 From: Ji Won Shin Date: Fri, 28 Jul 2017 16:01:51 -0700 Subject: [PATCH 2/5] Comment edits --- src/demo-app/stepper/stepper-demo.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/demo-app/stepper/stepper-demo.html b/src/demo-app/stepper/stepper-demo.html index 11d9242718f4..4990ffc84a65 100644 --- a/src/demo-app/stepper/stepper-demo.html +++ b/src/demo-app/stepper/stepper-demo.html @@ -45,7 +45,7 @@

Linear Vertical Stepper Demo

Linear Vertical Stepper Demo

-
+ Fill out your name @@ -62,7 +62,7 @@

Linear Vertical Stepper Demo

- +
Fill out your phone number
From 00bbb238fb67cc8fff2d46656fec3f1dfe351710 Mon Sep 17 00:00:00 2001 From: Ji Won Shin Date: Thu, 3 Aug 2017 11:25:30 -0700 Subject: [PATCH 3/5] Add 'valid' to MdStep for form validation --- src/demo-app/stepper/stepper-demo.html | 8 ++++---- src/demo-app/stepper/stepper-demo.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/demo-app/stepper/stepper-demo.html b/src/demo-app/stepper/stepper-demo.html index 4990ffc84a65..56f4add44b54 100644 --- a/src/demo-app/stepper/stepper-demo.html +++ b/src/demo-app/stepper/stepper-demo.html @@ -44,7 +44,7 @@

Linear Vertical Stepper Demo

Linear Vertical Stepper Demo

- + Fill out your name @@ -61,13 +61,13 @@

Linear Vertical Stepper Demo

- +
Fill out your phone number
- + This field is required
@@ -77,7 +77,7 @@

Linear Vertical Stepper Demo

- +
Confirm your information Everything seems correct. diff --git a/src/demo-app/stepper/stepper-demo.ts b/src/demo-app/stepper/stepper-demo.ts index 25a48f075649..b9d0aba261be 100644 --- a/src/demo-app/stepper/stepper-demo.ts +++ b/src/demo-app/stepper/stepper-demo.ts @@ -48,7 +48,7 @@ export class StepperDemo { }); this.phoneFormGroup = new FormGroup({ - phoneFormCtrl: new FormControl('', Validators.required) + phoneFormCtrl: new FormControl('') }); } } From 127789b07f46691ce2ae6b30310456585c3b2f5b Mon Sep 17 00:00:00 2001 From: Ji Won Shin Date: Tue, 8 Aug 2017 15:02:31 -0700 Subject: [PATCH 4/5] Add [stepControl] to each step based on merging --- src/cdk/stepper/stepper.ts | 2 +- src/demo-app/stepper/stepper-demo.html | 31 +++++++++++++------------- src/demo-app/stepper/stepper-demo.ts | 13 +++++------ 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/cdk/stepper/stepper.ts b/src/cdk/stepper/stepper.ts index 807369e1f87d..3e33a1bd5c51 100644 --- a/src/cdk/stepper/stepper.ts +++ b/src/cdk/stepper/stepper.ts @@ -209,7 +209,7 @@ export class CdkStepper { stepsArray[this._selectedIndex].interacted = true; if (this._linear) { for (let i = 0; i < index; i++) { - if (!stepsArray[i].stepControl.valid) { + if (stepsArray[i].stepControl.invalid) { return true; } } diff --git a/src/demo-app/stepper/stepper-demo.html b/src/demo-app/stepper/stepper-demo.html index 56f4add44b54..88c98fe76762 100644 --- a/src/demo-app/stepper/stepper-demo.html +++ b/src/demo-app/stepper/stepper-demo.html @@ -1,5 +1,6 @@ -

Linear Vertical Stepper Demo

Disable linear mode + +

Linear Vertical Stepper Demo using FormArray

@@ -42,17 +43,17 @@

Linear Vertical Stepper Demo

-

Linear Vertical Stepper Demo

- - +

Linear Horizontal Stepper Demo using multiple FormGroups

+ +
Fill out your name - + This field is required - + This field is required
@@ -61,13 +62,11 @@

Linear Vertical Stepper Demo

- +
- -
Fill out your phone number
-
+ Fill out your phone number - + This field is required
@@ -86,9 +85,9 @@

Linear Vertical Stepper Demo

- + -

Vertical Stepper Demo

+

Vertical Stepper Demo

Fill out your name @@ -143,7 +142,7 @@

Vertical Stepper Demo

-

Horizontal Stepper Demo

+

Horizontal Stepper Demo

Fill out your name @@ -198,7 +197,7 @@

Horizontal Stepper Demo

-

Horizontal Stepper Demo

+

Horizontal Stepper Demo

@@ -211,7 +210,7 @@

Horizontal Stepper Demo

-

Horizontal Stepper Demo with Templated Label

+

Horizontal Stepper Demo with Templated Label

{{step.label}} diff --git a/src/demo-app/stepper/stepper-demo.ts b/src/demo-app/stepper/stepper-demo.ts index b9d0aba261be..bd0327f69d50 100644 --- a/src/demo-app/stepper/stepper-demo.ts +++ b/src/demo-app/stepper/stepper-demo.ts @@ -22,12 +22,9 @@ export class StepperDemo { ]; /** Returns a FormArray with the name 'formArray'. */ - get formArray() { - return this.formGroup.get('formArray'); - } + get formArray() { return this.formGroup.get('formArray'); } - constructor(private _formBuilder: FormBuilder) { - } + constructor(private _formBuilder: FormBuilder) { } ngOnInit() { this.formGroup = this._formBuilder.group({ @@ -43,12 +40,12 @@ export class StepperDemo { }); this.nameFormGroup = new FormGroup({ - firstNameFormCtrl: new FormControl('', Validators.required), - lastNameFormCtrl: new FormControl('', Validators.required) + firstNameCtrl: new FormControl('', Validators.required), + lastNameCtrl: new FormControl('', Validators.required) }); this.phoneFormGroup = new FormGroup({ - phoneFormCtrl: new FormControl('') + phoneCtrl: new FormControl('') }); } } From 28f4ceb36f3c94d660384ec2a314a46d50c0cc90 Mon Sep 17 00:00:00 2001 From: Ji Won Shin Date: Tue, 15 Aug 2017 18:14:49 -0700 Subject: [PATCH 5/5] Changes based on review --- src/cdk/stepper/stepper.ts | 6 +-- src/demo-app/stepper/stepper-demo.html | 58 +++++++++++++------------- src/demo-app/stepper/stepper-demo.ts | 12 +++--- 3 files changed, 36 insertions(+), 40 deletions(-) diff --git a/src/cdk/stepper/stepper.ts b/src/cdk/stepper/stepper.ts index 3e33a1bd5c51..9bb879647d7e 100644 --- a/src/cdk/stepper/stepper.ts +++ b/src/cdk/stepper/stepper.ts @@ -208,11 +208,7 @@ export class CdkStepper { const stepsArray = this._steps.toArray(); stepsArray[this._selectedIndex].interacted = true; if (this._linear) { - for (let i = 0; i < index; i++) { - if (stepsArray[i].stepControl.invalid) { - return true; - } - } + return stepsArray.slice(0, index).some(step => step.stepControl.invalid); } return false; } diff --git a/src/demo-app/stepper/stepper-demo.html b/src/demo-app/stepper/stepper-demo.html index 88c98fe76762..70dd6ab6b81b 100644 --- a/src/demo-app/stepper/stepper-demo.html +++ b/src/demo-app/stepper/stepper-demo.html @@ -1,6 +1,6 @@ Disable linear mode -

Linear Vertical Stepper Demo using FormArray

+

Linear Vertical Stepper Demo using a single form

@@ -43,19 +43,19 @@

Linear Vertical Stepper Demo using FormArray

-

Linear Horizontal Stepper Demo using multiple FormGroups

+

Linear Horizontal Stepper Demo using a different form for each step

Fill out your name - + This field is required - - + + This field is required - +
@@ -65,10 +65,10 @@

Linear Horizontal Stepper Demo using multiple FormGroups

Fill out your phone number - - + + This field is required - +
@@ -91,15 +91,15 @@

Vertical Stepper Demo

Fill out your name - + This field is required - + - + This field is required - +
@@ -109,10 +109,10 @@

Vertical Stepper Demo

Fill out your phone number
- + This field is required - +
@@ -123,10 +123,10 @@

Vertical Stepper Demo

Fill out your address
- + This field is required - +
@@ -146,15 +146,15 @@

Horizontal Stepper Demo

Fill out your name - + This field is required - + - + This field is required - +
@@ -164,10 +164,10 @@

Horizontal Stepper Demo

Fill out your phone number
- + This field is required - +
@@ -178,10 +178,10 @@

Horizontal Stepper Demo

Fill out your address
- + This field is required - +
@@ -200,9 +200,9 @@

Horizontal Stepper Demo

Horizontal Stepper Demo

- + - +
@@ -214,9 +214,9 @@

Horizontal Stepper Demo with Templated Label

{{step.label}} - + - +
diff --git a/src/demo-app/stepper/stepper-demo.ts b/src/demo-app/stepper/stepper-demo.ts index bd0327f69d50..c4f546f0dfed 100644 --- a/src/demo-app/stepper/stepper-demo.ts +++ b/src/demo-app/stepper/stepper-demo.ts @@ -1,5 +1,5 @@ import {Component} from '@angular/core'; -import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms'; +import {FormBuilder, FormGroup, Validators} from '@angular/forms'; @Component({ moduleId: module.id, @@ -39,13 +39,13 @@ export class StepperDemo { ]) }); - this.nameFormGroup = new FormGroup({ - firstNameCtrl: new FormControl('', Validators.required), - lastNameCtrl: new FormControl('', Validators.required) + this.nameFormGroup = this._formBuilder.group({ + firstNameCtrl: ['', Validators.required], + lastNameCtrl: ['', Validators.required], }); - this.phoneFormGroup = new FormGroup({ - phoneCtrl: new FormControl('') + this.phoneFormGroup = this._formBuilder.group({ + phoneCtrl: ['', Validators.required] }); } }