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 inputs to be hidden #1664

Merged
merged 2 commits into from
Dec 5, 2022
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/gorgeous-pans-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Allow form inputs to be hidden
4 changes: 2 additions & 2 deletions lib/primer/forms/check_box.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<div class="FormControl-checkbox-wrap">
<%= content_tag(:div, class: "FormControl-checkbox-wrap", hidden: @input.hidden?) do %>
<%= builder.check_box(@input.name, @input.input_arguments, checked_value, unchecked_value) %>
<span class="FormControl-checkbox-labelWrap">
<%= builder.label(@input.name, **@input.label_arguments) do %>
<%= @input.label %>
<% end %>
<%= render(Caption.new(input: @input)) %>
</span>
</div>
<% end %>
<% if @input.nested_form_block %>
<%= content_tag(:div, nested_form_arguments) do %>
<%= render(@input.nested_form_block.call(builder)) %>
Expand Down
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 }
@nested_form_arguments = { **@input.nested_form_arguments, hidden: @input.hidden? }
@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/check_box_group.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<fieldset>
<%= content_tag(:fieldset, hidden: @input.hidden?) do %>
<% if @input.label %>
<%= content_tag(:legend, **@input.label_arguments) do %>
<%= @input.label %>
Expand All @@ -9,4 +9,4 @@
<%= render(check_box.to_component) %>
<% end %>
<% end %>
</fieldset>
<% end %>
7 changes: 0 additions & 7 deletions lib/primer/forms/dsl/text_field_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ def initialize(name:, label:, **system_arguments)

add_input_classes("FormControl-inset") if inset?
add_input_classes("FormControl-monospace") if monospace?

@field_wrap_classes = class_names(
"FormControl-input-wrap",
Primer::Forms::Dsl::Input::SIZE_MAPPINGS[size],
"FormControl-input-wrap--trailingAction": show_clear_button?,
"FormControl-input-wrap--leadingVisual": leading_visual?
)
end

alias show_clear_button? show_clear_button
Expand Down
4 changes: 2 additions & 2 deletions lib/primer/forms/radio_button.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<div class="FormControl-radio-wrap">
<%= content_tag(:div, class: "FormControl-radio-wrap", hidden: @input.hidden?) do %>
<%= builder.radio_button(@input.name, @input.value, **@input.input_arguments) %>
<span class="FormControl-radio-labelWrap">
<%= builder.label(@input.name, value: @input.value, **@input.label_arguments) do %>
<%= @input.label %>
<% end %>
<%= render(Caption.new(input: @input)) %>
</span>
</div>
<% end %>
<% if @input.nested_form_block %>
<%= content_tag(:div, nested_form_arguments) do %>
<%= render(@input.nested_form_block.call(builder)) %>
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 }
@nested_form_arguments = { **@input.nested_form_arguments, hidden: @input.hidden? }
@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/radio_button_group.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<fieldset>
<%= content_tag(:fieldset, hidden: @input.hidden?) do %>
<% if @input.label %>
<%= content_tag(:legend, **@input.label_arguments) do %>
<%= @input.label %>
Expand All @@ -9,4 +9,4 @@
<%= render(radio_button.to_component) %>
<% end %>
<% end %>
</fieldset>
<% end %>
2 changes: 1 addition & 1 deletion lib/primer/forms/select_list.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= render(FormControl.new(input: @input)) do %>
<%= content_tag(:div, class: @field_wrap_classes) do %>
<%= content_tag(:div, **@field_wrap_arguments) do %>
<%= builder.select(@input.name, options, @input.select_arguments, **@input.input_arguments) %>
<% end %>
<% end %>
5 changes: 4 additions & 1 deletion lib/primer/forms/select_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def initialize(input:)
"FormControl--medium"
)

@field_wrap_classes = class_names("FormControl-select-wrap")
@field_wrap_arguments = {
class: "FormControl-select-wrap",
hidden: @input.hidden?
}
end

