Comments (2)
Just as a follow up, I though I'd share my attempt at creating a pure stimulus controller - autopickr_controller.js.
It uses the same approach in that it has a markup that controls the application. I saw the pull request and I had also implemented the length stuff using a value.
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["input",'results','hidden','selected','button',"options"]
static values = { url: String ,slen: Number}
connect() {
console.log("Hi autopickr")
let slen // slen is the number of charcters entered before we search/query
if (this.hasSlenValue) {
this.slen = this.slenValue
}else{
this.slen = 1
}
this.inputTarget.click() // activate the autofocus target
}
select(){
const selected = event.target
if (this.hasButtonTarget) { // if there is a button target, display instead of following url
this.buttonTarget.classList.remove('hidden')
this.buttonTarget['href'] = selected.dataset.select
}else{
location.assign(selected.dataset.select)
}
}
selected(){
this.clear_results()
}
clear_results(){
while (this.resultsTarget.firstChild) {
this.resultsTarget.removeChild(this.resultsTarget.firstChild);
}
}
async search(){
var len = this.inputTarget.value.length
if (len >= this.slen) {
let response = await fetch(this.urlValue+`?input=${this.inputTarget.value}`);
let data = await response.text();
let frag = document.createRange().createContextualFragment(data);
this.clear_results() // set result to no children
this.resultsTarget.appendChild(frag) // rebuild the results
}
}
}
I'm certainly not a JS person, but I wondered why there was all this creating events to handle stuff like up and down arrows when the stimulus input action does all that. It has about everything except the ariel comment li
. Even had that half ass working but never used it in autocomplete.
In the markup I added an optional button that basically gives you a confirm button which you could modify the url. (I use slim instead of erb!)
div[
data-autopickr-url-value="/demo/search"
data-controller="autopickr"
data-autopickr-slen-value="1"
class=blueBox
// just calling a helper to set tailwind classes for a simple button
]
div
= text_field_tag(:input,nil,
data:{autopickr_target:"input", action:"autopickr#search" },
placeholder:"Search...",class:"w-48 px-1 my-2",autofocus:true)
// span = link_to('Pick',nil,data:{autopickr_target:"button"}, class:"bg-blue-500 hover:bg-blue-700 text-white font-bold mx-2 py px-2 rounded inline-block hidden")
div = hidden_field_tag('selected_id',nil,
data:{autopickr_target:"hidden",action:"change->autopickr#selected"})
ul.text-black.bg-gray-100.max-h-52.divide-y.divide-fuchsia-300.overflow-scroll[data-autopickr-target="results"]
My problem was not getting autocomplete to work with Rails 7.x This works and I even put up a demo of a simple name search from a simulated model. Just names of family and pets. The demo is on a staging server on Rails 6.x- Name Search
I put the demo app on GitHub
from stimulus-autocomplete.
Stimulus 3 and rails 7 are already supported on main and in the latest release.
but I wondered why there was all this creating events to handle stuff like up and down arrows when the stimulus input action does all that
The goal of the controller is to provide a fully functional autocomplete with a minimal amount of js and changes to your HTML. You can certainly bind the actions with data attributes, but that'd make the template more complicated when probably most people just want the default behaviour.
from stimulus-autocomplete.
Related Issues (20)
- Provide default styles
- Failed to resolve module specifier "stimulus-autocomplete" HOT 3
- Prefilled form HOT 1
- Additional event on select HOT 3
- How to fetch data on focus? HOT 11
- Make the query paramater name `q` customizable HOT 2
- error event doesn't fire on 400+ response HOT 1
- Auto-select/highlight the first result on "data load ended" HOT 2
- Suggestion: Allow to use local HTML instead of fetching from the server HOT 3
- Prevent Lighthouse Aria warnings HOT 1
- Data is not being Filtered. HOT 1
- Accessibility
- Feature improve: When mouse move on an option item, it should become the current selected option item. HOT 4
- data-autocomplete-query-param-value not working HOT 4
- New release? HOT 2
- Set "active" class based on what result the user's mouse is over HOT 1
- Combobox with results is hidden when a user goes to another Browser tab HOT 1
- Pre-selected value HOT 1
- Combo Box with client-side fuzzy finder with a static list options
- Question: Handle a 429 Response?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stimulus-autocomplete.