class Bootstrap4Helper::Dropdown

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

Public Class Methods

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

Class constructor

@param [ActionView] template @param [Symbol|String] type @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.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

Public Instance Methods

button(context = :primary, opts = {}) { || ... } click to toggle source

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.

@param [Symbol] context @param [Hash] opts @option opts [Boolean] :split @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @return [String]

# 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' : ''

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

Used to create a new `Dropdown::Menu`

@param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @return [Dropdown::Menu]

to_s() click to toggle source

String reprentation of the object.

@return [String]

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

Private Instance Methods

container_class() click to toggle source

Returns the container class for the dropdown component.

@return [String]

# File lib/bootstrap4_helper/dropdown.rb, line 84
def container_class
  case @type
  when :dropdown
    'dropdown'
  when :group
    'btn-group'
  else
    ''
  end
end