Class: DropdownComponent

Inherits:
Object
  • Object
show all
Includes:
HTMLComponent
Defined in:
lib/html-native/collections.rb

Overview

DropdownComponent represents an HTML table row generated from an Enumerable collection.

Attributes in an DropdownComponent are separated into multiple groups since there are multiple kinds of tags. These groups are:

  • menu - The attributes associated with the <select> element

  • item - The attributes associated with <option> elements

Constant Summary

Constants included from HTMLComponent

HTMLComponent::FORBIDDEN_ATTRIBUTES, HTMLComponent::LIMITED_ATTRIBUTES, HTMLComponent::TAG_LIST

Instance Method Summary collapse

Methods included from HTMLComponent

#_if, #_label, #_unless, #doctype, singleton, #valid_attribute?

Constructor Details

#initialize(choices, name, attributes: {}, &block) ⇒ DropdownComponent

Creates a new instance of DropdownComponent from the values of choices.

If a block is given, each item in choices is passed to it to render the item. If no block is given, choices is used directly.



322
323
324
325
326
327
328
# File 'lib/html-native/collections.rb', line 322

def initialize(choices, name, attributes: {}, &block)
  @choices = choices
  @name = name
  @menu_attributes = attributes[:menu]
  @item_attributes = attributes[:item]
  @block = block
end

Instance Method Details

#renderObject

Converts the DropdownComponent instance to the equivalent HTML.

render can be called directly, but that usually isn’t necessary. HTMLComponent::Builder handles this automatically, so it only needs to be done if there is no prior instance of one.



335
336
337
338
339
340
341
# File 'lib/html-native/collections.rb', line 335

def render
  select(@menu_attributes.merge({name: @name, id: "#{@name}-dropdown"})) do
    @choices.component_map do |c|
      option(@item_attributes.merge({value: c})) {@block ? @block.call(c) : c}
    end
  end
end