Class: Daisy::DataDisplay::AccordionComponent::AccordionSectionComponent

Inherits:
LocoMotion::BasicComponent show all
Defined in:
app/components/daisy/data_display/accordion_component.rb

Overview

Renders a single section of the accordion.

Constant Summary

Constants inherited from LocoMotion::BaseComponent

LocoMotion::BaseComponent::EMPTY_PART_IGNORED_TAGS, LocoMotion::BaseComponent::SELF_CLOSING_TAGS

Instance Attribute Summary collapse

Attributes inherited from LocoMotion::BaseComponent

#config, #loco_parent

Instance Method Summary collapse

Methods inherited from LocoMotion::BasicComponent

name

Methods inherited from LocoMotion::BaseComponent

build, #component_ref, #config_option, #cssify, define_modifier, define_modifiers, define_part, define_parts, define_size, define_sizes, #empty_part_content, #inspect, #part, register_component_initializer, register_component_setup, #rendered_css, #rendered_data, #rendered_html, #rendered_stimulus_controllers, #rendered_tag_name, renders_many, renders_one, set_component_name, #set_loco_parent, #strip_spaces

Constructor Details

#initialize(**kws, &block) ⇒ AccordionSectionComponent

Creates a new accordion section.

Parameters:

  • kws (Hash)

    The keyword arguments for the component.

Options Hash (**kws):

  • title (String)

    The text to display in the section’s title bar. You can also provide a custom title content using the title slot.

  • value (String)

    The value for the radio button that controls this section. Defaults to a random string.

  • checked (Boolean)

    Whether this section should start expanded. Defaults to false.

  • name (String)

    The name attribute for the radio button group. Usually provided by the parent accordion.



78
79
80
81
82
83
84
85
# File 'app/components/daisy/data_display/accordion_component.rb', line 78

def initialize(**kws, &block)
  super

  @value        = config_option(:value)
  @checked      = config_option(:checked, false)
  @simple_title = config_option(:title)
  @name         = config_option(:name)
end

Instance Attribute Details

#simple_titleString (readonly)

Returns Accessor for the ‘title` string passed via the component config.

Returns:

  • (String)

    Accessor for the ‘title` string passed via the component config.



60
61
62
# File 'app/components/daisy/data_display/accordion_component.rb', line 60

def simple_title
  @simple_title
end

Instance Method Details

#before_renderObject



87
88
89
90
91
92
# File 'app/components/daisy/data_display/accordion_component.rb', line 87

def before_render
  setup_component
  setup_radio_button
  setup_title
  setup_content
end

#callObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/components/daisy/data_display/accordion_component.rb', line 117

def call
  part(:component) do
    # Render the radio button which allows opening / closing
    concat(part(:radio_button))

    # Render the title slot, or simple title if provided
    if title?
      concat(title)
    elsif @simple_title.present?
      concat(part(:title) { @simple_title })
    end

    # Render the content
    concat(part(:content) { content })
  end
end

#setup_componentObject



94
95
96
97
98
99
100
101
# File 'app/components/daisy/data_display/accordion_component.rb', line 94

def setup_component
  # Reset the name to the config option or the parent name if available
  @name = config_option(:name, loco_parent.name)

  add_css(:component, "collapse")
  add_css(:component, "collapse-arrow") if loco_parent.config.modifiers.include?(:arrow)
  add_css(:component, "collapse-plus") if loco_parent.config.modifiers.include?(:plus)
end

#setup_contentObject



113
114
115
# File 'app/components/daisy/data_display/accordion_component.rb', line 113

def setup_content
  add_css(:content, "collapse-content")
end

#setup_radio_buttonObject



103
104
105
106
# File 'app/components/daisy/data_display/accordion_component.rb', line 103

def setup_radio_button
  set_tag_name(:radio_button, :input)
  add_html(:radio_button, { type: "radio", name: @name, value: @value, checked: @checked })
end

#setup_titleObject



108
109
110
111
# File 'app/components/daisy/data_display/accordion_component.rb', line 108

def setup_title
  set_tag_name(:title, :h2)
  add_css(:title, "collapse-title text-lg font-bold")
end