Class: Bootstrap4Helper::Dropdown::Menu

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

Overview

Builds a menu component for use in dropdowns.

Instance Method Summary collapse

Methods inherited from Component

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

Constructor Details

#initialize(template, opts = {}, &block) ⇒ Menu

Class constructor

Parameters:

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

Options Hash (opts):

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


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

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

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

Instance Method Details

#dividerString

Builds a divider element

Returns:

  • (String)


97
98
99
# File 'lib/bootstrap4_helper/dropdown/menu.rb', line 97

def divider
   :div, '', class: 'dropdown-divider'
end

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

Builds a Header component

Parameters:

  • text (Symbol|String)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

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

Returns:

  • (String)


89
90
91
# File 'lib/bootstrap4_helper/dropdown/menu.rb', line 89

def header(text, opts = {}, &block)
  build_sub_component :h6, text, 'header', opts, &block
end

#item(target, opts = {}) ⇒ String

Use this method when you are using the item in the menu as trigger for tab content.

Parameters:

  • target (Symbol|String)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

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

Returns:

  • (String)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bootstrap4_helper/dropdown/menu.rb', line 49

def item(target, opts = {})
  id    = opts.fetch(:id,    nil)
  klass = opts.fetch(:class, '')
  data  = opts.fetch(:data,  {}).merge(toggle: 'tab')
  aria  = opts.fetch(:aria,  {})

  (
    :a,
    id:    id,
    class: "dropdown-item #{klass}",
    href:  "##{target}",
    aria:  aria,
    data:  data
  ) do
    block_given? ? yield : target.to_s.titleize
  end
end

Use this method when the ‘item`, `link` in the item in the menu is nothing more than a hyperlink.

Parameters:

  • name (String) (defaults to: nil)
  • options (Hash) (defaults to: nil)
  • html_options (Hash) (defaults to: nil)

Returns:

  • (String)


32
33
34
35
36
# File 'lib/bootstrap4_helper/dropdown/menu.rb', line 32

def link(name = nil, options = nil, html_options = nil, &block)
  html_options = (html_options || {}).merge(class: 'dropdown-item')

  @template.link_to(name, options, html_options, &block)
end

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

Builds a Text component

Parameters:

  • text (Symbol|String)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

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

Returns:

  • (String)


76
77
78
# File 'lib/bootstrap4_helper/dropdown/menu.rb', line 76

def text(text, opts = {}, &block)
  build_sub_component :span, text, 'item-text', opts, &block
end

#to_sString

String representation of the object.

Returns:

  • (String)


105
106
107
108
109
# File 'lib/bootstrap4_helper/dropdown/menu.rb', line 105

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