Class: Daisy::DataInput::CheckboxComponent
- Inherits:
-
LocoMotion::BaseComponent
- Object
- ViewComponent::Base
- LocoMotion::BaseComponent
- Daisy::DataInput::CheckboxComponent
- Defined in:
- app/components/daisy/data_input/checkbox_component.rb
Overview
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
Constant Summary
Constants inherited from LocoMotion::BaseComponent
LocoMotion::BaseComponent::EMPTY_PART_IGNORED_TAGS, LocoMotion::BaseComponent::SELF_CLOSING_TAGS
Instance Attribute Summary collapse
-
#checked ⇒ Object
readonly
Returns the value of attribute checked.
-
#disabled ⇒ Object
readonly
Returns the value of attribute disabled.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#include_hidden ⇒ Object
readonly
Returns the value of attribute include_hidden.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#toggle ⇒ Object
readonly
Returns the value of attribute toggle.
-
#unchecked_value ⇒ Object
readonly
Returns the value of attribute unchecked_value.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Attributes inherited from LocoMotion::BaseComponent
Instance Method Summary collapse
-
#before_render ⇒ Object
Calls the #setup_component method before rendering the component.
-
#initialize(**kws) ⇒ CheckboxComponent
constructor
Instantiate a new Checkbox component.
-
#render_hidden? ⇒ Boolean
Whether to render the companion hidden field.
-
#setup_component ⇒ Object
Sets up the component by configuring the tag name, CSS classes, and HTML attributes.
-
#setup_hidden ⇒ Object
Configures the companion hidden field (see #render_hidden?).
- #setup_labels ⇒ Object
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
Constructor Details
#initialize(**kws) ⇒ CheckboxComponent
Instantiate a new Checkbox component.
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
#checked ⇒ Object (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 |
#disabled ⇒ Object (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 |
#id ⇒ Object (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_hidden ⇒ Object (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 |
#name ⇒ Object (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 |
#required ⇒ Object (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 |
#toggle ⇒ Object (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_value ⇒ Object (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 |
#value ⇒ Object (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_render ⇒ Object
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).
139 140 141 |
# File 'app/components/daisy/data_input/checkbox_component.rb', line 139 def render_hidden? @include_hidden && @name.present? && !@disabled end |
#setup_component ⇒ Object
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_hidden ⇒ Object
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_labels ⇒ Object
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 |