Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append custom custom classes to labels #397

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/govuk_design_system_formbuilder/elements/caption.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module GOVUKDesignSystemFormBuilder
module Elements
class Caption < Base
include Traits::Localisation
include Traits::HTMLAttributes
include Traits::HTMLClasses

def initialize(builder, object_name, attribute_name, text: nil, size: config.default_caption_size, **kwargs)
super(builder, object_name, attribute_name)
Expand All @@ -14,11 +16,15 @@ def initialize(builder, object_name, attribute_name, text: nil, size: config.def
def html
return unless active?

tag.span(@text, class: @size_class, **@html_attributes)
tag.span(@text, **attributes(@html_attributes))
end

private

def options
{ class: @size_class }
end

def active?
@text.present?
end
Expand All @@ -28,13 +34,9 @@ def text(override)
end

def size_class(size)
case size
when 'xl' then %(#{brand}-caption-xl)
when 'l' then %(#{brand}-caption-l)
when 'm' then %(#{brand}-caption-m)
else
fail ArgumentError, "invalid size '#{size}', must be xl, l or m"
end
fail ArgumentError, "invalid size '#{size}', must be xl, l or m" unless size.in?(%w(xl l m))

%(#{brand}-caption-#{size})
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/govuk_design_system_formbuilder/elements/hint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class Hint < Base
using PrefixableArray

include Traits::Localisation
include Traits::HTMLAttributes
include Traits::HTMLClasses

def initialize(builder, object_name, attribute_name, value: nil, text: nil, content: nil, radio: false, checkbox: false, **kwargs)
super(builder, object_name, attribute_name)
Expand All @@ -27,7 +29,7 @@ def active?
def html
return unless active?

tag.div(**hint_options, **@html_attributes) { hint_body }
tag.div(**attributes(@html_attributes)) { hint_body }
end

def hint_id
Expand All @@ -38,7 +40,7 @@ def hint_id

private

def hint_options
def options
{ class: classes, id: hint_id }
end

Expand Down
36 changes: 16 additions & 20 deletions lib/govuk_design_system_formbuilder/elements/label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class Label < Base

include Traits::Caption
include Traits::Localisation
include Traits::HTMLAttributes
include Traits::HTMLClasses

def initialize(builder, object_name, attribute_name, text: nil, value: nil, size: nil, hidden: false, radio: false, checkbox: false, tag: nil, link_errors: true, content: nil, caption: nil, **kwargs)
super(builder, object_name, attribute_name)
Expand Down Expand Up @@ -44,7 +46,7 @@ def active?
private

def label
@builder.label(@attribute_name, **options, **@html_attributes) do
@builder.label(@attribute_name, **attributes(@html_attributes)) do
@content || safe_join([caption, @text])
end
end
Expand All @@ -63,36 +65,30 @@ def options
{
value: @value,
for: field_id(link_errors: @link_errors),
class: %w(label).prefix(brand).push(@size_class, @weight_class, radio_class, checkbox_class).compact
class: classes
}
end

def caption
caption_element.html unless [@radio, @checkbox].any?
end

def radio_class
return unless @radio

%(#{brand}-radios__label)
def classes
build_classes(
%(label),
@size_class,
@weight_class,
%(radios__label) => @radio,
%(checkboxes__label) => @checkbox,
).prefix(brand)
end

def checkbox_class
return unless @checkbox
def size_class(size)
return if size.blank?

%(#{brand}-checkboxes__label)
end
fail "invalid size '#{size}', must be xl, l, m, s or nil" unless size.in?(%w(xl l m s))

def size_class(size)
case size
when 'xl' then %(#{brand}-label--xl)
when 'l' then %(#{brand}-label--l)
when 'm' then %(#{brand}-label--m)
when 's' then %(#{brand}-label--s)
when nil then nil
else
fail "invalid size '#{size}', must be xl, l, m, s or nil"
end
%(label--#{size})
end
end
end
Expand Down
32 changes: 19 additions & 13 deletions lib/govuk_design_system_formbuilder/elements/legend.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module GOVUKDesignSystemFormBuilder
module Elements
class Legend < Base
using PrefixableArray

include Traits::Caption
include Traits::Localisation
include Traits::HTMLAttributes
include Traits::HTMLClasses

def initialize(builder, object_name, attribute_name, text: nil, size: config.default_legend_size, hidden: false, tag: config.default_legend_tag, caption: nil, content: nil, **kwargs)
super(builder, object_name, attribute_name)
Expand Down Expand Up @@ -33,7 +37,19 @@ def active?
def legend
return unless active?

tag.legend(legend_content, class: classes, **@html_attributes)
tag.legend(legend_content, **attributes(@html_attributes))
end

def options
{ class: classes }
end

def classes
build_classes(
%(fieldset__legend),
@size_class,
%(visually-hidden) => @hidden
).prefix(brand)
end

def legend_content
Expand All @@ -48,20 +64,10 @@ def retrieve_text(supplied_text)
[supplied_text, localised_text(:legend), @attribute_name&.capitalize].find(&:presence)
end

def classes
[%(#{brand}-fieldset__legend), @size_class, visually_hidden_class].compact
end

def size_class(size)
if size.in?(%w(xl l m s))
%(#{brand}-fieldset__legend--#{size})
else
fail "invalid size '#{size}', must be xl, l, m or s"
end
end
fail "invalid size '#{size}', must be xl, l, m or s" unless size.in?(%w(xl l m s))

def visually_hidden_class
%(#{brand}-visually-hidden) if @hidden
%(fieldset__legend--#{size})
end

def heading_classes
Expand Down
9 changes: 9 additions & 0 deletions spec/support/shared/shared_caption_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
end
end

context 'when additional classes are provided' do
let(:classes) { %w(foo bar) }
subject { builder.send(*args, caption: { text: caption_text }.merge(class: classes)) }

specify 'the caption should have the custom classes' do
expect(subject).to have_tag('.govuk-caption-m', with: { class: classes }, text: caption_text)
end
end

context 'when additional HTML attributes are provided' do
let(:html_attributes) { { focusable: 'false', dir: 'rtl' } }
subject { builder.send(*args, **caption_container, caption: caption_args.merge(html_attributes)) }
Expand Down
11 changes: 10 additions & 1 deletion spec/support/shared/shared_hint_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@
expect(input_aria_describedby).to include(hint_id)
end

context 'when additional classes are provided' do
let(:classes) { %w(foo bar) }
subject { builder.send(*args, hint: { text: hint_text }.merge(class: classes)) }

specify 'the hint should have the custom classes' do
expect(subject).to have_tag('.govuk-hint', with: { class: classes }, text: hint_text)
end
end

context 'when additional HTML attributes are provided' do
let(:html_attributes) { { focusable: 'false', dir: 'rtl' } }
subject { builder.send(*args, hint: { text: hint_text }.merge(html_attributes)) }

specify 'the label should have the custom HTML attributes' do
specify 'the hint should have the custom HTML attributes' do
expect(subject).to have_tag('.govuk-hint', with: html_attributes, text: hint_text)
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/support/shared/shared_label_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@
end
end

context 'when additional classes are provided' do
let(:classes) { %w(foo bar) }
subject { builder.send(*args, label: { text: label_text }.merge(class: classes)) }

specify 'the label should have the custom classes' do
expect(subject).to have_tag('.govuk-label', with: { class: classes }, text: label_text)
end
end

context 'when additional HTML attributes are provided' do
let(:html_attributes) { { focusable: 'false', dir: 'rtl' } }
subject { builder.send(*args, label: { text: label_text }.merge(html_attributes)) }
Expand Down
9 changes: 9 additions & 0 deletions spec/support/shared/shared_legend_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@
end
end

context 'when additional classes are provided' do
let(:classes) { %w(foo bar) }
subject { builder.send(*args, legend: { text: legend_text }.merge(class: classes)) }

specify 'the legend should have the custom classes' do
expect(subject).to have_tag('.govuk-fieldset__legend', with: { class: classes }, text: legend_text)
end
end

context 'when additional HTML attributes are provided' do
let(:html_attributes) { { focusable: 'false', lang: 'fr' } }
subject { builder.send(*args, legend: { text: legend_text }.merge(html_attributes)) }
Expand Down