Class: Polaris::TextFieldComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/polaris/text_field_component.rb

Defined Under Namespace

Classes: Affix

Constant Summary collapse

TYPE_DEFAULT =
:text
TYPE_OPTIONS =
%i[
  text number email password search tel url date
  datetime_local month time week currency
]
ALIGN_DEFAULT =
:default
ALIGN_MAPPINGS =
{
  ALIGN_DEFAULT => "",
  :left => "Polaris-TextField__Input--alignLeft",
  :center => "Polaris-TextField__Input--alignCenter",
  :right => "Polaris-TextField__Input--alignRight"
}
ALIGN_OPTIONS =
ALIGN_MAPPINGS.keys

Constants included from ViewHelper

ViewHelper::POLARIS_HELPERS, ViewHelper::POLARIS_TEXT_STYLES

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ViewHelper

#polaris_body_styles, #polaris_html_classes, #polaris_html_styles, #polaris_icon_source

Methods included from StylesListHelper

#styles_list

Methods included from OptionHelper

#append_option, #prepend_option

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #fetch_or_fallback_nested

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(form: nil, attribute: nil, name: nil, value: nil, type: TYPE_DEFAULT, placeholder: nil, maxlength: nil, minlength: nil, step: 1, min: 0, max: 1_000_000, rows: 2, prefix: nil, suffix: nil, multiline: false, show_character_count: false, clear_button: false, monospaced: false, align: ALIGN_DEFAULT, label: nil, label_hidden: false, label_action: nil, disabled: false, readonly: false, required: false, help_text: nil, error: false, clear_errors_on_focus: false, wrapper_arguments: {}, input_options: {}, **system_arguments) ⇒ TextFieldComponent

Returns a new instance of TextFieldComponent.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/components/polaris/text_field_component.rb', line 27

def initialize(
  form: nil,
  attribute: nil,
  name: nil,
  value: nil,
  type: TYPE_DEFAULT,
  placeholder: nil,
  maxlength: nil,
  minlength: nil,
  step: 1,
  min: 0,
  max: 1_000_000,
  rows: 2,
  prefix: nil,
  suffix: nil,
  multiline: false,
  show_character_count: false,
  clear_button: false,
  monospaced: false,
  align: ALIGN_DEFAULT,
  label: nil,
  label_hidden: false,
  label_action: nil,
  disabled: false,
  readonly: false,
  required: false,
  help_text: nil,
  error: false,
  clear_errors_on_focus: false,
  wrapper_arguments: {},
  input_options: {},
  **system_arguments
)
  @form = form
  @attribute = attribute
  @name = name
  @value = value
  @type = fetch_or_fallback(TYPE_OPTIONS, type, TYPE_DEFAULT)
  @placeholder = placeholder
  @maxlength = maxlength
  @minlength = minlength
  @step = step
  @min = min
  @max = max
  @rows = rows
  @prefix = prefix
  @suffix = suffix
  @multiline = multiline
  @show_character_count = show_character_count
  @clear_button = clear_button
  @monospaced = monospaced
  @align_class = ALIGN_MAPPINGS[fetch_or_fallback(ALIGN_OPTIONS, align, ALIGN_DEFAULT)]
  @label = label
  @label_hidden = label_hidden
  @label_action = label_action
  @disabled = disabled
  @readonly = readonly
  @required = required
  @help_text = help_text
  @error = error
  @clear_errors_on_focus = clear_errors_on_focus
  @wrapper_arguments = wrapper_arguments
  @input_options = input_options
  @system_arguments = system_arguments
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



20
21
22
# File 'app/components/polaris/text_field_component.rb', line 20

def value
  @value
end

Instance Method Details

#character_countObject



187
188
189
# File 'app/components/polaris/text_field_component.rb', line 187

def character_count
  @character_count ||= CharacterCount.new(text_field: self, max_length: @maxlength)
end

#inputObject



172
173
174
175
176
177
178
179
180
181
# File 'app/components/polaris/text_field_component.rb', line 172

def input
  case @type
  when :text
    @multiline ? "text_area" : "text_field"
  when :tel then "telephone_field"
  when :currency then "text_field"
  else
    "#{@type}_field"
  end
end

#input_optionsObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/components/polaris/text_field_component.rb', line 133

def input_options
  default_options = {
    value: @value,
    disabled: @disabled,
    readonly: @readonly,
    required: @required,
    placeholder: @placeholder,
    maxlength: @maxlength,
    minlength: @minlength,
    data: {
      polaris_text_field_target: "input"
    }
  }
  if @type == :number
    default_options.merge!({
      step: @step,
      min: @min,
      max: @max
    })
  end
  if @multiline
    default_options[:rows] = @rows
  end

  default_options.deep_merge(@input_options).compact.tap do |opts|
    opts[:class] = class_names(
      opts[:class],
      "Polaris-TextField__Input",
      @align_class,
      "Polaris-TextField--monospaced": @monospaced,
      "Polaris-TextField__Input--suffixed": @suffix.present?
    )
    if @clear_errors_on_focus
      prepend_option(opts[:data], :action, "click->polaris-text-field#clearErrorMessages")
    end
    prepend_option(opts[:data], :action, "polaris-text-field#syncValue")
  end
end

#input_tagObject



183
184
185
# File 'app/components/polaris/text_field_component.rb', line 183

def input_tag
  "#{input}_tag"
end

#render_number_buttons?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'app/components/polaris/text_field_component.rb', line 191

def render_number_buttons?
  @type == :number && !@disabled
end

#system_argumentsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/components/polaris/text_field_component.rb', line 108

def system_arguments
  {
    tag: "div",
    data: {
      polaris_text_field_has_value_class: "Polaris-TextField--hasValue",
      polaris_text_field_clear_button_hidden_class: "Polaris-TextField__Hidden"
    }
  }.deep_merge(@system_arguments).tap do |opts|
    opts[:classes] = class_names(
      opts[:classes],
      "Polaris-TextField",
      "Polaris-TextField--disabled": @disabled,
      "Polaris-TextField--readOnly": @readonly,
      "Polaris-TextField--error": @error,
      "Polaris-TextField--hasValue": @value.present?,
      "Polaris-TextField--multiline": @multiline
    )
    prepend_option(opts[:data], :controller, "polaris-text-field")
    if @show_character_count
      opts[:data][:polaris_text_field_label_template_value] = character_count.label_template
      opts[:data][:polaris_text_field_text_template_value] = character_count.text_template
    end
  end
end

#wrapper_argumentsObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/components/polaris/text_field_component.rb', line 93

def wrapper_arguments
  {
    form: @form,
    attribute: @attribute,
    name: @name,
    label: @label,
    label_hidden: @label_hidden,
    label_action: @label_action,
    required: @required,
    help_text: @help_text,
    error: @error,
    classes: "polaris-text-field-wrapper"
  }.deep_merge(@wrapper_arguments)
end