Skip to content

Commit

Permalink
Merge pull request #4588 from alphagov/add-form-attribute-to-button-c…
Browse files Browse the repository at this point in the history
…omponent

Add a `form` attribute to the button component
  • Loading branch information
pezholio authored Jan 29, 2025
2 parents eb40487 + d3f7eb6 commit 9fd883d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
useful summary for people upgrading their application, not a replication
of the commit log.

## Unreleased

* Add a `form` attribute to the button component ([PR #4588](https://github.com/alphagov/govuk_publishing_components/pull/4588))

## 51.0.0

* Remove title component margin_top option ([PR #4508](https://github.com/alphagov/govuk_publishing_components/pull/4508))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,12 @@ examples:
data:
text: Button
aria_describedby: with_aria_describedby
with_form_attribute:
description: |
Buttons will usually be contained within their containing form, but sometimes it is desirable to have the button
outside the form with the `form` attribute pointing to the ID of the form. See
[The Mozilla dev docs on a button's `form` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#form)
for more information.
data:
text: With `form` argument
form: some_form_id
5 changes: 4 additions & 1 deletion lib/govuk_publishing_components/presenters/button_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class ButtonHelper
:classes,
:aria_label,
:aria_controls,
:aria_describedby
:aria_describedby,
:form

def initialize(local_assigns)
@disable_ga4 = local_assigns[:disable_ga4]
Expand Down Expand Up @@ -58,6 +59,7 @@ def initialize(local_assigns)
@button_id = "button-id-#{SecureRandom.hex(4)}"
@aria_controls = local_assigns[:aria_controls]
@aria_describedby = local_assigns[:aria_describedby]
@form = local_assigns[:form]

if local_assigns.include?(:classes)
@classes = local_assigns[:classes].split(" ")
Expand Down Expand Up @@ -106,6 +108,7 @@ def html_options
options[:aria][:controls] = aria_controls if aria_controls
options[:aria][:describedby] = aria_describedby if aria_describedby
options[:draggable] = false if link?
options[:form] = form if form
options
end

Expand Down
6 changes: 6 additions & 0 deletions spec/components/button_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,10 @@ def component_name

assert_select '.gem-c-button[aria-describedby="Testing aria-describedby"]'
end

it "renders with a form attribute" do
render_component(text: "Button", form: "some_form_id")

assert_select '.gem-c-button[form="some_form_id"]'
end
end

0 comments on commit 9fd883d

Please sign in to comment.