Module: RailsuiCharts::FilterHelper

Defined in:
lib/railsui_charts/filter_helper.rb

Instance Method Summary collapse

Instance Method Details

#railsui_chart_filters(filters, url: nil, frame: nil, select_class: nil, checkbox_class: nil) ⇒ Object

Renders the single filter row that scopes every chart below it.

<%= railsui_chart_filters @filters, url: dashboard_path %>

Submits as a plain GET form, so it works without JavaScript and each slice gets a shareable URL. With Turbo, the controller submits on change and only the frame re-renders.

The controls are deliberately bare — text and a chevron, with the pill only appearing on hover. Filter chrome sits above the data all day, so it earns its weight only while someone is reaching for it.

An app that would rather use its own form styles passes them in. Doing so drops the built-in chevron and switch, since those belong to this treatment:

<%= railsui_chart_filters @filters, url: dashboard_path,
    select_class: "form-select w-auto", checkbox_class: "form-input-checkbox" %>


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/railsui_charts/filter_helper.rb', line 23

def railsui_chart_filters(filters, url: nil, frame: nil, select_class: nil, checkbox_class: nil)
  bare_selects = select_class.nil?
  switch = checkbox_class.nil?
  select_class ||= "railsui-chart-filters__select"
  checkbox_class ||= "railsui-chart-filters__checkbox"

  form_tag(url, method: :get, class: "railsui-chart-filters", data: filter_form_data(frame)) do
    safe_join([
      filter_field("Date range",
                   select_tag(:range, options_for_select(filters.presets.map { |preset| [preset.label, preset.key] }, filters.preset.key), **filter_select_options(select_class)),
                   chevron: bare_selects),
      filter_field("Interval",
                   select_tag(:interval, options_for_select(filters.intervals, filters.interval.to_s), **filter_select_options(select_class)),
                   chevron: bare_selects),
      filter_compare(filters, checkbox_class, switch: switch),
      # Without Turbo or Stimulus the selects still need a way to apply.
      (:noscript, submit_tag("Apply", class: "railsui-chart-filters__apply", data: { disable_with: nil }))
    ])
  end
end