Class: Bootstrap5Helper::Accordion::Item

Inherits:
Component
  • Object
show all
Defined in:
lib/bootstrap5_helper/accordion/item.rb

Overview

:nodoc:

Instance Method Summary collapse

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.

Parameters:

  • template (ActionView)
  • parent_id (String|NilClass) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)
  • :collapse (Hash)


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.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)

Returns:

  • (String)


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,  {})

  (
    :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
    (
      :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.

Overloads:

  • #header(tag, opts) ⇒ String

    Parameters:

    • tag (Symbol|String)
      • The HTML element to use.

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)
  • #header(opts) ⇒ String

    Parameters:

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)

Returns:

  • (String)


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(*tag_or_options, &block)
  tag, args = parse_tag_or_options(*tag_or_options, {})

  @header_id = args.fetch(:id,    @header_id)
  klass      = args.fetch(:class, '')
  data       = args.fetch(:data,  {})

  (
    tag || config({ accordions: :header }, :h2),
    id:    @header_id,
    class: "accordion-header #{klass}",
    data:  data
  ) do
    (
      :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_sString

String representation of the object.

Returns:

  • (String)


121
122
123
124
125
# File 'lib/bootstrap5_helper/accordion/item.rb', line 121

def to_s
   :div, id: @id, class: "accordion-item #{@class}", data: @data do
    @content.call(self)
  end
end