Class: Brut::FrontEnd::Components::Inputs::InputTag
- Inherits:
-
Brut::FrontEnd::Components::Input
- Object
- Phlex::HTML
- Brut::FrontEnd::Component
- Brut::FrontEnd::Components::Input
- Brut::FrontEnd::Components::Inputs::InputTag
- Defined in:
- lib/brut/front_end/components/inputs/input_tag.rb
Overview
Generates an HTML <input>
field based on a form input.
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(form:, input_name:, index: nil, **html_attributes) ⇒ InputTag
constructor
Creates the appropriate input for the given Form and input name.
-
#invalid? ⇒ Boolean
-
#view_template ⇒ Object
Methods inherited from Brut::FrontEnd::Component
#around_template, component_name, #component_name
Methods included from Brut::FrontEnd::Component::Helpers
#entity, #global_component, #inline_svg
Methods included from I18n::ForHTML
Methods included from I18n::BaseMethods
#l, #t, #t_direct, #this_field_value
Methods included from Brut::Framework::Errors
Constructor Details
#initialize(form:, input_name:, index: nil, **html_attributes) ⇒ InputTag
Creates the appropriate input for the given Form and input name.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 |
# File 'lib/brut/front_end/components/inputs/input_tag.rb', line 11 def initialize(form:, input_name:, index: nil, **html_attributes) input = form.input(input_name, index:) if input.class.name != "Brut::FrontEnd::Forms::Input" raise ArgumentError, "#{self.class} can only be used with `input` elements, not #{input.class.name} form elements" end default_html_attributes = {} html_attributes = html_attributes.map { |key,value| [ key.to_sym, value ] }.to_h default_html_attributes[:required] = input.required default_html_attributes[:pattern] = input.pattern default_html_attributes[:type] = input.type default_html_attributes[:name] = if input.array? "#{input.name}[]" else input.name end if input.max default_html_attributes[:max] = input.max end if input.maxlength default_html_attributes[:maxlength] = input.maxlength end if input.min default_html_attributes[:min] = input.min end if input.minlength default_html_attributes[:minlength] = input.minlength end if input.step default_html_attributes[:step] = input.step end value = input.value if input.type == "checkbox" default_html_attributes[:value] = (index || true).to_s default_html_attributes[:checked] = value == "true" else default_html_attributes[:value] = value.nil? ? nil : value.to_s end if !form.new? && !input.valid? default_html_attributes["data-invalid"] = true input.validity_state.each do |constraint| default_html_attributes["data-#{constraint}"] = true end end @attributes = default_html_attributes.merge(html_attributes) end |
Instance Method Details
#invalid? ⇒ Boolean
3 |
# File 'lib/brut/front_end/components/inputs/input_tag.rb', line 3 def invalid? = @attributes["data-invalid"] == true |
#view_template ⇒ Object
59 60 61 |
# File 'lib/brut/front_end/components/inputs/input_tag.rb', line 59 def view_template input(**@attributes) end |