Skip to content
This repository was archived by the owner on May 5, 2020. It is now read-only.

Commit

Permalink
Merge pull request #89 from ministryofjustice/fieldset-legends
Browse files Browse the repository at this point in the history
Ability to provide html attributes to fieldset legends
  • Loading branch information
aliuk2012 authored Jan 12, 2018
2 parents 64d88d6 + f206bc3 commit b8f05f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
53 changes: 23 additions & 30 deletions lib/govuk_elements_form_builder/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def radio_button_fieldset attribute, options={}
id: form_group_id(attribute) do
content_tag :fieldset, fieldset_options(attribute, options) do
safe_join([
fieldset_legend(attribute),
fieldset_legend(attribute, options),
radio_inputs(attribute, options)
], "\n")
end
Expand All @@ -60,7 +60,7 @@ def check_box_fieldset legend_key, attributes, options={}
id: form_group_id(attributes) do
content_tag :fieldset, fieldset_options(attributes, options) do
safe_join([
fieldset_legend(legend_key),
fieldset_legend(legend_key, options),
check_box_inputs(attributes)
], "\n")
end
Expand All @@ -84,36 +84,29 @@ def collection_select method, collection, value_method, text_method, options = {

private

# Given an attributes hash that could include any number of arbitrary keys, this method
# ensure we merge one or more 'default' attributes into the hash, creating the keys if
# don't exist, or merging the defaults if the keys already exists.
# It supports strings or arrays as values.
#
def merge_attributes attributes, default:
hash = attributes || {}
hash.merge(default) { |_key, oldval, newval| Array(newval) + Array(oldval) }
end

def set_field_classes! options
text_field_class = "form-control"
options[:class] = case options[:class]
when String
[text_field_class, options[:class]]
when Array
options[:class].unshift text_field_class
else
options[:class] = text_field_class
end
options ||= {}
options.merge!(
merge_attributes(options, default: {class: 'form-control'})
)
end

def set_label_classes! options
text_field_class = "form-label"

if options.present? && options[:label_options].present?
options[:label_options][:class] = case options[:label_options][:class]
when String
[text_field_class, options[:label_options][:class]]
when Array
options[:label_options][:class].unshift text_field_class
else
options[:label_options][:class] = text_field_class
end
else
options ||= {}
options[:label_options] ||= {}
options[:label_options][:class] = text_field_class
end

options ||= {}
options[:label_options] ||= {}
options[:label_options].merge!(
merge_attributes(options[:label_options], default: {class: 'form-label'})
)
end

def check_box_inputs attributes
Expand Down Expand Up @@ -143,12 +136,12 @@ def radio_inputs attribute, options
end
end

def fieldset_legend attribute
def fieldset_legend attribute, options
legend = content_tag(:legend) do
tags = [content_tag(
:span,
fieldset_text(attribute),
class: 'form-label-bold'
merge_attributes(options[:legend_options], default: {class: 'form-label-bold'})
)]

if error_for? attribute
Expand Down
16 changes: 15 additions & 1 deletion spec/lib/govuk_elements_form_builder/form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ def expected_error_html method, type, attribute, name_value, label, error
]
end

it 'propagates html attributes down to the legend inner span if any provided, appending to the defaults' do
output = builder.radio_button_fieldset :has_user_account, inline: true, legend_options: {class: 'visuallyhidden', lang: 'en'}
expect(output).to match(/<legend><span class="form-label-bold visuallyhidden" lang="en">/)
end

context 'with a couple associated cases' do
let(:case_1) { Case.new(id: 1, name: 'Case One') }
let(:case_2) { Case.new(id: 2, name: 'Case Two') }
Expand Down Expand Up @@ -391,8 +396,11 @@ def expected_error_html method, type, attribute, name_value, label, error
end

describe '#check_box_fieldset' do
it 'outputs checkboxes wrapped in labels' do
before do
resource.waste_transport = WasteTransport.new
end

it 'outputs checkboxes wrapped in labels' do
output = builder.fields_for(:waste_transport) do |f|
f.check_box_fieldset :waste_transport, [:animal_carcasses, :mines_quarries, :farm_agricultural]
end
Expand Down Expand Up @@ -432,6 +440,12 @@ def expected_error_html method, type, attribute, name_value, label, error
]
end

it 'propagates html attributes down to the legend inner span if any provided, appending to the defaults' do
output = builder.fields_for(:waste_transport) do |f|
f.check_box_fieldset :waste_transport, [:animal_carcasses, :mines_quarries, :farm_agricultural], legend_options: {class: 'visuallyhidden', lang: 'en'}
end
expect(output).to match(/<legend><span class="form-label-bold visuallyhidden" lang="en">/)
end
end

describe '#collection_select' do
Expand Down

0 comments on commit b8f05f2

Please sign in to comment.