Class: Brut::FrontEnd::Components::Inputs::TextareaTag

Inherits:
Brut::FrontEnd::Components::Input show all
Defined in:
lib/brut/front_end/components/inputs/textarea_tag.rb

Overview

Generates an HTML <textarea> field based on a form input.

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) ⇒ TextareaTag

Creates the appropriate textarea 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 textarea 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 <textarea> element.



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

def initialize(form:, input_name:, index: nil, **html_attributes)
  html_attributes = html_attributes.map { |key,value| [ key.to_sym, value ] }.to_h
  default_html_attributes = {}

  index ||= 0
  input = form.input(input_name, index:)

  default_html_attributes[:required] = input.required
  default_html_attributes[:name]     = if input.array?
                                          "#{input.name}[]"
                                        else
                                          input.name
                                        end
  if input.maxlength
    default_html_attributes[:maxlength] = input.maxlength
  end
  if input.minlength
    default_html_attributes[:minlength] = input.minlength
  end
  if !form.new? && !input.valid?
    default_html_attributes["data-invalid"] = true
    input.validity_state.each do |constraint,violated|
      if violated
        default_html_attributes["data-#{constraint}"] = true
      end
    end
  end
  @value = input.value
  @attributes = default_html_attributes.merge(html_attributes)
end

Instance Method Details

#invalid?Boolean

Returns:

  • (Boolean)


42
# File 'lib/brut/front_end/components/inputs/textarea_tag.rb', line 42

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

#view_templateObject



44
45
46
47
48
# File 'lib/brut/front_end/components/inputs/textarea_tag.rb', line 44

def view_template
  textarea(**@attributes) {
    @value
  }
end