Module: LocoMotion::Concerns::LabelableComponent

Extended by:
ActiveSupport::Concern
Included in:
Daisy::DataInput::CallyInputComponent, Daisy::DataInput::CheckboxComponent, Daisy::DataInput::SelectComponent, Daisy::DataInput::TextInputComponent
Defined in:
lib/loco_motion/concerns/labelable_component.rb

Overview

Can be included in relevant components to add labeling functionality. This adds support for start, end, and floating labels that can either be provided as plain text or customized via slots.

Instance Method Summary collapse

Instance Method Details

#before_renderObject

Sets up the tag names for the label parts before rendering the component. This method is called automatically during the component rendering lifecycle.

Note that CSS classes for labels must be handled by the implementing component since requirements differ for each type of input component.



96
97
98
99
100
101
102
103
# File 'lib/loco_motion/concerns/labelable_component.rb', line 96

def before_render
  super

  set_tag_name(:label_wrapper, :label)
  set_tag_name(:start, :span)
  set_tag_name(:end, :span)
  set_tag_name(:floating, :span)
end

#has_any_label?Boolean

Checks if any type of label is present.

Returns:

  • (Boolean)

    true if any label is present, false otherwise



110
111
112
# File 'lib/loco_motion/concerns/labelable_component.rb', line 110

def has_any_label?
  has_start_label? || has_end_label? || has_floating_label?
end

#has_end_label?Boolean

Checks if an end label is present.

Returns:

  • (Boolean)

    true if end label is present, false otherwise



128
129
130
# File 'lib/loco_motion/concerns/labelable_component.rb', line 128

def has_end_label?
  end? || @end || config_option(:end).present?
end

#has_floating_label?Boolean

Checks if a floating label is present.

Returns:

  • (Boolean)

    true if floating label is present, false otherwise



137
138
139
# File 'lib/loco_motion/concerns/labelable_component.rb', line 137

def has_floating_label?
  floating? || @floating || config_option(:floating).present?
end

#has_start_label?Boolean

Checks if a start label is present.

Returns:

  • (Boolean)

    true if start label is present, false otherwise



119
120
121
# File 'lib/loco_motion/concerns/labelable_component.rb', line 119

def has_start_label?
  start? || @start || config_option(:start).present?
end

#initialize(*instance_args, **instance_kws, &instance_block) ⇒ Object

Initializes the component and sets up the label options.

Parameters:

  • instance_args (Array)

    Positional arguments passed to the component

  • instance_kws (Hash)

    Keyword arguments passed to the component

  • instance_block (Proc)

    Block passed to the component for rendering custom content

Options Hash (**instance_kws):

  • :start (String, nil)

    Text to display in the start label position

  • :end (String, nil)

    Text to display in the end label position

  • :floating (String, nil)

    Text to display in the floating label position

  • :placeholder (String, nil)

    The input’s placeholder text. If not provided and ‘floating_placeholder` is set, it will use that value.

  • :floating_placeholder (String, nil)

    Text to use for both the floating label and the input placeholder. This is a convenience option that sets both the ‘floating` and `placeholder` options to the same value. Both `floating` and `placeholder` take precedence over `floating_placeholder`.



77
78
79
80
81
82
83
84
85
86
# File 'lib/loco_motion/concerns/labelable_component.rb', line 77

def initialize(*instance_args, **instance_kws, &instance_block)
  super(*instance_args, **instance_kws, &instance_block)

  @floating_placeholder = config_option(:floating_placeholder)

  @start = config_option(:start)
  @end = config_option(:end)
  @floating = config_option(:floating, @floating_placeholder)
  @placeholder = config_option(:placeholder, @floating_placeholder)
end