File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -152,6 +152,11 @@ export class CdkStepper implements OnDestroy {
152
152
get selectedIndex ( ) { return this . _selectedIndex ; }
153
153
set selectedIndex ( index : number ) {
154
154
if ( this . _steps ) {
155
+ // Ensure that the index can't be out of bounds.
156
+ if ( index < 0 || index > this . _steps . length - 1 ) {
157
+ throw Error ( 'cdkStepper: Cannot assign out-of-bounds value to `selectedIndex`.' ) ;
158
+ }
159
+
155
160
if ( this . _anyControlsInvalidOrPending ( index ) || index < this . _selectedIndex &&
156
161
! this . _steps . toArray ( ) [ index ] . editable ) {
157
162
// remove focus from clicked step header if the step is not able to be selected
@@ -164,7 +169,7 @@ export class CdkStepper implements OnDestroy {
164
169
this . _selectedIndex = this . _focusIndex = index ;
165
170
}
166
171
}
167
- private _selectedIndex : number = 0 ;
172
+ private _selectedIndex = 0 ;
168
173
169
174
/** The step that is selected. */
170
175
@Input ( )
Original file line number Diff line number Diff line change @@ -51,6 +51,26 @@ describe('MatHorizontalStepper', () => {
51
51
expect ( stepperComponent . selectedIndex ) . toBe ( 0 ) ;
52
52
} ) ;
53
53
54
+ it ( 'should throw when a negative `selectedIndex` is assigned' , ( ) => {
55
+ const stepperComponent : MatHorizontalStepper = fixture . debugElement
56
+ . query ( By . css ( 'mat-horizontal-stepper' ) ) . componentInstance ;
57
+
58
+ expect ( ( ) => {
59
+ stepperComponent . selectedIndex = - 10 ;
60
+ fixture . detectChanges ( ) ;
61
+ } ) . toThrowError ( / C a n n o t a s s i g n o u t - o f - b o u n d s / ) ;
62
+ } ) ;
63
+
64
+ it ( 'should throw when an out-of-bounds `selectedIndex` is assigned' , ( ) => {
65
+ const stepperComponent : MatHorizontalStepper = fixture . debugElement
66
+ . query ( By . css ( 'mat-horizontal-stepper' ) ) . componentInstance ;
67
+
68
+ expect ( ( ) => {
69
+ stepperComponent . selectedIndex = 1337 ;
70
+ fixture . detectChanges ( ) ;
71
+ } ) . toThrowError ( / C a n n o t a s s i g n o u t - o f - b o u n d s / ) ;
72
+ } ) ;
73
+
54
74
it ( 'should change selected index on header click' , ( ) => {
55
75
let stepHeaders = fixture . debugElement . queryAll ( By . css ( '.mat-horizontal-stepper-header' ) ) ;
56
76
assertSelectionChangeOnHeaderClick ( fixture , stepHeaders ) ;
You can’t perform that action at this time.
0 commit comments