Skip to content

Commit 75d6d8a

Browse files
rjregenoldRJ Regenold
and
RJ Regenold
authored
Fixes two TypeError exceptions when up/down arrow pressed (#2585)
* fixes `Cannot read property 'position' of undefined` error. when using live search, if a user enters a value that does not return any results and then presses the up/down arrows, this error occurs. this commit fixes that error. * fixes `Cannot read property 'index' of undefined` error. when using live search, if a user enters a value that does not return any results and then presses the up/down arrows, this error occurs. this commit fixes that error. Co-authored-by: RJ Regenold <[email protected]>
1 parent 465676a commit 75d6d8a

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

js/bootstrap-select.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -2885,9 +2885,13 @@
28852885
liActiveIndex = that.selectpicker.current.elements.length - 1;
28862886
} else {
28872887
activeLi = that.selectpicker.current.data[liActiveIndex];
2888-
offset = activeLi.position - activeLi.height;
28892888

2890-
updateScroll = offset < scrollTop;
2889+
// could be undefined if no results exist
2890+
if (activeLi) {
2891+
offset = activeLi.position - activeLi.height;
2892+
2893+
updateScroll = offset < scrollTop;
2894+
}
28912895
}
28922896
} else if (e.which === keyCodes.ARROW_DOWN || downOnTab) { // down
28932897
// scroll to top and highlight first option
@@ -2897,15 +2901,19 @@
28972901
liActiveIndex = that.selectpicker.view.firstHighlightIndex;
28982902
} else {
28992903
activeLi = that.selectpicker.current.data[liActiveIndex];
2900-
offset = activeLi.position - that.sizeInfo.menuInnerHeight;
29012904

2902-
updateScroll = offset > scrollTop;
2905+
// could be undefined if no results exist
2906+
if (activeLi) {
2907+
offset = activeLi.position - that.sizeInfo.menuInnerHeight;
2908+
2909+
updateScroll = offset > scrollTop;
2910+
}
29032911
}
29042912
}
29052913

29062914
liActive = that.selectpicker.current.elements[liActiveIndex];
29072915

2908-
that.activeIndex = that.selectpicker.current.data[liActiveIndex].index;
2916+
that.activeIndex = (that.selectpicker.current.data[liActiveIndex] || {}).index;
29092917

29102918
that.focusItem(liActive);
29112919

0 commit comments

Comments
 (0)