Skip to content

Commit

Permalink
Allow setting custom values on submit buttons (#3141)
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron authored Oct 14, 2024
1 parent c8b9b4b commit 37e78c0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
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

0 comments on commit 37e78c0

Please sign in to comment.