Class: Daisy::DataInput::LabelComponent

Inherits:
LocoMotion::BaseComponent show all
Defined in:
app/components/daisy/data_input/label_component.rb

Overview

The Label component renders a DaisyUI styled label for form inputs. It can be used with any form input component and provides visual styling consistent with other Daisy UI elements.

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::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(title = nil, **kws) ⇒ LabelComponent

Instantiate a new Label component.

Parameters:

  • kws (Hash)

    The keyword arguments for the component.

Options Hash (**kws):

  • for (String)

    The ID of the input element this label is for. This connects the label to its associated form control for accessibility.

  • title (String)

    The text content of the label. If not provided, the content block will be used. If a content block is provided, it will take precedence over the title option.

  • required (Boolean)

    Whether the label is for a required input. Defaults to false.



37
38
39
40
41
42
43
# File 'app/components/daisy/data_input/label_component.rb', line 37

def initialize(title = nil, **kws)
  super

  @for = config_option(:for)
  @title = config_option(:title, title)
  @required = config_option(:required, false)
end

Instance Attribute Details

#forObject (readonly)

Returns the value of attribute for.



20
21
22
# File 'app/components/daisy/data_input/label_component.rb', line 20

def for
  @for
end

#requiredObject (readonly)

Returns the value of attribute required.



20
21
22
# File 'app/components/daisy/data_input/label_component.rb', line 20

def required
  @required
end

#titleObject (readonly)

Returns the value of attribute title.



20
21
22
# File 'app/components/daisy/data_input/label_component.rb', line 20

def title
  @title
end

Instance Method Details

#before_renderObject

Calls the #setup_component method before rendering the component.



48
49
50
# File 'app/components/daisy/data_input/label_component.rb', line 48

def before_render
  setup_component
end

#callObject

Renders the component with its content.



72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/components/daisy/data_input/label_component.rb', line 72

def call
  part(:component) do
    if content?
      content
    elsif @title
      (:span, @title, class: "label-text")
    else
      # Fallback to empty content
      ""
    end
  end
end

#setup_componentObject

Sets up the component by configuring the tag name, CSS classes, and HTML attributes. Sets the tag to ‘label’ and adds the ‘label’ CSS class.

This configures the ‘for’ attribute to connect the label to its input and adds appropriate styling for required inputs when needed.



59
60
61
62
63
64
65
66
67
# File 'app/components/daisy/data_input/label_component.rb', line 59

def setup_component
  set_tag_name(:component, :label)

  add_css(:component, "label")

  add_html(:component, {
    for: @for
  })
end