Skip to content

Commit

Permalink
Merge pull request #316 from DFE-Digital/improve-bold-label-behaviour
Browse files Browse the repository at this point in the history
Make radio labels respect the `bold_labels:` setting
  • Loading branch information
peteryates authored Sep 10, 2021
2 parents d19e953 + acb4638 commit 062f35c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/govuk_design_system_formbuilder/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,9 @@ def govuk_select(attribute_name, choices = nil, options: {}, label: {}, hint: {}
# @note Unlike the Rails +#collection_radio_buttons+ helper, this version can also insert
# hints per item in the collection by supplying a +:hint_method+
#
# @note +:bold_labels+, while false by default, is set to true when a
# +:hint_method+ is provided. This is done to make the label stand out more
# from the hint.
# @note +:bold_labels+, is +nil+ (falsy) by default. When a +:hint_method+
# is provided it will become +true+ to make the label stand out more
# from the hint. The choice can be overridden with +true+ or +false+.
#
# @param attribute_name [Symbol] The name of the attribute
# @param collection [Enumerable<Object>] Options to be added to the +select+ element
Expand Down Expand Up @@ -538,7 +538,7 @@ def govuk_select(attribute_name, choices = nil, options: {}, label: {}, hint: {}
# :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: config.default_collection_radio_buttons_include_hidden, 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: nil, classes: nil, include_hidden: config.default_collection_radio_buttons_include_hidden, form_group: {}, &block)
Elements::Radios::Collection.new(
self,
object_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ def initialize(builder, object_name, attribute_name, collection, value_method:,
@classes = classes
@form_group = form_group
@include_hidden = include_hidden
@bold_labels = hint_method.present? || bold_labels
@bold_labels = if bold_labels.nil?
hint_method.present?
else
bold_labels
end
end

def html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,41 @@

context 'bold labels' do
let(:bold_label_class) { 'govuk-label--s' }
let(:bold_labels) { nil }
let(:hint_method) { nil }

context 'when bold labels are specified in the options' do
subject do
builder.send(*args.push(:description), bold_labels: true)
subject do
builder.send(*args.push(hint_method), bold_labels: bold_labels)
end

context 'when bold_labels: nil' do
context 'when a hint method is present' do
let(:hint_method) { :description }

specify 'labels should be bold to help them stand out from hints' do
expect(subject).to have_tag('label', with: { class: bold_label_class }, count: colours.size)
end
end

context 'when no hint method is present' do
specify 'no labels should be set to bold' do
expect(subject).not_to have_tag('label', with: { class: bold_label_class })
end
end
end

context 'when bold_labels: true' do
let(:bold_labels) { true }

specify 'all labels should be bold when hints are enabled' do
expect(subject).to have_tag('label', with: { class: bold_label_class }, count: colours.size)
end
end

context 'when bold labels are not specified in the options' do
specify 'no labels should be bold when hints are enabled' do
context 'when bold_labels: false' do
let(:bold_labels) { false }

specify 'no labels should be set to bold' do
expect(subject).not_to have_tag('label', with: { class: bold_label_class })
end
end
Expand Down

0 comments on commit 062f35c

Please sign in to comment.