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

Prevent hidden inputs from taking up space on the page #3202

Merged
merged 2 commits into from
Nov 25, 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/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
Loading