Class: Katalyst::Tables::QueryComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
HtmlAttributes, Frontend
Defined in:
app/components/katalyst/tables/query_component.rb

Overview

A component for rendering a data driven filter for a collection.

<%= table_query_with(collection:) %>

Equivalent to:

<%= render Katalyst::Tables::QueryComponent.new(collection: @people, url: peoples_path) %>

By default, the component will render a form containing a single text field. Interacting with the text field will display a dropdown outlining all available keys and values to be filtered on.

You can override how the form and input displays by passing in content to the component. The component provides a helper function ‘form` to ensure the correct attributes and default form fields are collected. You can pass additional options to the `form` method to modify it.

Caution: ‘config.view_component.capture_compatibility_patch_enabled = true` is required for this to work.

<%= table_query_with(collection:) do |component| %>
  <% component.with_form(builder: GOVUKFormBuilder) do |form| %>
    <%= render component.query_input(form:) %>
    <%= form.govuk_submit "Apply" %>
  <% end %>
<% end %>

Additionally the component allows for access to the dropdown that displays when interacting with the input. The dropdown supports additional “footer” content to be added.

<%= table_query_with(collection:) do |component| %>
  <% component.with_modal(collection:) do |modal| %>
    <% modal.with_footer do %>
      <%= link_to "Docs", docs_path %>
    <% end %>
  <% end %>
<% end %>

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Frontend

#summary_table_with, #table_orderable_with, #table_pagination_with, #table_query_with, #table_selection_with, #table_with

Constructor Details

#initialize(collection:, url:) ⇒ QueryComponent

Returns a new instance of QueryComponent.



46
47
48
49
50
51
52
53
54
55
# File 'app/components/katalyst/tables/query_component.rb', line 46

def initialize(collection:, url:, **)
  super(**)

  @collection = collection
  @url        = url

  # defaults, can be overwritten from content
  with_modal(collection:)
  with_form
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



44
45
46
# File 'app/components/katalyst/tables/query_component.rb', line 44

def collection
  @collection
end

#urlObject (readonly)

Returns the value of attribute url.



44
45
46
# File 'app/components/katalyst/tables/query_component.rb', line 44

def url
  @url
end

Instance Method Details

#before_renderObject



57
58
59
# File 'app/components/katalyst/tables/query_component.rb', line 57

def before_render
  content # content is discarded, but may alter slots
end

#sort_input(form:) ⇒ Object



73
74
75
76
77
# File 'app/components/katalyst/tables/query_component.rb', line 73

def sort_input(form:)
  return if collection.default_sort?

  form.hidden_field(:sort)
end

#with_form(model: collection, url: @url, method: :get, **options, &block) ⇒ Object

Override the default form options for query. Proxies all arguments to ‘form_with`.

Caution: requires config.view_component.capture_compatibility_patch_enabled to be set



64
65
66
67
# File 'app/components/katalyst/tables/query_component.rb', line 64

def with_form(model: collection, url: @url, method: :get, **options, &block)
  @form_options = { model:, url:, method:, **options }
  @form_block = block
end

#with_modal(collection: self.collection) ⇒ Object



69
70
71
# File 'app/components/katalyst/tables/query_component.rb', line 69

def with_modal(collection: self.collection, **, &)
  set_slot(:modal, nil, collection:, **, &)
end