Skip to content

Commit

Permalink
🐛 fix: submit items from empty list correctly. Fixes #86
Browse files Browse the repository at this point in the history
  • Loading branch information
niketpathak committed Sep 26, 2024
1 parent bfc4a99 commit e228237
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
7 changes: 5 additions & 2 deletions demo/assets/js/testable.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,11 @@ const test4 = typeahead({
wrapper: 'typeahead-standalone typeahead-test-four',
},
highlight: true,
// autoSelect: true,
onSubmit: (e) => {
e.preventDefault();
document.querySelector('#input-4-submit-val').innerHTML = e.target.value;
},
templates: {
suggestion: (item, resultSet) => {
return `<span class="preview" data-resultset="${resultSet.hits[0].hash}" style="background-color:${item.hash}"></span>
Expand Down Expand Up @@ -982,5 +987,3 @@ const test19 = typeahead({
`,
},
});


3 changes: 3 additions & 0 deletions demo/dev.esm.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ <h4>Try searching for marvel/DC movies...</h4>
className: 'typeahead-example',
minLength: 1,
limit: 10,
onSubmit: (e, selectedSuggestion) => {
console.log(`Submitted value -> ${e.target.value}`);
},
templates: {
suggestion: (item) => {
const date = item.release_date
Expand Down
2 changes: 1 addition & 1 deletion demo/testable.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ <h3 class="subtitle">3B. Transform Remote</h3>
<section class="section-four top-section">
<form method="GET">
<h3 class="subtitle">4. Templates, Source: Local, Object Arr</h3>
<p class="details">Used Templates: header; footer; suggestion; group; notFound</p>
<p class="details">Used Templates: header; footer; suggestion; group; notFound <span id="input-4-submit-val"></span></p>
<div>
<label for="input-four">My Fav Color</label>
<input id="input-four" name="color-test-4" type="search" autocomplete="off" placeholder="Search...">
Expand Down
7 changes: 6 additions & 1 deletion src/typeahead-standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ const typeahead = <T extends Dictionary>(config: typeaheadConfig<T>): typeaheadR
return clear();
}

// clear selected items on deleting characters
if (ev.key === 'Backspace') {
selected = undefined;
}

if (resultSet.hits.length && (ev.key === 'ArrowUp' || ev.key === 'ArrowDown')) {
ev.key === 'ArrowDown' ? selectNext(ev) : selectPrev(ev);
update();
Expand All @@ -465,7 +470,7 @@ const typeahead = <T extends Dictionary>(config: typeaheadConfig<T>): typeaheadR
}

// if empty query, do nothing
if (!resultSet.query) return;
if (!resultSet.query && !input.value.length) return;

const useSelectedValue = (fallback = false) => {
if (!selected && fallback && resultSet.hits.length) {
Expand Down

0 comments on commit e228237

Please sign in to comment.