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

Fix width of text fields and multi inputs #1897

Merged
merged 4 commits into from
Mar 23, 2023
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/tough-bats-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Fix widths of text fields and multi inputs.
4 changes: 2 additions & 2 deletions app/forms/select_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# :nodoc:
class SelectForm < ApplicationForm
form do |check_form|
check_form.select_list(name: "cities", label: "Cool cities", caption: "Select your favorite!", include_blank: true) do |city_list|
form do |select_form|
select_form.select_list(name: "cities", label: "Cool cities", caption: "Select your favorite!", include_blank: true) do |city_list|
city_list.option(label: "Lopez Island", value: "lopez_island")
city_list.option(label: "Bellevue", value: "bellevue")
city_list.option(label: "Seattle", value: "seattle")
Expand Down
2 changes: 1 addition & 1 deletion lib/primer/forms/form_control.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if @input.form_control? %>
<%= content_tag(:div, **@form_group_arguments) do %>
<%= content_tag(@tag, **@form_group_arguments) do %>
<% if @input.label %>
<%= builder.label(@input.name, **@input.label_arguments) do %>
<%= @input.label %>
Expand Down
6 changes: 5 additions & 1 deletion lib/primer/forms/form_control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ module Forms
class FormControl < BaseComponent
delegate :builder, :form, to: :@input

def initialize(input:)
def initialize(input:, tag: :div, **system_arguments)
@input = input
@tag = tag
@input.add_label_classes("FormControl-label")
@form_group_arguments = {
**system_arguments,
class: class_names(
system_arguments[:class],
system_arguments[:classes],
"FormControl",
"width-full",
"FormControl--fullWidth" => @input.full_width?
Expand Down
14 changes: 6 additions & 8 deletions lib/primer/forms/multi.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<%= content_tag(:div, **@input.input_arguments) do %>
<%= render(FormControl.new(input: @input)) do %>
<primer-multi-input data-name="<%= @input.name %>">
<% @input.inputs.each do |child_input| %>
<%= render(child_input.to_component) %>
<% end %>
</primer-multi-input>
<% end %>
<%= render(FormControl.new(input: @input, **@input.input_arguments)) do %>
<primer-multi-input data-name="<%= @input.name %>">
<% @input.inputs.each do |child_input| %>
<%= render(child_input.to_component) %>
<% end %>
</primer-multi-input>
<% end %>
32 changes: 15 additions & 17 deletions lib/primer/forms/text_field.html.erb
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<primer-text-field>
<%= render(FormControl.new(input: @input)) do %>
<%= content_tag(:div, **@field_wrap_arguments) do %>
<% if @input.leading_visual %>
<span class="FormControl-input-leadingVisualWrap">
<%= render(Primer::Beta::Octicon.new(**@input.leading_visual)) %>
</span>
<% end %>
<%= render Primer::ConditionalWrapper.new(condition: @input.auto_check_src, tag: "auto-check", csrf: auto_check_authenticity_token, src: @input.auto_check_src) do %>
<%= builder.text_field(@input.name, **@input.input_arguments) %>
<% end %>
<% if @input.show_clear_button? %>
<button type="button" id="<%= @input.clear_button_id %>" class="FormControl-input-trailingAction" aria-label="Clear" data-action="click:primer-text-field#clearContents">
<%= render(Primer::Beta::Octicon.new(icon: :"x-circle-fill")) %>
</button>
<% end %>
<%= render(FormControl.new(input: @input, tag: :"primer-text-field")) do %>
<%= content_tag(:div, **@field_wrap_arguments) do %>
<% if @input.leading_visual %>
<span class="FormControl-input-leadingVisualWrap">
<%= render(Primer::Beta::Octicon.new(**@input.leading_visual)) %>
</span>
<% end %>
<%= render Primer::ConditionalWrapper.new(condition: @input.auto_check_src, tag: "auto-check", csrf: auto_check_authenticity_token, src: @input.auto_check_src) do %>
<%= builder.text_field(@input.name, **@input.input_arguments) %>
<% end %>
<% if @input.show_clear_button? %>
<button type="button" id="<%= @input.clear_button_id %>" class="FormControl-input-trailingAction" aria-label="Clear" data-action="click:primer-text-field#clearContents">
<%= render(Primer::Beta::Octicon.new(icon: :"x-circle-fill")) %>
</button>
<% end %>
<% end %>
</primer-text-field>
<% end %>
12 changes: 12 additions & 0 deletions test/lib/primer/forms/forms_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,16 @@ def test_renders_field_name_with_question_mark_caption_template
assert_selector "input[name=enabled]"
assert_selector ".my-test-caption"
end

def test_text_field_custom_element_is_form_control
render_preview :single_text_field_form

assert_selector "primer-text-field.FormControl"
end

def test_siblings_are_form_controls_when_including_a_multi_input
render_preview :multi_input_form

assert_selector ".FormControl-radio-group-wrap + .FormControl"
end
end