Skip to content

feat(stepper): add proper type to stepper buttons #9401

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/cdk/stepper/stepper-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,35 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Directive} from '@angular/core';
import {Directive, Input} from '@angular/core';
import {CdkStepper} from './stepper';

/** Button that moves to the next step in a stepper workflow. */
@Directive({
selector: 'button[cdkStepperNext]',
host: {'(click)': '_stepper.next()'}
host: {
'(click)': '_stepper.next()',
'[type]': 'type',
}
})
export class CdkStepperNext {
constructor(public _stepper: CdkStepper) { }
/** Type of the next button. Defaults to "submit" if not specified. */
@Input() type: string = 'submit';

constructor(public _stepper: CdkStepper) {}
}

/** Button that moves to the previous step in a stepper workflow. */
@Directive({
selector: 'button[cdkStepperPrevious]',
host: {'(click)': '_stepper.previous()'}
host: {
'(click)': '_stepper.previous()',
'[type]': 'type',
}
})
export class CdkStepperPrevious {
constructor(public _stepper: CdkStepper) { }
/** Type of the previous button. Defaults to "button" if not specified. */
@Input() type: string = 'button';

constructor(public _stepper: CdkStepper) {}
}
26 changes: 13 additions & 13 deletions src/demo-app/stepper/stepper-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h3>Linear Vertical Stepper Demo using a single form</h3>
<mat-error>This field is required</mat-error>
</mat-form-field>
<div>
<button mat-button matStepperNext type="button">Next</button>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>

Expand All @@ -28,8 +28,8 @@ <h3>Linear Vertical Stepper Demo using a single form</h3>
<mat-error>The input is invalid.</mat-error>
</mat-form-field>
<div>
<button mat-button matStepperPrevious type="button">Back</button>
<button mat-button matStepperNext type="button">Next</button>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>

Expand Down Expand Up @@ -100,7 +100,7 @@ <h3>Vertical Stepper Demo</h3>
<input matInput placeholder="Last Name">
</mat-form-field>
<div>
<button mat-button matStepperNext type="button">Next</button>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>

Expand All @@ -112,8 +112,8 @@ <h3>Vertical Stepper Demo</h3>
<input matInput placeholder="Phone number">
</mat-form-field>
<div>
<button mat-button matStepperPrevious type="button">Back</button>
<button mat-button matStepperNext type="button">Next</button>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>

Expand All @@ -125,8 +125,8 @@ <h3>Vertical Stepper Demo</h3>
<input matInput placeholder="Address">
</mat-form-field>
<div>
<button mat-button matStepperPrevious type="button">Back</button>
<button mat-button matStepperNext type="button">Next</button>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>

Expand All @@ -150,7 +150,7 @@ <h3>Horizontal Stepper Demo with Text Label</h3>
<input matInput placeholder="Last Name">
</mat-form-field>
<div>
<button mat-button matStepperNext type="button">Next</button>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>

Expand All @@ -159,8 +159,8 @@ <h3>Horizontal Stepper Demo with Text Label</h3>
<input matInput placeholder="Phone number">
</mat-form-field>
<div>
<button mat-button matStepperPrevious type="button">Back</button>
<button mat-button matStepperNext type="button">Next</button>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>

Expand All @@ -169,8 +169,8 @@ <h3>Horizontal Stepper Demo with Text Label</h3>
<input matInput placeholder="Address">
</mat-form-field>
<div>
<button mat-button matStepperPrevious type="button">Back</button>
<button mat-button matStepperNext type="button">Next</button>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>

Expand Down
16 changes: 12 additions & 4 deletions src/lib/stepper/stepper-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@ import {MatStepper} from './stepper';
/** Button that moves to the next step in a stepper workflow. */
@Directive({
selector: 'button[matStepperNext]',
host: {'(click)': '_stepper.next()'},
host: {
'(click)': '_stepper.next()',
'[type]': 'type',
},
inputs: ['type'],
providers: [{provide: CdkStepper, useExisting: MatStepper}]
})
export class MatStepperNext extends CdkStepperNext { }
export class MatStepperNext extends CdkStepperNext {}

/** Button that moves to the previous step in a stepper workflow. */
@Directive({
selector: 'button[matStepperPrevious]',
host: {'(click)': '_stepper.previous()'},
host: {
'(click)': '_stepper.previous()',
'[type]': 'type',
},
inputs: ['type'],
providers: [{provide: CdkStepper, useExisting: MatStepper}]
})
export class MatStepperPrevious extends CdkStepperPrevious { }
export class MatStepperPrevious extends CdkStepperPrevious {}
23 changes: 23 additions & 0 deletions src/lib/stepper/stepper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,18 @@ describe('MatHorizontalStepper', () => {
assertNextStepperButtonClick(fixture);
});

it('should set the next stepper button type to "submit"', () => {
assertStepperButtonType(fixture, MatStepperNext, 'submit');
});

it('should go to previous available step when the previous button is clicked', () => {
assertPreviousStepperButtonClick(fixture);
});

it('should set the previous stepper button type to "button"', () => {
assertStepperButtonType(fixture, MatStepperPrevious, 'button');
});

it('should set the correct step position for animation', () => {
assertCorrectStepAnimationDirection(fixture);
});
Expand Down Expand Up @@ -314,10 +322,18 @@ describe('MatVerticalStepper', () => {
assertNextStepperButtonClick(fixture);
});

it('should set the next stepper button type to "submit"', () => {
assertStepperButtonType(fixture, MatStepperNext, 'submit');
});

it('should go to previous available step when the previous button is clicked', () => {
assertPreviousStepperButtonClick(fixture);
});

it('should set the previous stepper button type to "button"', () => {
assertStepperButtonType(fixture, MatStepperPrevious, 'button');
});

it('should set the correct step position for animation', () => {
assertCorrectStepAnimationDirection(fixture);
});
Expand Down Expand Up @@ -503,6 +519,13 @@ function assertNextStepperButtonClick(fixture: ComponentFixture<any>) {
expect(stepperComponent.selectedIndex).toBe(2);
}

/** Asserts that the specified type of stepper button has the given type. */
function assertStepperButtonType(fixture: ComponentFixture<any>, stepperClass: any, type: string) {
const buttonElement = fixture.debugElement.query(By.directive(stepperClass)).nativeElement;

expect(buttonElement.type).toBe(type, `Expected the button to have "${type}" set as type.`);
}

/** Asserts that clicking on MatStepperPrevious button updates `selectedIndex` correctly. */
function assertPreviousStepperButtonClick(fixture: ComponentFixture<any>) {
let stepperComponent = fixture.debugElement.query(By.directive(MatStepper)).componentInstance;
Expand Down