Created
August 14, 2022 14:10
-
-
Save laptopmutia/0a67a81fd39f87bffc94f8441f8f80ac to your computer and use it in GitHub Desktop.
debouce oninput turbo https://www.colby.so/posts/filtering-tables-with-rails-and-hotwire
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
// 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) | |
} | |
} |
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
import { Controller } from "@hotwired/stimulus" | |
export default class extends Controller { | |
static targets = [ "form" ] | |
search() { | |
clearTimeout(this.timeout) | |
this.timeout = setTimeout(() => { | |
this.formTarget.requestSubmit() | |
}, 200) | |
} | |
} |
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
<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