Skip to content

Instantly share code, notes, and snippets.

View michalkorzawski's full-sized avatar

michalkorzawski

View GitHub Profile
# html input
<input id="search-input" data-reflex="keyup->FilterReflex#results">
# .rb Reflex
class FilterReflex < ApplicationReflex
def results
end
end
<%= form_with(url: '/', data: { reflex: "submit->FilterReflex#results" }) do %>
<!--
A bunch of <input> fields and <select> tags.
-->
<% end %>
<%== pagy_nav(@pagy, link_extra: "data-reflex='click->FilterReflex#results") %>
# FilterReflex#results
page = element.href&.split('=')&.last&.to_i || 1
# And pass it to pagy collection
pagy, collection = pagy(active_record_collection, page: page)
def filtered_params
params.permit(…)
end
def results
params_hash = element.dataset['params-hash'].present? ? JSON.parse(element.dataset['params-hash']) : filtered_params
pagy, collection = pagy(active_record_collection(filters), page: page)
# The HTML provided can be a partial or ViewComponent (as we use in our case)
cable_ready.morph(
children_only: true,
selector: '#id_of_a_dom_element',
html: render(
PaginationComponent.new(
<%== pagy_nav(@pagy, link_extra: "data-reflex='click->FilterReflex#results data-params-hash='#{@params_hash}'") %>
def results
params_hash = element.dataset['params-hash'].present? ? JSON.parse(element.dataset['params-hash']) : filtered_params
query = element.value.presence || params_hash['query']
params_hash['query'] = query
end
<input id="search-input" data-reflex="keyup->FilterReflex#results" data-from-search-input="true">
def results
params_hash = element.dataset['params-hash'].present? ? JSON.parse(element.dataset['params-hash']) : filtered_params
query = (element.dataset.from_search_input ? element.value : element.value.presence) || params_hash['query']
params_hash['query'] = query
end