Class: UiBibz::Ui::Core::Forms::Choices::ChoiceGroup

Inherits:
Buttons::ButtonGroup show all
Includes:
UiBibz::Ui::Concerns::HtmlConcern
Defined in:
lib/ui_bibz/ui/core/forms/choices/choice_group.rb

Overview

Create a choice group

This element is an extend of UiBibz::Ui::Core::Forms::Choices::ButtonGroup

Attributes

  • content - Content of element

  • options - Options of element

  • html_options - Html Options of element

Options

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

  • type - Symbol (:checkbox, :radio)

  • status - status of element with symbol value: (:primary, :secondary, :info, :warning, :danger)

  • size (:xs, :sm, :lg)

  • outline - Boolean

  • state - Symbol (:active, +:disabled)

Signatures

UiBibz::Ui::Core::Forms::Choices::ChoiceGroup.new(content, options = nil, html_options = nil)

UiBibz::Ui::Core::Forms::Choices::ChoiceGroup.new(options = nil, html_options = nil) do
  content
end

Examples

UiBibz::Ui::Core::Forms::Choices::ChoiceGroup.new({ status: primary }, { class: 'lable-class'}) do |cg|
  cg.choice 'Choice 1'
  cg.choice 'Choice 2'
end.render

Helper

ui_choice_group(options = {}, html_options = {}) do |cg|
  cg.choice content, options, html_options
  cg.choice content, options, html_options
end

Constant Summary

Constants inherited from Component

Component::BREAKPOINTS, Component::SIZES, Component::STATUSES

Instance Attribute Summary collapse

Attributes inherited from Component

#content, #html_options, #options

Attributes inherited from Base

#output_buffer

Instance Method Summary collapse

Methods inherited from Buttons::ButtonGroup

#button, #button_link, #choice_group, #dropdown, #html, #pre_render, #split_dropdown

Methods inherited from Component

#pre_render, #render, #tapped?

Methods included from PopoverExtension

#popover_data_html, #tooltip_data_html

Methods included from GlyphExtension

#generate_glyph, #glyph_and_content_html

Methods included from KlassExtension

#exclude_classes, #exclude_classes_in_html_options, #join_classes, #status

Methods inherited from Base

#generate_id, #i18n_set?, #inject_url

Constructor Details

#initialize(content = nil, options = nil, html_options = nil, &block) ⇒ ChoiceGroup

See UiBibz::Ui::Core::Forms::Choices::Button.initialize



55
56
57
58
59
60
61
# File 'lib/ui_bibz/ui/core/forms/choices/choice_group.rb', line 55

def initialize(content = nil, options = nil, html_options = nil, &block)
  super
  @items = []
  @errors = []
  @required_fields = []
  @radio_name = @options[:name] || generate_id('choice')
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



51
52
53
# File 'lib/ui_bibz/ui/core/forms/choices/choice_group.rb', line 51

def errors
  @errors
end

#itemsObject (readonly)

Returns the value of attribute items.



51
52
53
# File 'lib/ui_bibz/ui/core/forms/choices/choice_group.rb', line 51

def items
  @items
end

#required_fieldsObject (readonly)

Returns the value of attribute required_fields.



51
52
53
# File 'lib/ui_bibz/ui/core/forms/choices/choice_group.rb', line 51

def required_fields
  @required_fields
end

Instance Method Details

#choice(content = nil, opts = nil, html_options = nil, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ui_bibz/ui/core/forms/choices/choice_group.rb', line 63

def choice(content = nil, opts = nil, html_options = nil, &block)
  if block.nil?
    opts = @options.merge(opts || {})
  else
    content = @options.merge(content || {})
  end

  opts = opts.merge(name: @radio_name) if opts[:type] == :radio

  @items << Choice.new(content, opts, html_options, &block)
end

#input(attribute_name, options = {}, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/ui_bibz/ui/core/forms/choices/choice_group.rb', line 75

def input(attribute_name, options = {}, &block)
  new_options = options.merge(old_label: options[:label], label: false, wrapper: false, error: false)
  new_options = new_options.merge(name: @radio_name, type: :radio) if @options[:type] == :radio

  @items << @options[:form].input(attribute_name, new_options, &block)
  obj = @options[:form].object
  @errors << obj.errors[attribute_name] unless obj.errors[attribute_name].empty?
  @required_fields << (obj._validators[attribute_name].try(:first).class.to_s == 'ActiveRecord::Validations::PresenceValidator')
end