Skip to content

Commit

Permalink
Ensure nested forms can be hidden independently of their radio button…
Browse files Browse the repository at this point in the history
… or check box (#1673)
  • Loading branch information
camertron authored Dec 5, 2022
1 parent 297f733 commit 7a26cbb
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-pugs-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Ensure nested forms can be hidden independently of their radio button or check box
2 changes: 1 addition & 1 deletion lib/primer/forms/check_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(input:)
def nested_form_arguments
return @nested_form_arguments if defined?(@nested_form_arguments)

@nested_form_arguments = { **@input.nested_form_arguments, hidden: @input.hidden? }
@nested_form_arguments = { hidden: @input.hidden?, **@input.nested_form_arguments }
@nested_form_arguments[:class] = class_names(
@nested_form_arguments[:class],
@nested_form_arguments.delete(:classes),
Expand Down
4 changes: 2 additions & 2 deletions lib/primer/forms/dsl/input_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def hidden(**options)
add_input HiddenInput.new(builder: builder, form: form, **options)
end

def check_box(**options)
add_input CheckBoxInput.new(builder: builder, form: form, **options)
def check_box(**options, &block)
add_input CheckBoxInput.new(builder: builder, form: form, **options, &block)
end

def radio_button_group(**options, &block)
Expand Down
2 changes: 1 addition & 1 deletion lib/primer/forms/radio_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(input:)
def nested_form_arguments
return @nested_form_arguments if defined?(@nested_form_arguments)

@nested_form_arguments = { **@input.nested_form_arguments, hidden: @input.hidden? }
@nested_form_arguments = { hidden: @input.hidden?, **@input.nested_form_arguments }
@nested_form_arguments[:class] = class_names(
@nested_form_arguments[:class],
@nested_form_arguments.delete(:classes),
Expand Down
39 changes: 37 additions & 2 deletions test/lib/primer/forms/checkbox_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ def test_array_checkboxes_require_values
assert_includes error.message, "Check box needs an explicit value if scheme is array"
end

class NestedForm < ApplicationForm
form do |nested_form|
nested_form.text_field(
name: :bar,
label: "Bar"
)
end
end

class HiddenCheckboxForm < ApplicationForm
form do |check_form|
check_form.check_box(name: :foo, label: "Foo", hidden: true)
check_form.check_box(name: :foo, label: "Foo", hidden: true) do |foo_check|
foo_check.nested_form do |builder|
NestedForm.new(builder)
end
end
end
end

Expand All @@ -36,6 +49,28 @@ def test_hidden_checkbox
end
end

assert_selector ".FormControl-checkbox-wrap", visible: false
assert_selector "input[name=foo]", visible: false
assert_selector "input[name=bar]", visible: false
end

class CheckboxWithHiddenNestedForm < ApplicationForm
form do |check_form|
check_form.check_box(name: :foo, label: "Foo") do |foo_check|
foo_check.nested_form(hidden: true) do |builder|
NestedForm.new(builder)
end
end
end
end

def test_nested_form_can_be_hidden_independently
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render(CheckboxWithHiddenNestedForm.new(f))
end
end

assert_selector "input[name=foo]"
assert_selector "input[name=bar]", visible: false
end
end
43 changes: 40 additions & 3 deletions test/lib/primer/forms/radio_button_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@
class Primer::Forms::RadioButtonInputTest < Minitest::Test
include Primer::ComponentTestHelpers

class NestedForm < ApplicationForm
form do |nested_form|
nested_form.text_field(
name: :bar,
label: "Bar"
)
end
end

class HiddenRadioButtonForm < ApplicationForm
form do |radio_form|
radio_form.radio_button_group(name: :foos, label: "Foos") do |radio_group|
radio_group.radio_button(name: :foo, label: "Foo", value: "foo", hidden: true)
radio_group.radio_button(name: :foo, label: "Foo", value: "foo", hidden: true) do |radio_button|
radio_button.nested_form do |builder|
NestedForm.new(builder)
end
end
end
end
end
Expand All @@ -21,7 +34,31 @@ def test_hidden_radio_button
end

assert_selector "fieldset"
assert_selector ".FormControl-radio-wrap", visible: false
assert_selector "input[name=foo]", visible: false
assert_selector "input[name=bar]", visible: false
end

class RadioButtonWithHiddenNestedForm < ApplicationForm
form do |radio_form|
radio_form.radio_button_group(name: :foos, label: "Foos") do |radio_group|
radio_group.radio_button(name: :foo, label: "Foo", value: "foo") do |radio_button|
radio_button.nested_form(hidden: true) do |builder|
NestedForm.new(builder)
end
end
end
end
end

def test_nested_form_can_be_hidden_independently
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render(RadioButtonWithHiddenNestedForm.new(f))
end
end

assert_selector "input[name=foo]"
assert_selector "input[name=bar]", visible: false
end

class HiddenRadioButtonGroupForm < ApplicationForm
Expand All @@ -40,6 +77,6 @@ def test_hidden_radio_button_group
end

assert_selector "fieldset", visible: false
assert_selector ".FormControl-radio-wrap", visible: false
assert_selector "input[name=foo]", visible: false
end
end

0 comments on commit 7a26cbb

Please sign in to comment.