Skip to content

Commit

Permalink
Added "width" attribute to inputs
Browse files Browse the repository at this point in the history
- TextField
- Select
  • Loading branch information
apfohl committed Nov 9, 2023
1 parent fa2a288 commit 21bff2a
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 5 deletions.
10 changes: 10 additions & 0 deletions .changeset/added-width-to-inputs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@openproject/primer-view-components': minor
---

Added a "width" attribute to control how wide an input is rendered on screen.

It is active for:

- TextField
- Select
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ tmp/
# Simplecov folder
coverage/
demo/public/assets/

# IDE folders
.idea
27 changes: 27 additions & 0 deletions app/components/primer/alpha/text_field.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,36 @@
gap: var(--base-size-16);
}

/* widths */
@define-mixin FormControl-input-width {
&.FormControl-full {
width: 100%;
}

&.FormControl-wide {
width: 72rem;
}

&.FormControl-half {
width: 48rem;
}

&.FormControl-narrow {
width: 32rem;
}

&.FormControl-slim {
width: 16rem;
}
}

/* positioning for leading/trailing items for TextInput */
.FormControl-input-wrap {
position: relative;
display: grid;

@mixin FormControl-input-width;

& .FormControl-input-leadingVisualWrap {
position: absolute;
top: var(--base-size-8);
Expand Down Expand Up @@ -479,6 +504,8 @@
display: grid;
grid-template-columns: minmax(0, auto) var(--base-size-16);

@mixin FormControl-input-width;

/* mask allows for background-color to respect themes */
&::after {
width: var(--base-size-16);
Expand Down
6 changes: 4 additions & 2 deletions app/forms/multi_text_field_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class MultiTextFieldForm < ApplicationForm
name: :first_name,
label: "First name",
required: true,
caption: "That which we call a rose by any other name would smell as sweet."
caption: "That which we call a rose by any other name would smell as sweet.",
width: :narrow
)

my_form.separator
Expand All @@ -16,7 +17,8 @@ class MultiTextFieldForm < ApplicationForm
name: :last_name,
label: "Last name",
required: true,
caption: "Bueller. Bueller. Bueller."
caption: "Bueller. Bueller. Bueller.",
width: :half
)

my_form.hidden(
Expand Down
2 changes: 1 addition & 1 deletion app/forms/select_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# :nodoc:
class SelectForm < ApplicationForm
form do |select_form|
select_form.select_list(name: "cities", label: "Cool cities", caption: "Select your favorite!", include_blank: true) do |city_list|
select_form.select_list(name: "cities", label: "Cool cities", caption: "Select your favorite!", include_blank: true, width: :narrow) 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
3 changes: 2 additions & 1 deletion app/forms/single_text_field_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class SingleTextFieldForm < ApplicationForm
name: :ultimate_answer,
label: "Ultimate answer",
required: true,
caption: "The answer to life, the universe, and everything"
caption: "The answer to life, the universe, and everything",
width: :narrow
)
end
end
14 changes: 14 additions & 0 deletions lib/primer/forms/dsl/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ class Input
}.freeze
SIZE_OPTIONS = SIZE_MAPPINGS.keys

DEFAULT_WIDTH = :full
WIDTH_MAPPINGS = {

Check failure on line 49 in lib/primer/forms/dsl/input.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/MutableConstant: Freeze mutable objects assigned to constants.
:slim => "FormControl-slim",
:narrow => "FormControl-narrow",
:half => "FormControl-half",
:wide => "FormControl-wide",
DEFAULT_WIDTH => "FormControl-full"
}

include Primer::ClassNameHelper

attr_reader :builder, :form, :input_arguments, :label_arguments, :caption, :validation_message, :ids, :form_control, :base_id
Expand Down Expand Up @@ -73,6 +82,7 @@ def initialize(builder:, form:, **system_arguments)
@invalid = @input_arguments.delete(:invalid)
@full_width = @input_arguments.delete(:full_width)
@size = @input_arguments.delete(:size)
@width = @input_arguments.delete(:width)

# If scope_name_to_model is false, the name of the input for eg. `my_field`
# will be `my_field` instead of the Rails default of `model[my_field]`.
Expand Down Expand Up @@ -226,6 +236,10 @@ def size
@size ||= SIZE_MAPPINGS.include?(@size) ? @size : DEFAULT_SIZE
end

def width
@width ||= WIDTH_MAPPINGS.include?(@width) ? @width : DEFAULT_WIDTH
end

def validation_messages
@validation_messages ||=
if validation_message
Expand Down
5 changes: 4 additions & 1 deletion lib/primer/forms/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ def initialize(input:)
)

@field_wrap_arguments = {
class: "FormControl-select-wrap",
class: class_names(
"FormControl-select-wrap",
Primer::Forms::Dsl::Input::WIDTH_MAPPINGS[@input.width]
),
hidden: @input.hidden?
}
end
Expand Down
1 change: 1 addition & 0 deletions lib/primer/forms/text_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def initialize(input:)
class: class_names(
"FormControl-input-wrap",
INPUT_WRAP_SIZE[input.size],
Primer::Forms::Dsl::Input::WIDTH_MAPPINGS[@input.width],
"FormControl-input-wrap--trailingAction": @input.show_clear_button?,
"FormControl-input-wrap--leadingVisual": @input.leading_visual?
),
Expand Down

0 comments on commit 21bff2a

Please sign in to comment.