class RadioGroupComponent

RadioGroupComponent represents an HTML radio button group generated from an Enumerable collection.

Each item has a unique id of the form of `“#{name}-{choice}”`, where name is the provided name option, and choice is value of that button.

Each item produces a button and a label.

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

Public Class Methods

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

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

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

# File lib/html-native/collections.rb, line 363
def initialize(choices, name, attributes: {}, labelled: true, &block)
  @choices = choices
  @name = name
  @button_attributes = attributes[:button]
  @label_attributes = attributes[:label]
  @labelled = labelled
  @block = block
end

Public Instance Methods

render() click to toggle source

Converts the RadioGroupComponent 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 377
def render
  @choices.component_map do |c|
    id = "#{@name}-#{c}" 
    input({type: "radio", id: id, name: @name, value: c}) +
      (@labelled ? (label({for: id}) {@block ? @block.call(c) : c}) : nil)
  end
end