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