Skip to content

Commit

Permalink
Add options_form_component
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie authored and JamieCleare2525 committed Apr 12, 2024
1 parent 170d709 commit d0b9577
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
51 changes: 51 additions & 0 deletions app/components/options_form_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<% if records.present? %>
<%= form_with(
model:,
scope:,
url:,
method: "get",
data: { turbo: false },
builder: GOVUKDesignSystemFormBuilder::FormBuilder,
) do |f| %>
<%= f.govuk_error_summary %>
<%= f.hidden_field :search_param, value: search_param %>

<h1 class="govuk-heading-l govuk-!-margin-bottom-0">
<span class="govuk-caption-l"><%= title %></span>
</h1>

<%= f.govuk_radio_buttons_fieldset(
input_field_name,
scope:,
legend: {
text: t(
".legend",
record_count: records.count,
search_param:,
),
size: "l",
},
hint: { text: form_description },
) do %>
<% records.first(OPTIONS_PER_PAGE).each do |record| %>
<%= f.govuk_radio_button input_field_name,
record.id,
label: { text: record.name },
link_errors: true,
hint: { text: record.town_and_postcode } %>
<% end %>
<% end %>
<%= f.govuk_submit t(".continue") %>
<% end %>
<% else %>
<h1 class="govuk-heading-l">
<span class="govuk-caption-l"><%= t(".add_organisation") %></span>
<%= t(".no_results", search_param:) %>
</h1>

<div class="govuk-hint">
<%= govuk_link_to(t(".narrow_your_search"), back_link, no_visited_state: true) %>
</div>
<% end %>
61 changes: 61 additions & 0 deletions app/components/options_form_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class OptionsFormComponent < ApplicationComponent
OPTIONS_PER_PAGE = 15

attr_reader :model, :url, :search_param, :records,
:back_link, :scope, :input_field_name, :title

def initialize(
model:,
url:,
search_param:,
records:,
back_link:,
scope:,
input_field_name:,
title:,
classes: [],
html_attributes: {}
)
super(classes:, html_attributes:)

@model = model
@url = url
@search_param = search_param
@records = records
@back_link = back_link
@scope = scope
@input_field_name = input_field_name
@title = title
end

def form_description
if records.count > OPTIONS_PER_PAGE
t(
"components.options_form_component.paginated_form_description_html",
record_count: OPTIONS_PER_PAGE,
klass: records_klass,
link_to: govuk_link_to(
t("components.options_form_component.narrow_your_search"),
back_link,
no_visited_state: true,
),
)
else
t(
"components.options_form_component.form_description_html",
klass: records_klass,
link_to: govuk_link_to(
t("components.options_form_component.change_your_search"),
back_link,
no_visited_state: true,
),
)
end
end

private

def records_klass
@records_klass ||= records.base_class.to_s.downcase
end
end

0 comments on commit d0b9577

Please sign in to comment.