class Bootstrap4Helper::Dropdown::Menu
Builds a menu component for use in dropdowns.
Public Class Methods
Class constructor
@param [ActionView] template @param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data
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
Builds a divider element
@return [String]
# File lib/bootstrap4_helper/dropdown/menu.rb, line 97 def divider content_tag :div, '', class: 'dropdown-divider' end
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
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
Use this method when the `item`, `link` in the item in the menu is nothing more than a hyperlink.
@param [String] name @param [Hash] options @param [Hash] html_options @return [String]
# 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
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
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
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