Class: CheckboxComponent

Inherits:
Component show all
Defined in:
app/components/checkbox_component.rb

Overview

Checkbox — checkboxes, radios, toggles, sliders.

Usage:

Checkbox(label_text: "Accept terms", name: "terms")
Checkbox(type: :toggle, label_text: "Dark mode", checked: true)
Checkbox(type: :radio, label_text: "Option A", name: "choice", value: "a")

Constant Summary

Constants inherited from Component

Component::HTML_OPTIONS

Instance Method Summary collapse

Methods inherited from Component

default, #initialize, #render_in, slot

Constructor Details

This class inherits a constructor from Component

Instance Method Details

#to_sObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/components/checkbox_component.rb', line 20

def to_s
  classes = class_names(
    "ui",
    (type unless type == "checkbox"),
    { "fitted" => fitted, "read-only" => read_only, "disabled" => disabled },
    "checkbox"
  )

  field_type = type == "radio" ? "radio" : "checkbox"
  input_opts = { type: field_type }
  input_opts[:name] = name if name
  input_opts[:value] = value if value
  input_opts[:checked] = "checked" if checked
  input_opts[:disabled] = "disabled" if disabled

  label_el = tag.label { label_text || "" }

  tag.div(class: classes, data: { controller: "fui-checkbox" }) {
    safe_join([ tag.input(**input_opts), label_el ])
  }
end