Sha256: 782f662dbf47ee367a564b933412bd5822a3c17bdbafa3bf16eff2e44c26030a

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

class Brut::FrontEnd::Components::Inputs::TextField < Brut::FrontEnd::Components::Input
  def self.for_form_input(form:, input_name:, html_attributes: {})
    default_html_attributes = {}
    input = form[input_name]
    default_html_attributes["required"] = input.required
    default_html_attributes["pattern"]  = input.pattern
    default_html_attributes["type"]     = input.type
    default_html_attributes["name"]     = input.name
    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
    if input.type == "checkbox"
      default_html_attributes["value"] = "true"
      default_html_attributes["checked"] = input.value == "true"
    else
      default_html_attributes["value"] = input.value
    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
    Brut::FrontEnd::Components::Inputs::TextField.new(default_html_attributes.merge(html_attributes))
  end
  def initialize(attributes)
    @sanitized_attributes = attributes.map { |key,value|
        [
          key.to_s.gsub(/[\s\"\'>\/=]/,"-"),
          value
        ]
    }.select { |key,value|
      !value.nil?
    }.to_h
  end

  def render
    attribute_string = @sanitized_attributes.map { |key,value|
      if value == true
        key
      elsif value == false
        ""
      else
        REXML::Attribute.new(key,value).to_string
      end
    }.join(" ")
    "<input #{attribute_string}>"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
brut-0.0.1 lib/brut/front_end/components/inputs/text_field.rb