Skip to content

Commit

Permalink
Add small option to checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
andysellick committed Apr 15, 2019
1 parent b83efcb commit d7a5343
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%
# cb_helper.css_classes generates "gem-c-checkboxes"
cb_helper = GovukPublishingComponents::Presenters::CheckboxesHelper.new(local_assigns)
id = cb_helper.id
%>
Expand All @@ -17,7 +18,7 @@
<%= tag.span error, id: "#{id}-error", class: "govuk-error-message" %>
<% end %>

<%= tag.ul class: "govuk-checkboxes gem-c-checkboxes__list", data: {
<%= tag.ul class: cb_helper.list_classes, data: {
module: ('checkboxes' if cb_helper.has_conditional),
nested: ('true' if cb_helper.has_nested),
} do %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ examples:
value: "green"
- label: "Blue"
value: "blue"
with_small_checkboxes:
data:
name: "favourite_small_synonym"
heading: "What is your favourite synonym for small?"
small: true
items:
- label: "Tiny"
value: "tiny"
- label: "Little"
value: "little"
with_a_heading_on_one_checkbox:
description: One checkbox does not require a fieldset and therefore does not require a legend. However, if a heading is supplied, a fieldset will be included in the component and the heading used as the legend, as shown.
data:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CheckboxesHelper
include ActionView::Helpers
include ActionView::Context

attr_reader :items, :name, :css_classes, :error, :has_conditional, :has_nested, :id, :hint_text
attr_reader :items, :name, :css_classes, :list_classes, :error, :has_conditional, :has_nested, :id, :hint_text

def initialize(options)
@items = options[:items] || []
Expand All @@ -13,6 +13,9 @@ def initialize(options)
@css_classes << "govuk-form-group--error" if options[:error]
@error = true if options[:error]

@list_classes = %w(govuk-checkboxes gem-c-checkboxes__list)
@list_classes << "govuk-checkboxes--small" if options[:small]

# check if any item is set as being conditional
@has_conditional = options[:items].any? { |item| item.is_a?(Hash) && item[:conditional] }
@has_nested = options[:items].any? { |item| item.is_a?(Hash) && item[:items] }
Expand Down
13 changes: 13 additions & 0 deletions spec/components/checkboxes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ def component_name
assert_select ".govuk-label", text: "Green"
end

it "renders small checkboxes" do
render_component(
name: "favourite_colour",
heading: "What is your favourite colour?",
small: true,
items: [
{ label: "Red", value: "red" },
{ label: "Green", value: "green" },
]
)
assert_select ".govuk-checkboxes.govuk-checkboxes--small"
end

it "renders nothing if no heading is supplied" do
render_component(
name: "favourite_colour",
Expand Down

0 comments on commit d7a5343

Please sign in to comment.