def options
Expand Down
2 changes: 1 addition & 1 deletion lib/primer/forms/text_area.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= render(FormControl.new(input: @input)) do %>
<%= content_tag(:div, class: @field_wrap_classes) do %>
<%= content_tag(:div, **@field_wrap_arguments) do %>
<%= builder.text_area(@input.name, **@input.input_arguments) %>
<% end %>
<% end %>
6 changes: 5 additions & 1 deletion lib/primer/forms/text_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class TextArea < BaseComponent
def initialize(input:)
@input = input
@input.add_input_classes("FormControl-input", "FormControl--medium")
@field_wrap_classes = class_names("FormControl-input-wrap")

@field_wrap_arguments = {
class: class_names("FormControl-input-wrap"),
hidden: @input.hidden?
}
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/primer/forms/text_field.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= render(FormControl.new(input: @input)) do %>
<% if @input.leading_visual || @input.show_clear_button? %>
<%= content_tag(:div, class: @input.field_wrap_classes) do %>
<%= content_tag(:div, **@field_wrap_arguments) do %>
<% if @input.leading_visual %>
<span class="FormControl-input-leadingVisualWrap">
<%= render(Primer::OcticonComponent.new(**@input.leading_visual)) %>
Expand Down
11 changes: 11 additions & 0 deletions lib/primer/forms/text_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ class TextField < BaseComponent

def initialize(input:)
@input = input

@field_wrap_arguments = {
class: class_names(
"FormControl-input-wrap",
Primer::Forms::Dsl::Input::SIZE_MAPPINGS[@input.size],
"FormControl-input-wrap--trailingAction": @input.show_clear_button?,
"FormControl-input-wrap--leadingVisual": @input.leading_visual?
),

hidden: @input.hidden?
}
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions test/lib/primer/forms/checkbox_group_input_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "lib/test_helper"

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

class HiddenCheckboxGroupForm < ApplicationForm
form do |check_form|
check_form.check_box_group(label: "Foobar", hidden: true) do |check_group|
check_group.check_box(name: :foo, label: "Foo")
end
end
end

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

assert_selector "fieldset", visible: false
assert_selector ".FormControl-checkbox-wrap", visible: false
end
end
18 changes: 17 additions & 1 deletion test/lib/primer/forms/checkbox_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Primer::Forms::CheckboxInputTest < Minitest::Test

class BadCheckboxesForm < ApplicationForm
form do |check_form|
check_form.check_box(name: "alpha", label: "Alpha", scheme: :array)
check_form.check_box(name: :alpha, label: "Alpha", scheme: :array)
end
end

Expand All @@ -22,4 +22,20 @@ def test_array_checkboxes_require_values

assert_includes error.message, "Check box needs an explicit value if scheme is array"
end

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

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

assert_selector ".FormControl-checkbox-wrap", visible: false
end
end
45 changes: 45 additions & 0 deletions test/lib/primer/forms/radio_button_input_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require "lib/test_helper"

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

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)
end
end
end

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

assert_selector "fieldset"
assert_selector ".FormControl-radio-wrap", visible: false
end

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

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

assert_selector "fieldset", visible: false
assert_selector ".FormControl-radio-wrap", visible: false
end
end
25 changes: 25 additions & 0 deletions test/lib/primer/forms/select_list_input_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require "lib/test_helper"

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

class HiddenSelectListForm < ApplicationForm
form do |select_list_form|
select_list_form.select_list(name: :foo, label: "Foo") do |list|
list.option(label: "Foo", value: "foo")
end
end
end

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

assert_selector ".FormControl", visible: false
end
end
23 changes: 23 additions & 0 deletions test/lib/primer/forms/text_area_input_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "lib/test_helper"

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

class HiddenTextAreaForm < ApplicationForm
form do |text_field_form|
text_field_form.text_area(name: :foo, label: "Foo")
end
end

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

assert_selector ".FormControl", visible: false
end
end
23 changes: 23 additions & 0 deletions test/lib/primer/forms/text_field_input_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "lib/test_helper"

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

class HiddenTextFieldForm < ApplicationForm
form do |text_field_form|
text_field_form.text_field(name: :foo, label: "Foo")
end
end

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

assert_selector ".FormControl", visible: false
end
end