Skip to content

Commit

Permalink
Prevent hidden inputs from taking up space on the page (#3202)
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron authored Nov 25, 2024
1 parent 5d9fab3 commit a574b0e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/two-badgers-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Prevent hidden inputs from taking up space on the page
2 changes: 2 additions & 0 deletions app/forms/horizontal_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# :nodoc:
class HorizontalForm < ApplicationForm
form do |my_form|
my_form.hidden(name: :token, value: "abc123")

my_form.group(layout: :horizontal) do |name_group|
name_group.text_field(
name: :first_name,
Expand Down
4 changes: 4 additions & 0 deletions app/lib/primer/forms/dsl/hidden_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def type
def supports_validation?
false
end

def hidden?
true
end
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion app/lib/primer/forms/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ def initialize(inputs:, builder:, form:, layout: DEFAULT_LAYOUT, **system_argume
@layout = layout
@system_arguments = system_arguments

@system_arguments[:display] = :none if inputs.all?(&:hidden?)

@system_arguments[:classes] = class_names(
@system_arguments.delete(:classes),
"FormControl-horizontalGroup" => horizontal?
"FormControl-horizontalGroup" => horizontal?,
"FormControl-spacingWrapper" => !horizontal? && inputs.size > 1
)
end

Expand Down
6 changes: 6 additions & 0 deletions test/lib/primer/forms_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ def test_renders_horizontal_group
assert_selector ".FormControl-horizontalGroup .FormControl", count: 2
end

def test_hidden_items_wrapped_in_display_none
render_preview :horizontal_form

assert_selector ".d-none input[type=hidden]", visible: :hidden
end

def test_renders_multi_input
render_preview :multi_input_form

Expand Down

0 comments on commit a574b0e

Please sign in to comment.