From 6ce8b9f11a28c319d3fb19a5cc032f30386eb614 Mon Sep 17 00:00:00 2001 From: Ji Won Shin Date: Wed, 16 Aug 2017 11:50:00 -0700 Subject: [PATCH] feat(stepper): Add support for linear stepper #2 - each step as its own form. (#6117) * Add form control - consider each step as its own form group * Comment edits * Add 'valid' to MdStep for form validation * Add [stepControl] to each step based on merging * Changes based on review --- src/cdk/stepper/stepper.ts | 6 +- src/demo-app/stepper/stepper-demo.html | 95 +++++++++++++++++++------- src/demo-app/stepper/stepper-demo.ts | 14 +++- 3 files changed, 84 insertions(+), 31 deletions(-) diff --git a/src/cdk/stepper/stepper.ts b/src/cdk/stepper/stepper.ts index 807369e1f87d..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.valid) { - 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 f394b0de8a24..70dd6ab6b81b 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 a single form

@@ -42,19 +43,63 @@

Linear Vertical Stepper Demo

-

Vertical Stepper Demo

+

Linear Horizontal Stepper Demo using a different form for each step

+ + +
+ 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

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

Vertical Stepper Demo

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

Vertical Stepper Demo

Fill out your address
- + This field is required - +
@@ -97,19 +142,19 @@

Vertical Stepper Demo

-

Horizontal Stepper Demo

+

Horizontal Stepper Demo

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

Horizontal Stepper Demo

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

Horizontal Stepper Demo

Fill out your address
- + This field is required - +
@@ -152,12 +197,12 @@

Horizontal Stepper Demo

-

Horizontal Stepper Demo

+

Horizontal Stepper Demo

- + - +
@@ -165,13 +210,13 @@

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 cc0bc6673215..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 {Validators, FormBuilder, FormGroup} from '@angular/forms'; +import {FormBuilder, 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'}, @@ -35,5 +38,14 @@ export class StepperDemo { }) ]) }); + + this.nameFormGroup = this._formBuilder.group({ + firstNameCtrl: ['', Validators.required], + lastNameCtrl: ['', Validators.required], + }); + + this.phoneFormGroup = this._formBuilder.group({ + phoneCtrl: ['', Validators.required] + }); } }