Class: Bootstrap4Helper::InputGroup

Inherits:
Component
  • Object
show all
Defined in:
lib/bootstrap4_helper/input_group.rb

Overview

The InputGroup helper is meant to help you rapidly build Bootstrap input group components quickly and easily.

Constant Summary collapse

VALID_TYPES =
i[append prepend].freeze

Instance Method Summary collapse

Methods inherited from Component

#capture, #concat, #config, #content_tag, #parse_arguments, #uuid

Constructor Details

#initialize(template, type = :prepend, opts = {}, &block) ⇒ InputGroup

Class constructor

Parameters:

  • template (Class)
    • Template in which your are binding too.

  • type (Symbol) (defaults to: :prepend)
    • Whether the component is prepend or append.

  • opts (Hash) (defaults to: {})


15
16
17
18
19
20
21
22
23
# File 'lib/bootstrap4_helper/input_group.rb', line 15

def initialize(template, type = :prepend, opts = {}, &block)
  super(template)

  @type    = VALID_TYPES.include?(type) ? type : :prepend
  @id      = opts.fetch(:id,          nil)
  @class   = opts.fetch(:class,       '')
  @data    = opts.fetch(:data,        {})
  @content = block || proc { '' }
end

Instance Method Details

#text(opts = {}, &block) ⇒ String

This is the element that actually houses the icon or text used in the input group.

Parameters:

  • opts (Hash) (defaults to: {})

Returns:

  • (String)


31
32
33
34
35
36
37
# File 'lib/bootstrap4_helper/input_group.rb', line 31

def text(opts = {}, &block)
  opts[:class] = (opts[:class] || '') << " input-group-#{@type}"

   :div, opts do
     :span, class: 'input-group-text', &block
  end
end

#to_sString

Used to render out the InputGroup component.

Returns:

  • (String)


43
44
45
46
47
48
49
50
51
52
# File 'lib/bootstrap4_helper/input_group.rb', line 43

def to_s
  (
    :div,
    id:    @id,
    class: "input-group #{@class}",
    data:  @data
  ) do
    @content.call(self)
  end
end