Class: Brut::FrontEnd::Components::Inputs::InputTag

Inherits:
Brut::FrontEnd::Components::Input show all
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

RadioButton

Instance Method Summary collapse

Methods inherited from Brut::FrontEnd::Component

component_name, #component_name

Methods included from Brut::FrontEnd::Component::Helpers

#global_component, #inline_svg

Methods included from I18n::ForHTML

#html_escape, #t

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from Brut::Framework::Errors

#abstract_method!, #bug!

Constructor Details

#initialize(form:, input_name:, index: nil, **html_attributes) ⇒ InputTag

Creates the appropriate input for the given Form and input name. Generally, you want to use this method over the initializer.

Parameters:

  • form (Brut::FrontEnd::Form)

    The form that is being rendered. This method will consult this class to understand the requirements on this input so its HTML is generated correctly.

  • input_name (String)

    the name of the input, which should be a member of form

  • index (Integer) (defaults to: nil)

    if this input is part of an array, this is the index into that array. This is used to get the input's value.

  • html_attributes (Hash)

    any additional HTML attributes to include on the <input> element.



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
# File 'lib/brut/front_end/components/inputs/input_tag.rb', line 12

def initialize(form:, input_name:, index: nil, **html_attributes)
  input = form.input(input_name, index:)
  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

Returns:

  • (Boolean)


3
# File 'lib/brut/front_end/components/inputs/input_tag.rb', line 3

def invalid? = @attributes["data-invalid"] == true

#view_templateObject



57
58
59
# File 'lib/brut/front_end/components/inputs/input_tag.rb', line 57

def view_template
  input(**@attributes)
end