class DropdownComponent

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:

Public Class Methods

new(choices, name, attributes: {}, &block) click to toggle source

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.

# 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

Public Instance Methods

render() click to toggle source

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.

# 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