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_id` is
NilClass
it is because theAccordion::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 |
# File 'lib/bootstrap5_helper/accordion/item.rb', line 16 def initialize(template, parent_id = nil, opts = {}, &block) super(template) @parent = parent_id @id = opts.fetch(:id, uuid) @class = opts.fetch(:class, '') @data = opts.fetch(:data, {}) @expanded = opts.fetch(:expanded, false) @collapse = opts.fetch(:collapse, {}) @collapse_id = @collapse.fetch(:id, uuid) @collapse_klass = @collapse.fetch(:class, '') @collapse_data = @collapse.fetch(:data, {}) @header_id = uuid @content = block || proc { '' } end |
Instance Method Details
#body(opts = {}, &block) ⇒ String
Builds the body component for the accordion.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/bootstrap5_helper/accordion/item.rb', line 92 def body(opts = {}, &block) id = opts.fetch(:id, uuid) klass = opts.fetch(:class, '') data = opts.fetch(: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, &block ) end end |
#header(tag, opts) ⇒ String #header(opts) ⇒ String
Builds a header component for the accordion.
51 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 |
# File 'lib/bootstrap5_helper/accordion/item.rb', line 51 def header(*, &block) tag, args = (*, {}) @header_id = args.fetch(:id, @header_id) klass = args.fetch(:class, '') data = args.fetch(:data, {}) content_tag( tag || config({ accordions: :header }, :h2), id: @header_id, class: "accordion-header #{klass}", data: data ) 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.
121 122 123 124 125 |
# File 'lib/bootstrap5_helper/accordion/item.rb', line 121 def to_s content_tag :div, id: @id, class: "accordion-item #{@class}", data: @data do @content.call(self) end end |