Class: Campo::Input

Inherits:
Base
  • Object
show all
Defined in:
lib/campo/campo.rb

Overview

form << Campo::Input.new( “submit”, :submit )

Constant Summary

Constants inherited from Base

Base::DEFAULT

Instance Attribute Summary

Attributes inherited from Base

#attributes, #attributes The element's html attributes., #fields, #fields The element's child elements.

Attributes included from Childish

#parent

Instance Method Summary collapse

Methods inherited from Base

#each, #labelled, #on_output, #output, output, quotable, unhash

Methods included from Convenience

#bit_of_ruby, #checkbox, #fieldset, #hidden, #input, #literal, #password, #radio, #select, #submit, #text, #textarea

Methods included from Iding

#id_tag

Methods included from Childish

#push=

Constructor Details

#initialize(name, type = :text, attributes = {}) ⇒ Input

Returns a new instance of Input.

Examples:

Campo::Input.new( "abc", :text, maxlength: 50 )
# => %input{ atts[:abc], tabindex: "#{@campo_tabindex += 1}", id: "abc", type: "text", maxlength: "50", name: "abc",  }

Parameters:

  • name (String)
  • type (Symbol) (defaults to: :text)
  • attributes (Hash) (defaults to: {})


634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/campo/campo.rb', line 634

def initialize( name, type=:text, attributes={} )
  id_tag = id_tag(
    [:text,:hidden,:submit,:password].include?(type) ? 
      nil : 
      attributes[:value]
  ).gsub(/\W/, "_")
  
  name2 = name.gsub(/\[\]/, "") # remove any [] that may have been used for an array like / grouped object
  
  atts_name = "#{name2.gsub(/\W/, "_")}#{id_tag}"
  
  super( name, 
        { type: type.to_s, 
          id: "#{name2}#{id_tag}",
          tabindex: %q!#{@campo_tabindex += 1}!, 
        }.merge( attributes ) )
        
           
  @attributes.delete(:name) if type == :submit
  @attributes.delete(:tabindex) if type == :hidden
        
  self.on_output do |n=0, tab=2|
    %Q!#{" " * n * tab}%input{ atts[:#{atts_name}], #{Base.unhash( @attributes )} }! 
  end
end