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

Pass select list options to Rails form builder #1505

Merged
merged 2 commits into from
Oct 18, 2022
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/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