Class: ShadcnPhlexcomponents::Switch

Inherits:
Base
  • Object
show all
Defined in:
lib/shadcn_phlexcomponents/components/switch.rb

Constant Summary

Constants inherited from Base

Base::SANITIZER_ALLOWED_ATTRIBUTES, Base::SANITIZER_ALLOWED_TAGS, Base::TAILWIND_MERGER

Instance Method Summary collapse

Methods inherited from Base

#before_template, #convert_collection_hash_to_struct, #find_as_child, #icon, #item_disabled?, #merge_default_attributes, #merged_as_child_attributes, #nokogiri_attributes_to_hash, #overlay, #sanitize_as_child

Constructor Details

#initialize(name: nil, value: "1", unchecked_value: "0", checked: false, include_hidden: true, disabled: false, **attributes) ⇒ Switch

Returns a new instance of Switch.



19
20
21
22
23
24
25
26
27
# File 'lib/shadcn_phlexcomponents/components/switch.rb', line 19

def initialize(name: nil, value: "1", unchecked_value: "0", checked: false, include_hidden: true, disabled: false, **attributes)
  @name = name
  @value = value
  @unchecked_value = unchecked_value
  @checked = checked
  @include_hidden = include_hidden
  @disabled = disabled
  super(**attributes)
end

Instance Method Details

#default_attributesObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/shadcn_phlexcomponents/components/switch.rb', line 53

def default_attributes
  {
    type: "button",
    role: "switch",
    disabled: @disabled,
    aria: {
      checked: @checked.to_s,
    },
    data: {
      state: @checked ? "checked" : "unchecked",
      controller: "switch",
      action: "click->switch#toggle",
      switch_is_checked_value: @checked.to_s,
    },
  }
end

#view_templateObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/shadcn_phlexcomponents/components/switch.rb', line 29

def view_template(&)
  button(**@attributes) do
    SwitchThumb(checked: @checked)

    if @include_hidden
      input(name: @name, type: "hidden", value: @unchecked_value, autocomplete: "off")
    end

    input(
      type: "checkbox",
      value: @value,
      class: "-translate-x-full pointer-events-none absolute m-0 top-0 left-0 size-4 opacity-0",
      name: @name,
      disabled: @disabled,
      tabindex: -1,
      checked: @checked,
      aria: { hidden: "true" },
      data: {
        switch_target: "input",
      },
    )
  end
end