Created
May 23, 2024 16:34
-
-
Save spencerldixon/1d6116ddddbdeb8884f374918263ce47 to your computer and use it in GitHub Desktop.
Real time search with 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
def search | |
@pagy, @bookmarks = if params[:search].present? | |
pagy(@bookmarks.search(params[:search]), params: ->(params) { params.merge!({search: params[:search]}) }) | |
else | |
pagy(@bookmarks) | |
end | |
end |
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" | |
// Connects to data-controller="form-submit" | |
// Use data-action="input->form-submit#submit" to auto-submit a form on input | |
export default class extends Controller { | |
submit() { | |
clearTimeout(this.timeout) | |
this.timeout = setTimeout(() => { | |
this.element.requestSubmit(); | |
}, 100) | |
} | |
} |
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
<%= form_with(url: search_path, class: "flex-grow", data: { turbo_stream: true, controller: "form-submit" }) do |form| %> | |
<div class="flex items-center justify-center w-full"> | |
<div class="relative flex items-center w-full flex-wrap"> | |
<%= form.text_field :search, placeholder: "Search", class: "bg-white border-slate-400 rounded-full grow px-6 py-3 cursor-pointer", data: { action: "input->form-submit#submit" } %> | |
<span class="z-10 absolute items-center justify-center w-8 right-0 pr-3 py-3 cursor-pointer"> | |
<i class="fa-solid fa-magnifying-glass text-slate-900"></i> | |
</span> | |
</div> | |
</div> | |
<% end %> |
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
post "search", to: "bookmarks#search" |
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
<%= turbo_stream.replace "bookmarks" do %> | |
<%= render "bookmarks/bookmarks", { bookmarks: @bookmarks, pagy: @pagy } %> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment