Skip to content

Commit

Permalink
fix(state) clear current item on select (#91)
Browse files Browse the repository at this point in the history
* fix(state) make sure current item is passed to defaultSection
item so that highlighted item works.
  • Loading branch information
darrenjennings authored Jan 19, 2019
1 parent 45fd849 commit 41e9913
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions __tests__/__snapshots__/autosuggest.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ exports[`Autosuggest can render suggestions 1`] = `
data-suggestion-index="0"
data-section-name="default"
id="autosuggest__results_item-0"
class="autosuggest__results_item"
class="autosuggest__results_item-highlighted autosuggest__results_item"
>
clifford kits
</li>
Expand Down Expand Up @@ -702,7 +702,7 @@ exports[`Autosuggest is aria complete 1`] = `
data-suggestion-index="2"
data-section-name="default"
id="autosuggest__results_item-2"
class="autosuggest__results_item"
class="autosuggest__results_item-highlighted autosuggest__results_item"
>
phonics
</li>
Expand Down
7 changes: 6 additions & 1 deletion src/Autosuggest.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
v-for="(cs, key) in computedSections"
:ref="getSectionRef(key)"
:key="getSectionRef(key)"
:current-index="currentIndex"
:normalize-item-function="normalizeItem"
:render-suggestion="renderSuggestion"
:section="cs"
Expand Down Expand Up @@ -202,6 +203,9 @@ export default {
});
},
selected: () => {
// Determine which onSelected to fire. This can be either from inside
// a section's object, from the @selected event, or from the deprecated
// native onSelected prop (to be removed later)
if (
this.currentItem &&
this.sectionConfigs[this.currentItem.name] &&
Expand All @@ -219,6 +223,7 @@ export default {
// TODO: 2.0 deprecate old event listeners
this._onSelected(this.currentItem);
}
this.setChangeItem(null)
}
};
},
Expand Down Expand Up @@ -391,7 +396,7 @@ export default {
}
},
setChangeItem(item, overrideOriginalInput = false) {
if (this.currentIndex === null) {
if (this.currentIndex === null || !item) {
this.currentItem = null;
} else if (item) {
this.searchInput = this.getSuggestionValue(item);
Expand Down
2 changes: 1 addition & 1 deletion src/parts/DefaultSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const DefaultSection = {
name: "default-section",
props: {
section: { type: Object, required: true },
currentIndex: { type: Number, required: false, default: Infinity },
currentIndex: { type: [Number, String], required: false, default: Infinity },
updateCurrentIndex: { type: Function, required: true },
searchInput: { type: [String, Number], required: false, default: "" },
renderSuggestion: { type: Function, required: false },
Expand Down
2 changes: 1 addition & 1 deletion src/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ storiesOf("Vue-Autosuggest", module)
methods: {
onSelected(item) {
action("Selected")(item);
this.add(item.item);
item && item.item && this.add(item.item);
},
onClick() {
this.onInputChange("");
Expand Down

0 comments on commit 41e9913

Please sign in to comment.