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 6d4f042
Show file tree
Hide file tree
Showing 2 changed files with 14 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
12 changes: 12 additions & 0 deletions spec/support/shared/shared_legend_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
context 'valid sizes' do
%w(s m l xl).each do |size|
let(:size) { size }

context %(when the legend size is #{size}) do
subject { builder.send(*args, legend: { text: legend_text, size: }) }

Expand All @@ -53,6 +54,17 @@
end
end
end

context 'when the legend size is nil' do
subject { builder.send(*args, legend: { text: legend_text, size: nil }) }

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

context 'with an invalid size' do
Expand Down

0 comments on commit 6d4f042

Please sign in to comment.