Class: EasyAdmin::FiltersComponent

Inherits:
Phlex::HTML
  • Object
show all
Defined in:
app/components/easy_admin/filters_component.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource_class:, current_params: {}, search_params: {}) ⇒ FiltersComponent

Returns a new instance of FiltersComponent.



3
4
5
6
7
# File 'app/components/easy_admin/filters_component.rb', line 3

def initialize(resource_class:, current_params: {}, search_params: {})
  @resource_class = resource_class
  @current_params = current_params
  @search_params = search_params || {}
end

Instance Method Details

#view_templateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/components/easy_admin/filters_component.rb', line 9

def view_template
  return unless @resource_class.filterable_fields.any?

  div(class: "bg-white shadow rounded-lg mb-6") do
    form(
      method: "get",
      action: route_helper.resources_path(@resource_class.route_key),
      data: { turbo_frame: "table-frame" },
      class: "space-y-4"
    ) do
      # Preserve existing params (except search params)
      preserve_params

      div(class: "p-6 border-b border-gray-200") do
        div(class: "flex items-center justify-between mb-4") do
          div do
            h3(class: "text-lg font-medium text-gray-900") { "Filters" }
            p(class: "text-sm text-gray-500") { "Filter #{@resource_class.title.downcase} by specific criteria" }
          end
          
          # Clear filters button
          if has_active_filters?
            a(
              href: build_clear_filters_url,
              class: "text-sm text-red-600 hover:text-red-800 font-medium",
              data: { turbo_frame: "table-frame" }
            ) { "Clear all filters" }
          end
        end

        div(class: "grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4") do
          @resource_class.filterable_fields.each do |field|
            render_filter_field(field)
          end
        end
      end

      div(class: "px-6 py-4 bg-gray-50") do
        div(class: "flex items-center justify-end space-x-3") do
          button(
            type: "submit",
            class: "inline-flex items-center px-4 py-2 bg-blue-600 text-white text-sm font-medium rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
          ) do
            unsafe_raw '<svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 4a1 1 0 011-1h16a1 1 0 011 1v2.586a1 1 0 01-.293.707l-6.414 6.414a1 1 0 00-.293.707v6.586l-4-4V9.414a1 1 0 00-.293-.707L3.293 6.293A1 1 0 013 5.586V4z"></path>
            </svg>'
            span { "Apply Filters" }
          end
        end
      end
    end
  end
end