Class: Daisy::DataInput::RadioButtonComponent

Inherits:
LocoMotion::BaseComponent show all
Includes:
LocoMotion::Concerns::AriableComponent, LocoMotion::Concerns::LabelableComponent
Defined in:
app/components/daisy/data_input/radio_button_component.rb

Overview

The Radio Button component renders a DaisyUI styled radio button input. It can be used standalone or with a form builder, and is ideal for creating option groups where users must select exactly one choice.

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 included from LocoMotion::Concerns::LabelableComponent

#has_any_label?, #has_floating_label?, #has_leading_label?, #has_trailing_label?

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

Methods included from LocoMotion::Concerns::InspectableComponent

#build_inspect_string

Constructor Details

#initialize(**kws) ⇒ RadioButtonComponent

Instantiate a new Radio Button component.

Parameters:

  • kws (Hash)

    The keyword arguments for the component.

Options Hash (**kws):

  • name (String)

    The name attribute for the radio button input.

  • id (String)

    The ID attribute for the radio button input.

  • value (String)

    The value attribute for the radio button input.

  • checked (Boolean)

    Whether the radio button is checked. Defaults to false.

  • disabled (Boolean)

    Whether the radio button is disabled. Defaults to false.

  • required (Boolean)

    Whether the radio button is required for form validation. Defaults to false.

  • leading (String)

    Text to display in the leading label position (before the radio button).

  • trailing (String)

    Text to display in the trailing label position (after the radio button).



79
80
81
82
83
84
85
86
87
88
# File 'app/components/daisy/data_input/radio_button_component.rb', line 79

def initialize(**kws)
  super

  @name = config_option(:name)
  @id = config_option(:id)
  @value = config_option(:value)
  @checked = config_option(:checked, false)
  @disabled = config_option(:disabled, false)
  @required = config_option(:required, false)
end

Instance Attribute Details

#checkedObject (readonly)

Returns the value of attribute checked.



51
52
53
# File 'app/components/daisy/data_input/radio_button_component.rb', line 51

def checked
  @checked
end

#disabledObject (readonly)

Returns the value of attribute disabled.



51
52
53
# File 'app/components/daisy/data_input/radio_button_component.rb', line 51

def disabled
  @disabled
end

#idObject (readonly)

Returns the value of attribute id.



51
52
53
# File 'app/components/daisy/data_input/radio_button_component.rb', line 51

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



51
52
53
# File 'app/components/daisy/data_input/radio_button_component.rb', line 51

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



51
52
53
# File 'app/components/daisy/data_input/radio_button_component.rb', line 51

def required
  @required
end

#valueObject (readonly)

Returns the value of attribute value.



51
52
53
# File 'app/components/daisy/data_input/radio_button_component.rb', line 51

def value
  @value
end

Instance Method Details

#before_renderObject

Calls the #setup_component method before rendering the component.



93
94
95
96
97
98
# File 'app/components/daisy/data_input/radio_button_component.rb', line 93

def before_render
  super

  setup_labels
  setup_component
end

#setup_componentObject

Sets up the component by configuring the tag name, CSS classes, and HTML attributes. Sets the tag to input with type 'radio' and adds the 'radio' CSS class.

This configures the name, id, value, disabled state, required state, and checked state of the radio button.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/components/daisy/data_input/radio_button_component.rb', line 111

def setup_component
  set_tag_name(:component, :input)

  add_css(:component, "radio") unless @skip_styling

  add_html(:component, {
             type: "radio",
             name: @name,
             id: @id,
             value: @value,
             checked: @checked,
             disabled: @disabled,
             required: @required
           })
end

#setup_labelsObject



100
101
102
# File 'app/components/daisy/data_input/radio_button_component.rb', line 100

def setup_labels
  add_css(:label_wrapper, "label") if has_any_label?
end