-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace autocomplete with stimulus controller
- Loading branch information
1 parent
552816e
commit 43c851d
Showing
4 changed files
with
127 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
ul.ui-autocomplete { | ||
.autocomplete { | ||
--autocomplete-icon-width: 2.5rem; | ||
|
||
@extend .dropdown-menu; | ||
|
||
li { @extend .dropdown-item; } | ||
} | ||
top: 100%; | ||
left: var(--autocomplete-icon-width); | ||
width: calc(100% - var(--autocomplete-icon-width)); | ||
border-top-left-radius: 0; | ||
border-top-right-radius: 0; | ||
|
||
.autocomplete-item { | ||
@extend .dropdown-item; | ||
} | ||
|
||
.ui-helper-hidden-accessible{ | ||
display:none !important; | ||
.autocomplete-item-text { | ||
@extend .dropdown-item-text; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
src/api/app/javascript/controllers/autocomplete_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
static targets = [ "input", "results", "icon" ] | ||
static values = { url: String } | ||
|
||
connect() { | ||
// Creating the autocomplete dropdown menu | ||
const results = document.createElement("div"); | ||
results.setAttribute("data-autocomplete-target", "results"); | ||
results.classList.add("autocomplete"); | ||
this.inputTarget.insertAdjacentElement("afterend", results); | ||
|
||
// Empty the fields between reloads | ||
this.setEmpty(); | ||
this.inputTarget.value = ''; | ||
this.iconSearch(); | ||
} | ||
|
||
setEmpty() { | ||
this.resultsTarget.innerHTML = ''; | ||
const emptyText = document.createElement('span'); | ||
emptyText.classList.add("autocomplete-item-text"); | ||
emptyText.textContent = "Nothing found."; | ||
this.resultsTarget.appendChild(emptyText); | ||
} | ||
|
||
urlValueChanged() { | ||
if (this.hasResultsTarget) | ||
this.setEmpty(); | ||
} | ||
|
||
setResults(results) { | ||
if (results.length < 1) | ||
return this.setEmpty() | ||
this.resultsTarget.innerHTML = ''; | ||
results.forEach((result) => { | ||
let resultLink = document.createElement('a'); | ||
resultLink.setAttribute("data-action", "autocomplete#setInput"); | ||
resultLink.setAttribute("href", "#") | ||
resultLink.classList.add('autocomplete-item'); | ||
resultLink.textContent = result; | ||
this.resultsTarget.appendChild(resultLink); | ||
}); | ||
} | ||
|
||
setInput(event) { | ||
this.input(event.target.textContent); | ||
event.preventDefault(); | ||
} | ||
|
||
input(value) { | ||
this.dispatch("setInput", { detail: value }) | ||
this.inputTarget.value = value; | ||
this.hideResults(); | ||
} | ||
|
||
iconLoad() { | ||
this.iconTarget.classList = 'fas fa-spinner fa-spin'; | ||
} | ||
|
||
iconSearch() { | ||
this.iconTarget.classList = 'fas fa-search'; | ||
} | ||
|
||
showResults() { | ||
this.resultsTarget.classList.add('show') | ||
} | ||
|
||
hideResults() { | ||
// We need a small delay to allow for clicking on the dropdown | ||
setTimeout(() => { | ||
this.resultsTarget.classList.remove('show') | ||
}, 200) | ||
} | ||
|
||
async search() { | ||
this.iconLoad(); | ||
let query = this.inputTarget.value; | ||
this.dispatch("search", { detail: query }) | ||
let url = new URL(this.urlValue, window.location.origin); | ||
url.searchParams.set('term', query); | ||
|
||
if (query.length >= 3) { | ||
try { | ||
const response = await fetch(url, { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
const raw_data = await response.text(); | ||
const data = JSON.parse(raw_data); | ||
if (data.includes(query)) { | ||
this.input(query); | ||
} else { | ||
this.setResults(data); | ||
this.showResults(); | ||
} | ||
} catch { | ||
console.error("Failed to return results"); | ||
} | ||
} else { | ||
this.hideResults(); | ||
} | ||
this.iconSearch(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters