Class: Bootstrap4Helper::Dropdown

Inherits:
Component
  • Object
show all
Defined in:
lib/bootstrap4_helper/dropdown.rb,
lib/bootstrap4_helper/dropdown/menu.rb

Overview

Builds a Dropdown component that can be used in other components.

Defined Under Namespace

Classes: Menu

Instance Method Summary collapse

Methods inherited from Component

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

Constructor Details

#initialize(template, type = :dropdown, opts = {}, &block) ⇒ Dropdown

Class constructor

Parameters:

  • template (ActionView)
  • type (Symbol|String) (defaults to: :dropdown)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)


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

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

  @type    = type
  @id      = opts.fetch(:id,    uuid)
  @class   = opts.fetch(:class, '')
  @data    = opts.fetch(:data,  {})
  @content = block || proc { '' }
end

Instance Method Details

#button(context = :primary, opts = {}) ⇒ String

Used to generate a button for the dropdown. The buttons default as just a button that opens the coresponding dropdown menu. The ‘split: true` option make the button just the arrow indicator that open the menu.

Parameters:

  • context (Symbol) (defaults to: :primary)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :split (Boolean)
  • :id (String)
  • :class (String)
  • :data (Hash)

Returns:

  • (String)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bootstrap4_helper/dropdown.rb', line 37

def button(context = :primary, opts = {})
  split = opts.fetch(:split, false)
  id    = opts.fetch(:id,    nil)
  klass = opts.fetch(:class, '')
  data  = opts.fetch(:data,  {}).merge(toggle: 'dropdown')
  extra = split ? 'dropdown-toggle-split' : ''

  (
    :button,
    id:    id,
    type:  'button',
    class: "dropdown-toggle btn btn-#{context} #{klass} #{extra}",
    data:  data,
    aria:  { haspopup: true, expanded: false }
  ) do
    split ? (:span, 'Toggle Dropdwon', class: 'sr-only') : yield
  end
end

Used to create a new ‘Dropdown::Menu`

Parameters:

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

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)

Returns:



64
65
66
# File 'lib/bootstrap4_helper/dropdown.rb', line 64

def menu(opts = {}, &block)
  Menu.new(@template, opts, &block)
end

#to_sString

String reprentation of the object.

Returns:

  • (String)


72
73
74
75
76
# File 'lib/bootstrap4_helper/dropdown.rb', line 72

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