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

Allow setting custom values on submit buttons #3141

Merged
merged 2 commits into from
Oct 14, 2024
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
5 changes: 5 additions & 0 deletions .changeset/cuddly-games-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Allow setting custom values on submit buttons.
7 changes: 5 additions & 2 deletions app/lib/primer/forms/button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ def input_arguments
private

def tag_attributes
attrs = { name: @input.name }
attrs[:value] = @input.value if @input.value

case @type
when :submit
ButtonAttributeGenerator.submit_tag_attributes(@input.label, name: @input.name)
ButtonAttributeGenerator.submit_tag_attributes(@input.label, **attrs)
else
ButtonAttributeGenerator.button_tag_attributes(@input.label, name: @input.name)
ButtonAttributeGenerator.button_tag_attributes(@input.label, **attrs)
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion app/lib/primer/forms/dsl/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ def id
@input_arguments[:id]
end

def value
@input_arguments[:value]
end

# :nocov:
def name
raise_for_abstract_method!(__method__)
Expand Down Expand Up @@ -305,7 +309,7 @@ def input_data
def caption_template_name
return nil unless name

@caption_template_name ||= if respond_to?(:value)
@caption_template_name ||= if respond_to?(:value) && value.present?
:"#{name}_#{value}"
else
name.to_sym
Expand Down
31 changes: 31 additions & 0 deletions test/lib/primer/forms/submit_button_input_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "lib/test_helper"

class Primer::Forms::SubmitButtonInputTest < Minitest::Test
include Primer::ComponentTestHelpers

def test_uses_name_as_value_by_default
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render_inline_form(f) do |submit_button_form|
submit_button_form.submit(name: :foo, label: "Foo")
end
end
end

assert_selector "button[type=submit][value=Foo]"
end

def test_allows_overriding_value
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render_inline_form(f) do |submit_button_form|
submit_button_form.submit(name: :foo, label: "Foo", value: "bar")
end
end
end

assert_selector "button[type=submit][value=bar]"
end
end
Loading