Class: Ariadne::Forms::Dsl::Input
- Inherits:
-
Object
- Object
- Ariadne::Forms::Dsl::Input
show all
- Includes:
- ClassNameHelper
- Defined in:
- lib/ariadne/forms/dsl/input.rb
Overview
Constant Summary
collapse
- SPACE_DELIMITED_ARIA_ATTRIBUTES =
[:describedby].freeze
- DEFAULT_SIZE =
:medium
- SIZE_MAPPINGS =
{
:small => "FormControl-small",
DEFAULT_SIZE => "FormControl-medium",
:large => "FormControl-large",
}.freeze
- SIZE_OPTIONS =
SIZE_MAPPINGS.keys
Instance Attribute Summary collapse
Instance Method Summary
collapse
#merge_class_names
Constructor Details
#initialize(builder:, form:, **options) ⇒ Input
Returns a new instance of Input.
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/ariadne/forms/dsl/input.rb', line 54
def initialize(builder:, form:, **options)
@builder = builder
@form = form
@options = options.dup
@input_attributes = @options.delete(:html_attrs) || {}
@input_attributes.delete(:id) if @input_attributes[:id].blank?
@label_attributes = @options[:label_html_attributes] || {}
@label_attributes[:for] = id if id.present?
@label_attributes[:class] = merge_class_names(
@label_attributes[:class],
@options.delete(:visually_hide_label) ? "sr-only" : nil,
)
@input_attributes.delete(:class) if @input_attributes[:class].blank?
@label_attributes.delete(:class) if @label_attributes[:class].blank?
@caption = @options.delete(:caption)
@placeholder = @options.delete(:placeholder)
@disabled = @options.delete(:disabled)
@validation_message = @options.delete(:validation_message)
@invalid = @options.delete(:invalid)
@full_width = @options.delete(:full_width) { true }
unless @options.delete(:scope_name_to_model) { true }
@input_attributes[:name] = name
end
unless @options.delete(:scope_id_to_model) { true }
@input_attributes[:id] = @input_attributes.delete(:id) { name }
end
@form_control = @options.delete(:form_control) { true }
@options[:invalid] = "true" if invalid?
@base_id = ::Ariadne::ViewHelper.generate_id
@ids = {}.tap do |id_map|
id_map[:validation] = "validation-#{@base_id}" if supports_validation?
id_map[:caption] = "caption-#{@base_id}" if caption? || caption_template?
end
if required?
input_attributes[:required] = true
add_input_aria(:required, true)
end
add_input_aria(:invalid, true) if invalid?
add_input_aria(:describedby, ids.values) if ids.any?
end
|
Instance Attribute Details
#base_id ⇒ Object
Returns the value of attribute base_id.
50
51
52
|
# File 'lib/ariadne/forms/dsl/input.rb', line 50
def base_id
@base_id
end
|
#builder ⇒ Object
Returns the value of attribute builder.
50
51
52
|
# File 'lib/ariadne/forms/dsl/input.rb', line 50
def builder
@builder
end
|
#caption ⇒ Object
Returns the value of attribute caption.
50
51
52
|
# File 'lib/ariadne/forms/dsl/input.rb', line 50
def caption
@caption
end
|
Returns the value of attribute form.
50
51
52
|
# File 'lib/ariadne/forms/dsl/input.rb', line 50
def form
@form
end
|
Returns the value of attribute form_control.
50
51
52
|
# File 'lib/ariadne/forms/dsl/input.rb', line 50
def form_control
@form_control
end
|
#ids ⇒ Object
Returns the value of attribute ids.
50
51
52
|
# File 'lib/ariadne/forms/dsl/input.rb', line 50
def ids
@ids
end
|
Returns the value of attribute input_attributes.
50
51
52
|
# File 'lib/ariadne/forms/dsl/input.rb', line 50
def input_attributes
@input_attributes
end
|
#label_attributes ⇒ Object
Returns the value of attribute label_attributes.
50
51
52
|
# File 'lib/ariadne/forms/dsl/input.rb', line 50
def label_attributes
@label_attributes
end
|
#validation_message ⇒ Object
Returns the value of attribute validation_message.
50
51
52
|
# File 'lib/ariadne/forms/dsl/input.rb', line 50
def validation_message
@validation_message
end
|
Instance Method Details
133
134
135
136
137
138
139
140
141
|
# File 'lib/ariadne/forms/dsl/input.rb', line 133
def add_input_aria(key, value)
@input_attributes[:aria] ||= {}
@input_attributes[:aria][key] = if space_delimited_aria_attribute?(key)
aria_join(@input_attributes[:aria][key], *Array(value))
else
value
end
end
|
127
128
129
130
131
|
# File 'lib/ariadne/forms/dsl/input.rb', line 127
def add_input_classes(*class_names)
input_attributes[:class] = merge_class_names(
input_attributes[:class], *class_names
)
end
|
143
144
145
|
# File 'lib/ariadne/forms/dsl/input.rb', line 143
def add_input_data(key, value)
input_data[key] = value
end
|
#autofocus! ⇒ Object
232
233
234
|
# File 'lib/ariadne/forms/dsl/input.rb', line 232
def autofocus!
@input_attributes[:autofocus] = true
end
|
#caption? ⇒ Boolean
180
181
182
|
# File 'lib/ariadne/forms/dsl/input.rb', line 180
def caption?
caption.present?
end
|
#caption_id ⇒ Object
176
177
178
|
# File 'lib/ariadne/forms/dsl/input.rb', line 176
def caption_id
ids[:caption]
end
|
#caption_template? ⇒ Boolean
184
185
186
187
188
|
# File 'lib/ariadne/forms/dsl/input.rb', line 184
def caption_template?
return false unless form
form.caption_template?(caption_template_name)
end
|
#disabled? ⇒ Boolean
213
214
215
|
# File 'lib/ariadne/forms/dsl/input.rb', line 213
def disabled?
@disabled.present?
end
|
#focusable? ⇒ Boolean
258
259
260
|
# File 'lib/ariadne/forms/dsl/input.rb', line 258
def focusable?
false
end
|
#hidden? ⇒ Boolean
202
203
204
|
# File 'lib/ariadne/forms/dsl/input.rb', line 202
def hidden?
!!input_attributes[:hidden]
end
|
#id ⇒ Object
236
237
238
|
# File 'lib/ariadne/forms/dsl/input.rb', line 236
def id
@input_attributes[:id]
end
|
262
263
264
|
# File 'lib/ariadne/forms/dsl/input.rb', line 262
def input?
true
end
|
#invalid? ⇒ Boolean
198
199
200
|
# File 'lib/ariadne/forms/dsl/input.rb', line 198
def invalid?
supports_validation? && !valid?
end
|
#label ⇒ Object
245
246
247
|
# File 'lib/ariadne/forms/dsl/input.rb', line 245
def label
raise_for_abstract_method!(__method__)
end
|
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/ariadne/forms/dsl/input.rb', line 153
def merge_input_attributes!(arguments)
arguments.each do |k, v|
case k
when :class, :classes, "class", "classes"
add_input_classes(v)
when :aria, "aria"
v.each do |aria_k, aria_v|
add_input_aria(aria_k, aria_v)
end
when :data, "data"
v.each do |data_k, data_v|
add_input_data(data_k, data_v)
end
else
@input_attributes[k] = v
end
end
end
|
#name ⇒ Object
241
242
243
|
# File 'lib/ariadne/forms/dsl/input.rb', line 241
def name
raise_for_abstract_method!(__method__)
end
|
148
149
150
|
# File 'lib/ariadne/forms/dsl/input.rb', line 148
def remove_input_data(key)
input_data.delete(key)
end
|
#render_caption_template ⇒ Object
190
191
192
|
# File 'lib/ariadne/forms/dsl/input.rb', line 190
def render_caption_template
form.render_caption_template(caption_template_name)
end
|
#required? ⇒ Boolean
206
207
208
209
210
211
|
# File 'lib/ariadne/forms/dsl/input.rb', line 206
def required?
@options[:required] ||
input_attributes[:aria_required] ||
input_attributes[:"aria-required"] ||
input_attributes.dig(:aria, :required)
end
|
#size ⇒ Object
217
218
219
|
# File 'lib/ariadne/forms/dsl/input.rb', line 217
def size
@size ||= SIZE_MAPPINGS.include?(@size) ? @size : DEFAULT_SIZE
end
|
#supports_validation? ⇒ Boolean
266
267
268
|
# File 'lib/ariadne/forms/dsl/input.rb', line 266
def supports_validation?
true
end
|
#to_component ⇒ Object
253
254
255
|
# File 'lib/ariadne/forms/dsl/input.rb', line 253
def to_component
raise_for_abstract_method!(__method__)
end
|
#type ⇒ Object
249
250
251
|
# File 'lib/ariadne/forms/dsl/input.rb', line 249
def type
raise_for_abstract_method!(__method__)
end
|
#valid? ⇒ Boolean
194
195
196
|
# File 'lib/ariadne/forms/dsl/input.rb', line 194
def valid?
supports_validation? && validation_messages.empty? && !@invalid
end
|
#validation_arguments ⇒ Object
270
271
272
273
274
275
276
|
# File 'lib/ariadne/forms/dsl/input.rb', line 270
def validation_arguments
{
class: "FormControl-inlineValidation",
id: validation_id,
hidden: valid? || validation_messages.empty?,
}
end
|
#validation_error_icon_target ⇒ Object
286
287
288
|
# File 'lib/ariadne/forms/dsl/input.rb', line 286
def validation_error_icon_target
""
end
|
#validation_id ⇒ Object
172
173
174
|
# File 'lib/ariadne/forms/dsl/input.rb', line 172
def validation_id
ids[:validation]
end
|
#validation_message_arguments ⇒ Object
278
279
280
|
# File 'lib/ariadne/forms/dsl/input.rb', line 278
def validation_message_arguments
{}
end
|
#validation_messages ⇒ Object
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/ariadne/forms/dsl/input.rb', line 221
def validation_messages
@validation_messages ||=
if validation_message
[validation_message]
elsif builder.object.respond_to?(:errors)
name ? builder.object.errors.full_messages_for(name) : []
else
[]
end
end
|
#validation_success_icon_target ⇒ Object
282
283
284
|
# File 'lib/ariadne/forms/dsl/input.rb', line 282
def validation_success_icon_target
""
end
|