Skip to content

Commit 37e78c0

Browse files
authoredOct 14, 2024··
Allow setting custom values on submit buttons (#3141)
1 parent c8b9b4b commit 37e78c0

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed
 

‎.changeset/cuddly-games-prove.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/view-components': patch
3+
---
4+
5+
Allow setting custom values on submit buttons.

‎app/lib/primer/forms/button.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,14 @@ def input_arguments
5454
private
5555

5656
def tag_attributes
57+
attrs = { name: @input.name }
58+
attrs[:value] = @input.value if @input.value
59+
5760
case @type
5861
when :submit
59-
ButtonAttributeGenerator.submit_tag_attributes(@input.label, name: @input.name)
62+
ButtonAttributeGenerator.submit_tag_attributes(@input.label, **attrs)
6063
else
61-
ButtonAttributeGenerator.button_tag_attributes(@input.label, name: @input.name)
64+
ButtonAttributeGenerator.button_tag_attributes(@input.label, **attrs)
6265
end
6366
end
6467
end

‎app/lib/primer/forms/dsl/input.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ def id
246246
@input_arguments[:id]
247247
end
248248

249+
def value
250+
@input_arguments[:value]
251+
end
252+
249253
# :nocov:
250254
def name
251255
raise_for_abstract_method!(__method__)
@@ -305,7 +309,7 @@ def input_data
305309
def caption_template_name
306310
return nil unless name
307311

308-
@caption_template_name ||= if respond_to?(:value)
312+
@caption_template_name ||= if respond_to?(:value) && value.present?
309313
:"#{name}_#{value}"
310314
else
311315
name.to_sym
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require "lib/test_helper"
4+
5+
class Primer::Forms::SubmitButtonInputTest < Minitest::Test
6+
include Primer::ComponentTestHelpers
7+
8+
def test_uses_name_as_value_by_default
9+
render_in_view_context do
10+
primer_form_with(url: "/foo") do |f|
11+
render_inline_form(f) do |submit_button_form|
12+
submit_button_form.submit(name: :foo, label: "Foo")
13+
end
14+
end
15+
end
16+
17+
assert_selector "button[type=submit][value=Foo]"
18+
end
19+
20+
def test_allows_overriding_value
21+
render_in_view_context do
22+
primer_form_with(url: "/foo") do |f|
23+
render_inline_form(f) do |submit_button_form|
24+
submit_button_form.submit(name: :foo, label: "Foo", value: "bar")
25+
end
26+
end
27+
end
28+
29+
assert_selector "button[type=submit][value=bar]"
30+
end
31+
end

0 commit comments

Comments
 (0)
Please sign in to comment.