-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(stepper): Add support for linear stepper #6116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
74b24ce
5d78514
5e57f17
1acfef4
71cb69f
e648c0e
d8b53ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import { | |
} from '@angular/core'; | ||
import {LEFT_ARROW, RIGHT_ARROW, ENTER, SPACE} from '@angular/cdk/keyboard'; | ||
import {CdkStepLabel} from './step-label'; | ||
import {coerceBooleanProperty} from '@angular/cdk/coercion'; | ||
|
||
/** Used to generate unique ID for each stepper component. */ | ||
let nextId = 0; | ||
|
@@ -45,7 +46,7 @@ export class CdkStepperSelectionEvent { | |
|
||
@Component({ | ||
selector: 'cdk-step', | ||
templateUrl: 'step.html', | ||
templateUrl: 'step.html' | ||
}) | ||
export class CdkStep { | ||
/** Template for step label if it exists. */ | ||
|
@@ -54,6 +55,18 @@ export class CdkStep { | |
/** Template for step content. */ | ||
@ViewChild(TemplateRef) content: TemplateRef<any>; | ||
|
||
// TODO(jwshin): use disabled mixin when moved to cdk. | ||
/** Whether step is disabled or not. */ | ||
@Input() | ||
get disabled() { return this._disabled; } | ||
set disabled(value: any) { | ||
this._disabled = coerceBooleanProperty(value); | ||
} | ||
private _disabled = false; | ||
|
||
/** Whether user has seen the expanded step content or not . */ | ||
interacted = false; | ||
|
||
/** Label of the step. */ | ||
@Input() | ||
label: string; | ||
|
@@ -84,7 +97,8 @@ export class CdkStepper { | |
@Input() | ||
get selectedIndex() { return this._selectedIndex; } | ||
set selectedIndex(index: number) { | ||
if (this._selectedIndex != index) { | ||
this._steps.toArray()[this._selectedIndex].interacted = true; | ||
if (this._selectedIndex != index && !this._steps.toArray()[index].disabled) { | ||
this._emitStepperSelectionEvent(index); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we only want to emit a change event if the selected index changes due to user interaction, therefore this should be moved to one of the user event handlers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought this is commonly called by all user interactions and that's why we decided to delegate emitting a change event here. Is this not what you meant? |
||
this._focusStep(this._selectedIndex); | ||
} | ||
|
@@ -153,7 +167,7 @@ export class CdkStepper { | |
break; | ||
case SPACE: | ||
case ENTER: | ||
this._emitStepperSelectionEvent(this._focusIndex); | ||
this.selectedIndex = this._focusIndex; | ||
break; | ||
default: | ||
// Return to avoid calling preventDefault on keys that are not explicitly handled. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,8 @@ | |
"outDir": "../../../dist/packages/cdk", | ||
"baseUrl": ".", | ||
"paths": { | ||
"@angular/cdk/keyboard": ["../../../dist/packages/cdk/keyboard/public_api"] | ||
"@angular/cdk/keyboard": ["../../../dist/packages/cdk/keyboard/public_api"], | ||
"@angular/cdk/coercion": ["../../../dist/packages/cdk/coercion/public_api"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to delete the entire There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. possibly... depends where her stepper branch is synced to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
}, | ||
"files": [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,50 @@ | ||
<h2>Linear Vertical Stepper Demo</h2> | ||
<form [formGroup]="formGroup"> | ||
<md-vertical-stepper formArrayName="formArray"> | ||
<md-step formGroupName="0"> | ||
<ng-template mdStepLabel>Fill out your name</ng-template> | ||
<md-input-container> | ||
<input mdInput placeholder="First Name" formControlName="firstNameFormCtrl" required> | ||
<md-error>This field is required</md-error> | ||
</md-input-container> | ||
|
||
<md-input-container> | ||
<input mdInput placeholder="Last Name" formControlName="lastNameFormCtrl" required> | ||
<md-error>This field is required</md-error> | ||
</md-input-container> | ||
<div> | ||
<button md-button mdStepperNext type="button">Next</button> | ||
</div> | ||
</md-step> | ||
|
||
<md-step formGroupName="1" | ||
[disabled]="formArray.hasError('invalid step') ? | ||
formArray.getError('invalid step').index <= 1 : false"> | ||
<ng-template mdStepLabel> | ||
<div>Fill out your phone number</div> | ||
</ng-template> | ||
<md-input-container> | ||
<input mdInput placeholder="Phone number" formControlName="phoneFormCtrl"> | ||
<md-error>This field is required</md-error> | ||
</md-input-container> | ||
<div> | ||
<button md-button mdStepperPrevious type="button">Back</button> | ||
<button md-button mdStepperNext type="button">Next</button> | ||
</div> | ||
</md-step> | ||
|
||
<md-step | ||
[disabled]="formArray.hasError('invalid step') ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't really seem to have made the disabled logic much easier... is that what you had in mind @kara or is there a better way to organize the custom validator? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mmalerba wouldn't it be better to not direct;y use the previous step form validity on the disabled input and instead use a It's going to be more complex than just checking if the previous is valid because it's going to need to validate against disabled, editable, optional and completed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @joejordanbrown The changed code no longer only checks for the previous step's validity. Now it uses the custom step validation function @mmalerba : Waiting for @kara 's response on additional suggestions regarding this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @g1shin I see that, but I think it's too complex for the end user to have to add that to each step. It's going to get confusing for people when you use that with You should be using less than operator
Do you have the design document for the stepper? I don't see it on the wiki. |
||
formArray.getError('invalid step').index <= 2 : false"> | ||
<ng-template mdStepLabel>Confirm your information</ng-template> | ||
Everything seems correct. | ||
<div> | ||
<button md-button>Done</button> | ||
</div> | ||
</md-step> | ||
</md-vertical-stepper> | ||
</form> | ||
|
||
<h2>Horizontal Stepper Demo</h2> | ||
<md-horizontal-stepper> | ||
<md-step *ngFor="let step of steps" [label]="step.label"> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,26 +15,79 @@ import { | |
// considers such imports as unused (https://github.com/Microsoft/TypeScript/issues/14953) | ||
// tslint:disable-next-line:no-unused-variable | ||
ElementRef, | ||
Inject, | ||
Optional, | ||
QueryList, | ||
SkipSelf, | ||
ViewChildren | ||
}from '@angular/core'; | ||
import {MdStepLabel} from './step-label'; | ||
import { | ||
defaultErrorStateMatcher, | ||
ErrorOptions, | ||
MD_ERROR_GLOBAL_OPTIONS, | ||
ErrorStateMatcher | ||
} from '../core/error/error-options'; | ||
import { | ||
FormArray, FormControl, FormGroupDirective, NgForm, ValidationErrors, | ||
ValidatorFn | ||
} from '@angular/forms'; | ||
|
||
/** | ||
* Form array validator to check if all form groups in form array are valid. | ||
* If not, it will return the index of the first invalid form group. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment left over from a previous implementation? Doesn't seem to go with |
||
export const stepValidator: ValidatorFn = (formArray: FormArray): ValidationErrors | null => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should probably be called |
||
for (let i = 0; i < formArray.length; i++) { | ||
if (formArray.at(i).invalid) { | ||
return {'invalid step': {'index': i}}; | ||
} | ||
} | ||
return null; | ||
}; | ||
|
||
/** | ||
* Form array validator to check if all form groups in form array are valid. | ||
* If not, it will return the index of the first invalid form group. | ||
*/ | ||
|
||
@Component({ | ||
moduleId: module.id, | ||
selector: 'md-step, mat-step', | ||
templateUrl: 'step.html', | ||
providers: [{provide: MD_ERROR_GLOBAL_OPTIONS, useExisting: MdStep}] | ||
}) | ||
export class MdStep extends CdkStep { | ||
export class MdStep extends CdkStep implements ErrorOptions { | ||
/** Content for step label given by <ng-template matStepLabel> or <ng-template mdStepLabel>. */ | ||
@ContentChild(MdStepLabel) stepLabel: MdStepLabel; | ||
|
||
constructor(mdStepper: MdStepper) { | ||
/** Original ErrorStateMatcher that checks the validity of form control. */ | ||
private _originalErrorStateMatcher: ErrorStateMatcher; | ||
|
||
constructor(mdStepper: MdStepper, | ||
@Optional() @SkipSelf() @Inject(MD_ERROR_GLOBAL_OPTIONS) errorOptions: ErrorOptions) { | ||
super(mdStepper); | ||
this._originalErrorStateMatcher = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, #6147 should make this easier There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw the PR, but should this change be made after that PR is merged into master and 'stepper' branch has synced to upstream master? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, just an fyi |
||
errorOptions ? errorOptions.errorStateMatcher || defaultErrorStateMatcher | ||
: defaultErrorStateMatcher; | ||
} | ||
|
||
/** Custom error state matcher that additionally checks for validity of interacted form. */ | ||
errorStateMatcher = (control: FormControl, form: FormGroupDirective | NgForm) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The class should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it work if this is a prototype method rather than a property? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No because the |
||
let originalErrorState = this._originalErrorStateMatcher(control, form); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment that explains the background for why we are doing this custom error matcher stuff? (i.e., everything we talked about in the meeting) |
||
|
||
/** | ||
* Custom error state checks for the validity of form that is not submitted or touched | ||
* since user can trigger a form change by calling for another step without directly | ||
* interacting with the current form. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use inline-style ( |
||
let customErrorState = control.invalid && this.interacted; | ||
|
||
return originalErrorState || customErrorState; | ||
} | ||
} | ||
|
||
export class MdStepper extends CdkStepper { | ||
export class MdStepper extends CdkStepper implements ErrorOptions { | ||
/** The list of step headers of the steps in the stepper. */ | ||
@ViewChildren('stepHeader') _stepHeader: QueryList<ElementRef>; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a
TODO
to use the disabled mixin when it's moved to the cdk?