Skip to content

Commit

Permalink
fix(currentIndex) disallow negative index > -1 (#192)
Browse files Browse the repository at this point in the history
* chore(*) release 2.1.1

* fix(index) don't go negative
  • Loading branch information
Darren Jennings authored May 29, 2020
1 parent e616d70 commit 51e5599
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
26 changes: 26 additions & 0 deletions __tests__/autosuggest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -882,4 +882,30 @@ describe("Autosuggest", () => {
expect(itemChangedEmpty[0]).toBeNull();
expect(itemChangedEmpty[1]).toBeNull();
});

it("current index resilient against many keyups #190", async () => {
const props = { ...defaultProps };
props.inputProps = { ...defaultProps.inputProps };

const wrapper = mount(Autosuggest, {
propsData: props,
});

const input = wrapper.find("input");
input.setValue("G");
input.trigger("keydown.down");
await wrapper.vm.$nextTick(() => {})
expect(wrapper.vm.currentIndex).toBe(0)
input.trigger("keydown.up");
expect(wrapper.vm.currentIndex).toBe(-1)

// Go into the upside down, but make sure to come back unscathed
await wrapper.vm.$nextTick(() => {})
input.trigger("keydown.up");
await wrapper.vm.$nextTick(() => {})
input.trigger("keydown.up");
await wrapper.vm.$nextTick(() => {})

expect(wrapper.vm.currentIndex).toBe(-1)
});
});
2 changes: 1 addition & 1 deletion docs/build/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/storybook/iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<body>
<div id="root"></div>
<div id="error-display"></div>
<script src="static/preview.3342a23524c89d0f2aa9.bundle.js"></script>
<script src="static/preview.a4c7ea3211409c3899df.bundle.js"></script>
</body>
</html>

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-autosuggest",
"version": "2.1.0",
"version": "2.1.1",
"description": "Vue autosuggest component.",
"engines": {
"node": "> 4",
Expand Down
6 changes: 4 additions & 2 deletions src/Autosuggest.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
import DefaultSection from "./parts/DefaultSection.js";
import { addClass, removeClass } from "./utils";
const INDEX_IS_FOCUSED_ON_INPUT = -1
const defaultSectionConfig = {
name: "default",
type: "default-section"
Expand Down Expand Up @@ -462,14 +464,14 @@ export default {
}
// Determine direction of arrow up/down and determine new currentIndex
const direction = keyCode === 40 ? 1 : -1;
const newIndex = (parseInt(this.currentIndex) || 0) + (wasClosed ? 0 : direction);
const newIndex = Math.max((parseInt(this.currentIndex) || 0) + (wasClosed ? 0 : direction), INDEX_IS_FOCUSED_ON_INPUT);
this.setCurrentIndex(newIndex, this.totalResults);
this.didSelectFromOptions = true;
if (this.totalResults > 0 && this.currentIndex >= 0) {
this.setChangeItem(this.getItemByIndex(this.currentIndex));
this.didSelectFromOptions = true;
} else if (this.currentIndex == -1) {
} else if (this.currentIndex === INDEX_IS_FOCUSED_ON_INPUT) {
this.setChangeItem(null)
this.internalValue = this.searchInputOriginal;
e.preventDefault();
Expand Down

0 comments on commit 51e5599

Please sign in to comment.