Skip to content

Commit

Permalink
Merge pull request #240 from DFE-Digital/add-include-hidden-support-t…
Browse files Browse the repository at this point in the history
…o-govuk-collection-check-boxes

Provide the ability to suppress hidden fields in checkbox collections
  • Loading branch information
peteryates authored Feb 17, 2021
2 parents 5e446fa + 9fc0a7f commit 516b35f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
8 changes: 8 additions & 0 deletions lib/govuk_design_system_formbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ module GOVUKDesignSystemFormBuilder
# blocks. As per the GOV.UK Design System spec, it defaults to
# 'There is a problem'.
#
# * +:default_collection_check_boxes_include_hidden+ controls whether or not
# a hidden field is added when rendering a collection of check boxes
#
# * +:default_collection_radio_buttons_include_hidden+ controls whether or not
# a hidden field is added when rendering a collection of radio buttons
#
# * +:localisation_schema_fallback+ sets the prefix elements for the array
# used to build the localisation string. The final two elements are always
# are the object name and attribute name. The _special_ value +__context__+,
Expand All @@ -52,6 +58,8 @@ module GOVUKDesignSystemFormBuilder
default_submit_button_text: 'Continue',
default_radio_divider_text: 'or',
default_error_summary_title: 'There is a problem',
default_collection_check_boxes_include_hidden: true,
default_collection_radio_buttons_include_hidden: true,

localisation_schema_fallback: %i(helpers __context__),
localisation_schema_label: nil,
Expand Down
6 changes: 4 additions & 2 deletions lib/govuk_design_system_formbuilder/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def govuk_collection_select(attribute_name, collection, value_method, text_metho
# :name,
# legend: -> { tag.h3('Which category do you belong to?') }
#
def govuk_collection_radio_buttons(attribute_name, collection, value_method, text_method = nil, hint_method = nil, hint: {}, legend: {}, caption: {}, inline: false, small: false, bold_labels: false, classes: nil, include_hidden: false, form_group: {}, &block)
def govuk_collection_radio_buttons(attribute_name, collection, value_method, text_method = nil, hint_method = nil, hint: {}, legend: {}, caption: {}, inline: false, small: false, bold_labels: false, classes: nil, include_hidden: config.default_collection_radio_buttons_include_hidden, form_group: {}, &block)
Elements::Radios::Collection.new(
self,
object_name,
Expand Down Expand Up @@ -646,6 +646,7 @@ def govuk_radio_divider(text = config.default_radio_divider_text)
# @param form_group [Hash] configures the form group
# @option form_group classes [Array,String] sets the form group's classes
# @option form_group kwargs [Hash] additional attributes added to the form group
# @param include_hidden [Boolean] controls whether a hidden field is inserted to allow for empty submissions
# @param block [Block] any HTML passed in will be injected into the fieldset, after the hint and before the checkboxes
# @return [ActiveSupport::SafeBuffer] HTML output
#
Expand Down Expand Up @@ -682,7 +683,7 @@ def govuk_radio_divider(text = config.default_radio_divider_text)
# :name,
# legend: -> { tag.h3('What kind of sandwich do you want?') }
#
def govuk_collection_check_boxes(attribute_name, collection, value_method, text_method, hint_method = nil, hint: {}, legend: {}, caption: {}, small: false, classes: nil, form_group: {}, &block)
def govuk_collection_check_boxes(attribute_name, collection, value_method, text_method, hint_method = nil, hint: {}, legend: {}, caption: {}, small: false, classes: nil, form_group: {}, include_hidden: config.default_collection_check_boxes_include_hidden, &block)
Elements::CheckBoxes::Collection.new(
self,
object_name,
Expand All @@ -697,6 +698,7 @@ def govuk_collection_check_boxes(attribute_name, collection, value_method, text_
small: small,
classes: classes,
form_group: form_group,
include_hidden: include_hidden,
&block
).html
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ class Collection < Base
include Traits::Hint
include Traits::Supplemental

def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint:, legend:, caption:, small:, classes:, form_group:, hint_method: nil, &block)
def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint:, legend:, caption:, small:, classes:, form_group:, include_hidden:, hint_method: nil, &block)
super(builder, object_name, attribute_name, &block)

@collection = collection
@value_method = value_method
@text_method = text_method
@hint_method = hint_method
@small = small
@legend = legend
@caption = caption
@hint = hint
@classes = classes
@form_group = form_group
@collection = collection
@value_method = value_method
@text_method = text_method
@hint_method = hint_method
@small = small
@legend = legend
@caption = caption
@hint = hint
@classes = classes
@form_group = form_group
@include_hidden = include_hidden
end

def html
Expand Down Expand Up @@ -56,7 +57,7 @@ def check_boxes
def collection
link_errors = has_errors?

@builder.collection_check_boxes(@attribute_name, @collection, @value_method, @text_method) do |check_box|
@builder.collection_check_boxes(@attribute_name, @collection, @value_method, @text_method, include_hidden: @include_hidden) do |check_box|
Elements::CheckBoxes::CollectionCheckBox.new(
@builder,
@object_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
end
end

specify 'a hidden field is present by default' do
expect(subject).to have_tag('input', with: { type: 'hidden', name: "#{object_name}[#{attribute}][]" })
end

context 'check box size' do
context 'when small is specified in the options' do
subject { builder.send(*args, small: true) }
Expand Down Expand Up @@ -156,6 +160,14 @@
it_behaves_like 'a collection field that supports setting the value via a proc' do
let(:attribute) { :favourite_colour }
end

context 'suppressing the hidden field' do
subject { builder.send(*args, include_hidden: false) }

specify "the hidden field should be present within the fieldset" do
expect(subject).not_to have_tag('input', with: { type: 'hidden' })
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
end

specify 'the radio button linked to should be first' do
expect(parsed_subject.css('input').first['id']).to eql(identifier)
expect(parsed_subject.css('input[type="radio"]').first['id']).to eql(identifier)
expect(parsed_subject.css('label').first['for']).to eql(identifier)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@
end

specify %(shouldn't have a hidden field by default) do
expect(subject).not_to have_tag('input', with: { type: 'hidden' })
expect(subject).to have_tag('fieldset') do
with_tag('input', with: { type: 'hidden', name: "#{object_name}[#{attribute}]" })
end
end

specify 'each label should have the correct classes' do
Expand Down Expand Up @@ -229,12 +231,10 @@
end

context 'generating a hidden field' do
subject { builder.send(*args, include_hidden: true) }
subject { builder.send(*args, include_hidden: false) }

specify "the hidden field should be present within the fieldset" do
expect(subject).to have_tag('fieldset') do
with_tag('input', with: { type: 'hidden', name: "#{object_name}[#{attribute}]" })
end
expect(subject).not_to have_tag('input', with: { type: 'hidden' })
end
end
end
Expand Down

0 comments on commit 516b35f

Please sign in to comment.