Skip to content

Instantly share code, notes, and snippets.

@laptopmutia
Created August 14, 2022 14:10
Show Gist options
  • Save laptopmutia/0a67a81fd39f87bffc94f8441f8f80ac to your computer and use it in GitHub Desktop.
Save laptopmutia/0a67a81fd39f87bffc94f8441f8f80ac to your computer and use it in GitHub Desktop.
// app/javascript/controllers/debounce_controller.js
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "form" ]
connect() { console.log("debounce controller connected") }
search() {
clearTimeout(this.timeout)
this.timeout = setTimeout(() => {
this.formTarget.requestSubmit()
}, 500)
}
}
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "form" ]
search() {
clearTimeout(this.timeout)
this.timeout = setTimeout(() => {
this.formTarget.requestSubmit()
}, 200)
}
}
<div class="flex justify-end mb-1">
<%= form_with url: list_players_path, method: :get, data: { controller: "search-form", search_form_target: "form", turbo_frame: "players" } do |form| %>
<%= form.text_field :name,
placeholder: "Search by name",
class: "border border-blue-500 rounded p-2",
autocomplete: "off",
data: { action: "input->search-form#search" }
%>
<% end %>
</div>
<%= turbo_frame_tag "players", class: "shadow overflow-hidden rounded border-b border-gray-200" do %>
<!-- Snip table -->
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment