Skip to content

Commit

Permalink
Allow legend size to be nil
Browse files Browse the repository at this point in the history
This updates the legend element to allow the size attribute to be `nil`,
and when `nil` a size class won't be specified for the element. This is
required to support the case where a field has a legend element but
shouldn't have a size class.

There's an example of this kind of component in the GOV.UK Design
System:

https://design-system.service.gov.uk/components/date-input/#if-youre-asking-more-than-one-question-on-the-page

It's currently not possible to re-create this example using purely the
form builder classes as the size is enforced as one of `s`, `m`, `l` or
`xl`.
  • Loading branch information
thomasleese committed Jul 17, 2024
1 parent 8c0fb5d commit 8ca528a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/govuk_design_system_formbuilder/elements/legend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def retrieve_text(supplied_text)
end

def size_class(size)
return nil if size.nil?

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

%(fieldset__legend--#{size})
Expand Down
55 changes: 55 additions & 0 deletions spec/govuk_design_system_formbuilder/elements/legend_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
describe GOVUKDesignSystemFormBuilder::Elements::Legend do
subject(:html) { described_class.new(builder, object_name, attribute_name, size:).html }

let(:controller) { ActionController::Base.new }
let(:lookup_context) { ActionView::LookupContext.new(nil) }
let(:helper) { ActionView::Base.new(lookup_context, {}, controller) }
let(:object) { Person.new(name: 'Joey') }
let(:object_name) { :person }
let(:builder) { GOVUKDesignSystemFormBuilder::FormBuilder.new(object_name, object, helper, {}) }
let(:attribute_name) { :name }

context 'with a nil size' do
let(:size) { nil }

specify 'the size class should be absent' do
expect(html).to have_tag('legend', with: { class: 'govuk-fieldset__legend' })
expect(html).to_not have_tag('legend', with: { class: 'govuk-fieldset__legend--s' })
expect(html).to_not have_tag('legend', with: { class: 'govuk-fieldset__legend--m' })
expect(html).to_not have_tag('legend', with: { class: 'govuk-fieldset__legend--l' })
expect(html).to_not have_tag('legend', with: { class: 'govuk-fieldset__legend--xl' })
end
end

context 'with a "s" size' do
let(:size) { 's' }

specify 'the size class should be present' do
expect(html).to have_tag('legend', with: { class: 'govuk-fieldset__legend govuk-fieldset__legend--s' })
end
end

context 'with a "m" size' do
let(:size) { 'm' }

specify 'the size class should be present' do
expect(html).to have_tag('legend', with: { class: 'govuk-fieldset__legend govuk-fieldset__legend--m' })
end
end

context 'with a "l" size' do
let(:size) { 'l' }

specify 'the size class should be present' do
expect(html).to have_tag('legend', with: { class: 'govuk-fieldset__legend govuk-fieldset__legend--l' })
end
end

context 'with a "xl" size' do
let(:size) { 'xl' }

specify 'the size class should be present' do
expect(html).to have_tag('legend', with: { class: 'govuk-fieldset__legend govuk-fieldset__legend--xl' })
end
end
end

0 comments on commit 8ca528a

Please sign in to comment.