Class: Bootstrap5Helper::Accordion::Item
- Defined in:
- lib/bootstrap5_helper/accordion/item.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#body(opts = {}, &block) ⇒ String
Builds the body component for the accordion.
-
#header(*tag_or_options, &block) ⇒ String
Builds a header component for the accordion.
-
#initialize(template, parent_id = nil, opts = {}, &block) ⇒ Item
constructor
Take note that if
parent_idis NilClass it is because the Accordion::Item is meant to stay open and not close the previous one. -
#to_s ⇒ String
String representation of the object.
Methods inherited from Component
#capture, #concat, #config, #content_tag, #parse_arguments, #parse_context_or_options, #parse_tag_or_options, #parse_text_or_options, #uuid
Constructor Details
#initialize(template, parent_id = nil, opts = {}, &block) ⇒ Item
Take note that if parent_id is NilClass it is because
the Accordion::Item is meant to stay open and not close
the previous one.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bootstrap5_helper/accordion/item.rb', line 16 def initialize(template, parent_id = nil, opts = {}, &block) super(template) @parent = parent_id @attrs = opts @id = @attrs.delete(:id) { uuid } @class = @attrs.delete(:class) { '' } @data = @attrs.delete(:data) { {} } @expanded = @attrs.delete(:expanded) { false } @collapse = @attrs.delete(:collapse) { {} } @collapse_id = @collapse.delete(:id) { uuid } @collapse_klass = @collapse.delete(:class) { '' } @collapse_data = @collapse.delete(:data) { {} } @header_id = uuid @content = block || proc { '' } end |
Instance Method Details
#body(opts = {}, &block) ⇒ String
Builds the body component for the accordion.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/bootstrap5_helper/accordion/item.rb', line 95 def body(opts = {}, &block) id = opts.delete(:id) { uuid } klass = opts.delete(:class) { '' } data = opts.delete(:data) { {} } content_tag( :div, id: @collapse_id, class: "accordion-collapse collapse #{@collapse_klass} #{@expanded ? 'show' : ''}", aria: { labelledby: @header_id }, data: { 'bs-parent' => @parent.present? ? "##{@parent}" : nil }.merge(@collapse_data) ) do content_tag( :div, { id: id, class: "accordion-body #{klass}", data: data }.merge(opts), &block ) end end |
#header(tag, opts) ⇒ String #header(opts) ⇒ String
Builds a header component for the accordion.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/bootstrap5_helper/accordion/item.rb', line 52 def header(*, &block) tag, args = (*, {}) @header_id = args.delete(:id) { @header_id } klass = args.delete(:class) { '' } data = args.delete(:data) { {} } content_tag( tag || config({ accordions: :header }, :h2), { id: @header_id, class: "accordion-header #{klass}", data: data }.merge(args) ) do content_tag( :button, type: :button, class: "accordion-button #{@expanded ? '' : 'collapsed'}", data: { 'bs-toggle' => 'collapse', 'bs-target' => "##{@collapse_id}" }, aria: { expanded: @expanded, controls: @collapse_id }, &block ) end end |
#to_s ⇒ String
String representation of the object.
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/bootstrap5_helper/accordion/item.rb', line 126 def to_s content_tag( :div, { id: @id, class: "accordion-item #{@class}", data: @data }.merge(@attrs) ) do @content.call(self) end end |