Skip to content

Commit a574b0e

Browse files
authoredNov 25, 2024··
Prevent hidden inputs from taking up space on the page (#3202)
1 parent 5d9fab3 commit a574b0e

File tree

5 files changed

+21
-1
lines changed

5 files changed

+21
-1
lines changed
 

‎.changeset/two-badgers-suffer.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/view-components': patch
3+
---
4+
5+
Prevent hidden inputs from taking up space on the page

‎app/forms/horizontal_form.rb

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# :nodoc:
44
class HorizontalForm < ApplicationForm
55
form do |my_form|
6+
my_form.hidden(name: :token, value: "abc123")
7+
68
my_form.group(layout: :horizontal) do |name_group|
79
name_group.text_field(
810
name: :first_name,

‎app/lib/primer/forms/dsl/hidden_input.rb

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def type
2727
def supports_validation?
2828
false
2929
end
30+
31+
def hidden?
32+
true
33+
end
3034
end
3135
end
3236
end

‎app/lib/primer/forms/group.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ def initialize(inputs:, builder:, form:, layout: DEFAULT_LAYOUT, **system_argume
1818
@layout = layout
1919
@system_arguments = system_arguments
2020

21+
@system_arguments[:display] = :none if inputs.all?(&:hidden?)
22+
2123
@system_arguments[:classes] = class_names(
2224
@system_arguments.delete(:classes),
23-
"FormControl-horizontalGroup" => horizontal?
25+
"FormControl-horizontalGroup" => horizontal?,
26+
"FormControl-spacingWrapper" => !horizontal? && inputs.size > 1
2427
)
2528
end
2629

‎test/lib/primer/forms_test.rb

+6
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ def test_renders_horizontal_group
172172
assert_selector ".FormControl-horizontalGroup .FormControl", count: 2
173173
end
174174

175+
def test_hidden_items_wrapped_in_display_none
176+
render_preview :horizontal_form
177+
178+
assert_selector ".d-none input[type=hidden]", visible: :hidden
179+
end
180+
175181
def test_renders_multi_input
176182
render_preview :multi_input_form
177183

0 commit comments

Comments
 (0)
Please sign in to comment.