class Bootstrap4Helper::Dropdown::Menu

Builds a menu component for use in dropdowns.

Public Class Methods

new(template, opts = {}, &block) click to toggle source

Class constructor

@param [ActionView] template @param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data

Calls superclass method Bootstrap4Helper::Component::new
# 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

Public Instance Methods

divider() click to toggle source

Builds a divider element

@return [String]

# File lib/bootstrap4_helper/dropdown/menu.rb, line 97
def divider
  content_tag :div, '', class: 'dropdown-divider'
end
header(text, opts = {}, &block) click to toggle source

Builds a Header component

@param [Symbol|String] text @param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @return [String]

# 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 = {}) { |: target.titleize| ... } click to toggle source

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

@param [Symbol|String] target @param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @option opts [Hash] :aria @return [String]

# 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,  {})

  content_tag(
    :a,
    id:    id,
    class: "dropdown-item #{klass}",
    href:  "##{target}",
    aria:  aria,
    data:  data
  ) do
    block_given? ? yield : target.to_s.titleize
  end
end
text(text, opts = {}, &block) click to toggle source

Builds a Text component

@param [Symbol|String] text @param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @return [String]

# 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_s() click to toggle source

String representation of the object.

@return [String]

# File lib/bootstrap4_helper/dropdown/menu.rb, line 105
def to_s
  content_tag :div, id: @id, class: "dropdown-menu #{@class}", data: @data do
    @content.call(self)
  end
end

Private Instance Methods

build_sub_component(tag, text, type, opts) { |: text || ''| ... } click to toggle source

Used to build specific components.

@param [Symbol] tag @param [Symbol|String] text @param [Symbol|String] type @param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @return [String]

# File lib/bootstrap4_helper/dropdown/menu.rb, line 124
def build_sub_component(tag, text, type, opts)
  id    = opts.fetch(:id,    nil)
  klass = opts.fetch(:class, '')
  data  = opts.fetch(:data,  {})

  content_tag(
    tag,
    id:    id,
    class: "dropdown-#{type} #{klass}",
    data:  data
  ) do
    block_given? ? yield : text || ''
  end
end