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 form groups to accept system arguments #3149

Merged
merged 1 commit 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/hip-deers-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': minor
---

Allow form groups to accept system arguments
8 changes: 0 additions & 8 deletions app/lib/primer/forms/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ def compile_and_render_template
self.class.compile! unless self.class.instance_methods(false).include?(:render_template)
render_template
end

def content_tag_if(condition, tag, **kwargs, &block)
if condition
content_tag(tag, **kwargs, &block)
else
capture(&block)
end
end
end
end
end
2 changes: 1 addition & 1 deletion app/lib/primer/forms/group.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= content_tag_if(horizontal?, :div, class: "FormControl-horizontalGroup") do %>
<%= render(Primer::Box.new(**@system_arguments)) do %>
<% @inputs.each do |input| %>
<%= render(input.to_component) %>
<% end %>
Expand Down
5 changes: 5 additions & 0 deletions app/lib/primer/forms/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def initialize(inputs:, builder:, form:, layout: DEFAULT_LAYOUT, **system_argume
@form = form
@layout = layout
@system_arguments = system_arguments

@system_arguments[:classes] = class_names(
@system_arguments.delete(:classes),
"FormControl-horizontalGroup" => horizontal?
)
end

def horizontal?
Expand Down
22 changes: 22 additions & 0 deletions test/lib/primer/forms/group_input_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "lib/test_helper"

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

def test_group_accepts_system_arguments
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render_inline_form(f) do |sys_args_form|
sys_args_form.group(layout: :horizontal, border: true, p: 1) do |group|
group.text_field(name: :first_name, label: "First name")
group.text_field(name: :last_name, label: "Last name")
end
end
end
end

assert_selector ".FormControl-horizontalGroup.border.p-1"
end
end
6 changes: 0 additions & 6 deletions test/lib/primer/forms_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,6 @@ def test_text_field_custom_element_is_form_control
assert_selector "primer-text-field.FormControl"
end

def test_siblings_are_form_controls_when_including_a_multi_input
render_preview :multi_input_form

assert_selector ".FormControl-radio-group-wrap + .FormControl"
end

def test_toggle_switch_button_labelled_by_label
render_preview(:example_toggle_switch_form)

Expand Down
Loading