class UiBibz::Ui::Core::Forms::Dropdowns::Dropdown

Create a dropdown

This element is an extend of UiBibz::Ui::Core::Component.

Attributes

Options

You can add HTML attributes using the html_options. You can pass arguments in options attribute:

Signatures

UiBibz::Ui::Core::Forms::Dropdowns::Dropdown.new(options = nil, html_options = nil).tap do |d|
  ...
  d.header content = nil, options = nil, html_options = nil, &block
  d.divider
  d.link content = nil, options = nil, html_options = nil, &block
  ...
end

Examples

UiBibz::Ui::Core::Forms::Dropdowns::Dropdown.new(name, status: :success).tap do |d|
  d.link 'test', { url: '#' }
  d.divider
  d.header 'Header 1'
  d.link 'test2', { url: '#' }
end.render

Helper

dropdown(name, options = {}, html_options = {}) do |d|
  d.link(content, options = {}, html_options = {})
  d.link(options = {}, html_options = {}) do
    content
  end
  d.divider
  d.header(content, options = {}, html_options = {})
  d.header(options = {}, html_options = {}) do
    content
  end
end

Public Class Methods

new(content, options = nil, html_options = nil, &block) click to toggle source
Calls superclass method UiBibz::Ui::Core::Component::new
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 73
def initialize(content, options = nil, html_options = nil, &block)
  super
  @items = []
  @status = @options.delete(:status)
end

Public Instance Methods

divider() click to toggle source

Add dropdown Separator See UiBibz::Ui::Core::Forms::Dropdowns::Components::DropdownDivider

# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 95
def divider
  @items << UiBibz::Ui::Core::Forms::Dropdowns::Components::DropdownDivider.new.render
end
header(content = nil, options = nil, html_options = nil, &block) click to toggle source

Add dropdown header See UiBibz::Ui::Core::Forms::Dropdowns::Components::DropdownHeader

# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 89
def header(content = nil, options = nil, html_options = nil, &block)
  @items << UiBibz::Ui::Core::Forms::Dropdowns::Components::DropdownHeader.new(content, options, html_options, &block).render
end
id() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 105
def id
  @id ||= html_options[:id] || generate_id('dropdown')
end
pre_render() click to toggle source

Render html tag

# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 80
def pre_render
  content_tag :div, html_options do
    concat button_html
    concat ul_html
  end
end

Protected Instance Methods

alignment() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 136
def alignment
  return nil if @options[:alignment].nil?

  if @options[:alignment].is_a? Hash
    ['dropdown-menu', @options[:alignment][:size], match_direction[@options[:alignment][:direction]]].join('-')
  else
    "dropdown-menu-#{match_direction[@options[:alignment]]}"
  end
end
button_content() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 115
def button_content
  glyph_and_content_html(@content)
end
button_html() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 119
def button_html
  html_button = options[:html_button] || {}
  if options[:tag] == :a
    content_tag dropdown_tag, button_content, { class: join_classes('btn', button_status, state, size, 'dropdown-toggle'), role: 'button', 'data-bs-toggle' => 'dropdown', 'aria-expanded' => false, 'id' => id }.merge(html_button)
  else
    content_tag dropdown_tag, button_content, { class: join_classes('btn', button_status, state, size, 'dropdown-toggle'), type: 'button', 'data-bs-toggle' => 'dropdown', 'aria-expanded' => false, 'id' => id }.merge(html_button)
  end
end
button_status() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 170
def button_status
  ['btn', outline, @status || :secondary].compact.join('-')
end
component_html_classes() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 111
def component_html_classes
  [position, open, inline, without_caret, keep_open]
end
dropdown_tag() click to toggle source
inline() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 158
def inline
  'btn-group' if @options[:inline] || @options[:alignment]
end
keep_open() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 154
def keep_open
  'keep-open' if @options[:keep_open]
end
match_direction() click to toggle source

Match end and start directions

# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 184
def match_direction
  {
    up: 'up',
    right: 'end',
    down: 'down',
    left: 'start',
    start: 'start',
    end: 'end'
  }.with_indifferent_access
end
open() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 150
def open
  'show' if @options[:open]
end
outline() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 166
def outline
  'outline' if @options[:outline]
end
position() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 146
def position
  "drop#{match_direction[@options[:position] || :down]}"
end
size() click to toggle source

:lg, :sm or :xs

# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 175
def size
  "btn-#{@options[:size]}" if @options[:size]
end
theme() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 179
def theme
  'dropdown-menu-dark' if @options[:theme]
end
ul_html() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 128
def ul_html
  content_tag :div, @items.join.html_safe, class: join_classes('dropdown-menu', theme, alignment, open), 'arial-labelledby' => id
end
without_caret() click to toggle source
# File lib/ui_bibz/ui/core/forms/dropdowns/dropdown.rb, line 162
def without_caret
  'without-caret' if @options[:caret] == false
end