Class: Daisy::DataInput::CheckboxComponent

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

Overview

The Checkbox component renders a DaisyUI styled checkbox input. It can be used standalone or with a form builder, and supports various styling options including toggle mode for switch-like appearance.

Direct Known Subclasses

ToggleComponent

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_end_label?, #has_floating_label?, #has_start_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

Constructor Details

#initialize(**kws) ⇒ CheckboxComponent

Instantiate a new Checkbox component.

Parameters:

  • kws (Hash)

    The keyword arguments for the component.

Options Hash (**kws):

  • name (String)

    The name attribute for the checkbox input.

  • id (String)

    The ID attribute for the checkbox input.

  • value (String)

    The value attribute for the checkbox input. Defaults to “1”.

  • checked (Boolean)

    Whether the checkbox is checked. Defaults to false.

  • toggle (Boolean)

    Whether the checkbox should be styled as a toggle switch. Defaults to false.

  • disabled (Boolean)

    Whether the checkbox is disabled. Defaults to false.

  • required (Boolean)

    Whether the checkbox is required for form validation. Defaults to false.



61
62
63
64
65
66
67
68
69
70
71
# File 'app/components/daisy/data_input/checkbox_component.rb', line 61

def initialize(**kws)
  super

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

Instance Attribute Details

#checkedObject (readonly)

Returns the value of attribute checked.



35
36
37
# File 'app/components/daisy/data_input/checkbox_component.rb', line 35

def checked
  @checked
end

#disabledObject (readonly)

Returns the value of attribute disabled.



35
36
37
# File 'app/components/daisy/data_input/checkbox_component.rb', line 35

def disabled
  @disabled
end

#idObject (readonly)

Returns the value of attribute id.



35
36
37
# File 'app/components/daisy/data_input/checkbox_component.rb', line 35

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



35
36
37
# File 'app/components/daisy/data_input/checkbox_component.rb', line 35

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



35
36
37
# File 'app/components/daisy/data_input/checkbox_component.rb', line 35

def required
  @required
end

#toggleObject (readonly)

Returns the value of attribute toggle.



35
36
37
# File 'app/components/daisy/data_input/checkbox_component.rb', line 35

def toggle
  @toggle
end

#valueObject (readonly)

Returns the value of attribute value.



35
36
37
# File 'app/components/daisy/data_input/checkbox_component.rb', line 35

def value
  @value
end

Instance Method Details

#before_renderObject

Calls the #setup_component method before rendering the component.



76
77
78
79
80
81
# File 'app/components/daisy/data_input/checkbox_component.rb', line 76

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.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/components/daisy/data_input/checkbox_component.rb', line 91

def setup_component
  set_tag_name(:component, :input)

  add_css(:component, @toggle ? "toggle" : "checkbox")

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

#setup_labelsObject



83
84
85
# File 'app/components/daisy/data_input/checkbox_component.rb', line 83

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