Skip to content

Commit

Permalink
fix: prevent infinite loop
Browse files Browse the repository at this point in the history
Closes #168
  • Loading branch information
Ernest committed May 19, 2021
1 parent f2f532d commit 69b3ff0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const usePointer = (options: Ref<Option[]>, highlightedOriginalIndex: Ref
if (isSomeSelectable.value === false) return
if (highlightedOriginalIndex.value === null) return
let tempOriginalIndex = highlightedOriginalIndex.value + 1
while (tempOriginalIndex !== highlightedOriginalIndex.value) {
let safeCount = 0
while (tempOriginalIndex !== highlightedOriginalIndex.value && safeCount++ < options.value.length) {
if (options.value.length <= tempOriginalIndex) tempOriginalIndex = 0
if (pointerSet(tempOriginalIndex)) break
++tempOriginalIndex
Expand All @@ -35,7 +36,8 @@ export const usePointer = (options: Ref<Option[]>, highlightedOriginalIndex: Ref
if (isSomeSelectable.value === false) return
if (highlightedOriginalIndex.value === null) return
let tempOriginalIndex = highlightedOriginalIndex.value - 1
while (tempOriginalIndex !== highlightedOriginalIndex.value) {
let safeCount = 0
while (tempOriginalIndex !== highlightedOriginalIndex.value && safeCount++ < options.value.length) {
if (tempOriginalIndex < 0) tempOriginalIndex = options.value.length - 1
if (pointerSet(tempOriginalIndex)) break
--tempOriginalIndex
Expand Down
8 changes: 6 additions & 2 deletions src/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -556,20 +556,24 @@ const VueSelect = {
if (!dropdown.value) return
const computedStyle = getComputedStyle(highlightedEl)
let safeCount
safeCount = 0
while (
highlightedEl.offsetTop +
parseFloat(computedStyle.height) +
parseFloat(computedStyle.paddingTop) +
parseFloat(computedStyle.paddingBottom) >
dropdown.value.$el.clientHeight + dropdown.value.$el.scrollTop
dropdown.value.$el.clientHeight + dropdown.value.$el.scrollTop &&
safeCount++ < optionsWithInfo.value.length
) {
dropdown.value.$el.scrollTop =
dropdown.value.$el.scrollTop +
parseFloat(computedStyle.height) +
parseFloat(computedStyle.paddingTop) +
parseFloat(computedStyle.paddingBottom)
}
while (highlightedEl.offsetTop < dropdown.value.$el.scrollTop) {
safeCount = 0
while (highlightedEl.offsetTop < dropdown.value.$el.scrollTop && safeCount++ < optionsWithInfo.value.length) {
dropdown.value.$el.scrollTop =
dropdown.value.$el.scrollTop -
parseFloat(computedStyle.height) -
Expand Down

0 comments on commit 69b3ff0

Please sign in to comment.