Skip to content

Commit

Permalink
Pass select list options to Rails form builder (#1505)
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron authored Oct 18, 2022
1 parent 27be01b commit 5d3448b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-stingrays-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Pass select list options to Rails form builder
2 changes: 1 addition & 1 deletion app/forms/select_list_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# :nodoc:
class SelectListForm < ApplicationForm
form do |check_form|
check_form.select_list(name: "cities", label: "Cool cities", caption: "Select your favorite!") do |city_list|
check_form.select_list(name: "cities", label: "Cool cities", caption: "Select your favorite!", include_blank: true) do |city_list|
city_list.option(label: "Lopez Island", value: "lopez_island")
city_list.option(label: "Bellevue", value: "bellevue")
city_list.option(label: "Seattle", value: "seattle")
Expand Down
10 changes: 9 additions & 1 deletion lib/primer/forms/dsl/select_list_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Forms
module Dsl
# :nodoc:
class SelectListInput < Input
SELECT_ARGUMENTS = %i[multiple disabled include_blank prompt].freeze

# :nodoc:
class Option
attr_reader :label, :value, :system_arguments
Expand All @@ -16,13 +18,19 @@ def initialize(label:, value:, **system_arguments)
end
end

attr_reader :name, :label, :options
attr_reader :name, :label, :options, :select_arguments

def initialize(name:, label:, **system_arguments)
@name = name
@label = label
@options = []

@select_arguments = {}.tap do |select_args|
SELECT_ARGUMENTS.each do |select_arg|
select_args[select_arg] = system_arguments.delete(select_arg)
end
end

super(**system_arguments)

yield(self) if block_given?
Expand Down
2 changes: 1 addition & 1 deletion lib/primer/forms/select_list.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= render(FormControl.new(input: @input)) do %>
<%= content_tag(:div, class: @field_wrap_classes) do %>
<%= builder.select(@input.name, options, {}, **@input.input_arguments) %>
<%= builder.select(@input.name, options, @input.select_arguments, **@input.input_arguments) %>
<% end %>
<% end %>
1 change: 1 addition & 0 deletions test/primer/forms/forms_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def test_radio_button_group_form
def test_select_list_form
render_preview :select_list_form

assert_selector ".FormControl-select option[value='']"
assert_selector ".FormControl-select option[value=lopez_island]"
assert_selector ".FormControl-select option[value=bellevue]"
assert_selector ".FormControl-select option[value=seattle]"
Expand Down

0 comments on commit 5d3448b

Please sign in to comment.