Class: Daisy::DataInput::CheckboxComponent

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

Overview

Note:

Like Rails' own check_box, a named, enabled checkbox also emits a companion hidden field just before the input (an unchecked box submits nothing, so the hidden field supplies the "off" value — "0" by default). Pass include_hidden: false to opt out.

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_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) ⇒ 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.

  • include_hidden (Boolean)

    Whether to render a companion hidden field before the checkbox so an unchecked box still submits a value (mirrors Rails' check_box). Only applies when a name is given and the checkbox is not disabled. Defaults to true.

  • unchecked_value (String)

    The value the companion hidden field submits when the box is unchecked. Defaults to "0".

  • leading (String)

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

  • trailing (String)

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



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/components/daisy/data_input/checkbox_component.rb', line 105

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)
  @include_hidden = config_option(:include_hidden, true)
  @unchecked_value = config_option(:unchecked_value, "0")
end

Instance Attribute Details

#checkedObject (readonly)

Returns the value of attribute checked.



64
65
66
# File 'app/components/daisy/data_input/checkbox_component.rb', line 64

def checked
  @checked
end

#disabledObject (readonly)

Returns the value of attribute disabled.



64
65
66
# File 'app/components/daisy/data_input/checkbox_component.rb', line 64

def disabled
  @disabled
end

#idObject (readonly)

Returns the value of attribute id.



64
65
66
# File 'app/components/daisy/data_input/checkbox_component.rb', line 64

def id
  @id
end

#include_hiddenObject (readonly)

Returns the value of attribute include_hidden.



64
65
66
# File 'app/components/daisy/data_input/checkbox_component.rb', line 64

def include_hidden
  @include_hidden
end

#nameObject (readonly)

Returns the value of attribute name.



64
65
66
# File 'app/components/daisy/data_input/checkbox_component.rb', line 64

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



64
65
66
# File 'app/components/daisy/data_input/checkbox_component.rb', line 64

def required
  @required
end

#toggleObject (readonly)

Returns the value of attribute toggle.



64
65
66
# File 'app/components/daisy/data_input/checkbox_component.rb', line 64

def toggle
  @toggle
end

#unchecked_valueObject (readonly)

Returns the value of attribute unchecked_value.



64
65
66
# File 'app/components/daisy/data_input/checkbox_component.rb', line 64

def unchecked_value
  @unchecked_value
end

#valueObject (readonly)

Returns the value of attribute value.



64
65
66
# File 'app/components/daisy/data_input/checkbox_component.rb', line 64

def value
  @value
end

Instance Method Details

#before_renderObject

Calls the #setup_component method before rendering the component.



122
123
124
125
126
127
128
# File 'app/components/daisy/data_input/checkbox_component.rb', line 122

def before_render
  super

  setup_labels
  setup_hidden
  setup_component
end

#render_hidden?Boolean

Whether to render the companion hidden field. A hidden companion only makes sense for a named, enabled input — Rails likewise omits it for disabled checkboxes (a disabled control submits nothing at all).

Returns:

  • (Boolean)


139
140
141
# File 'app/components/daisy/data_input/checkbox_component.rb', line 139

def render_hidden?
  @include_hidden && @name.present? && !@disabled
end

#setup_componentObject

Sets up the component by configuring the tag name, CSS classes, and HTML attributes.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/components/daisy/data_input/checkbox_component.rb', line 164

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_hiddenObject

Configures the companion hidden field (see #render_hidden?). It shares the checkbox's name and is emitted first, so when the box is checked the checkbox's value wins (the last value with the same name).



148
149
150
151
152
153
154
155
156
157
158
# File 'app/components/daisy/data_input/checkbox_component.rb', line 148

def setup_hidden
  return unless render_hidden?

  set_tag_name(:hidden, :input)
  add_html(:hidden, {
             type: "hidden",
             name: @name,
             value: @unchecked_value,
             autocomplete: "off"
           })
end

#setup_labelsObject



130
131
132
# File 'app/components/daisy/data_input/checkbox_component.rb', line 130

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