Class: Shadcn::RadioGroup::Item::Component

Inherits:
ApplicationViewComponent show all
Defined in:
app/components/shadcn/radio_group/item/component.rb

Overview

RadioGroupItem

Constant Summary collapse

INDICATOR_CLASSES =
"relative flex items-center justify-center"
DOT_CLASSES =
"absolute top-1/2 left-1/2 size-2 -translate-x-1/2 -translate-y-1/2 " \
"fill-primary"

Constants inherited from ApplicationViewComponent

ApplicationViewComponent::CONTENTS_STYLE, ApplicationViewComponent::VOID_TAGS

Instance Attribute Summary collapse

Attributes inherited from ApplicationViewComponent

#attributes

Instance Method Summary collapse

Methods inherited from ApplicationViewComponent

#css_classes, default_tag, #merged_style, #render_element, #shadcn_t, slot_name, #style_variants, #tag_name

Constructor Details

#initialize(value:, checked: false, disabled: false, **attributes) ⇒ Component

Returns a new instance of Component.



28
29
30
31
32
33
# File 'app/components/shadcn/radio_group/item/component.rb', line 28

def initialize(value:, checked: false, disabled: false, **attributes)
  @value = value.to_s
  @checked = checked
  @disabled = disabled
  super(**attributes)
end

Instance Attribute Details

#checkedObject (readonly)

Returns the value of attribute checked.



26
27
28
# File 'app/components/shadcn/radio_group/item/component.rb', line 26

def checked
  @checked
end

#disabledObject (readonly)

Returns the value of attribute disabled.



26
27
28
# File 'app/components/shadcn/radio_group/item/component.rb', line 26

def disabled
  @disabled
end

#valueObject (readonly)

Returns the value of attribute value.



26
27
28
# File 'app/components/shadcn/radio_group/item/component.rb', line 26

def value
  @value
end

Instance Method Details

#callObject



56
57
58
# File 'app/components/shadcn/radio_group/item/component.rb', line 56

def call
  render_element(body: indicator)
end

#element_attributes(**defaults) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/components/shadcn/radio_group/item/component.rb', line 39

def element_attributes(**defaults)
  super(**{
    type: "button",
    role: "radio",
    value: value,
    disabled: (true if disabled),
    tabindex: (checked ? "0" : "-1"),
    "aria-checked" => checked.to_s,
    "data-state" => state,
    "data-value" => value,
    "data-disabled" => (true if disabled),
    "data-shadcn--radio-group-target" => "item",
    "data-action" => "click->shadcn--radio-group#select " \
                     "keydown->shadcn--radio-group#keydown"
  }.merge(defaults))
end

#stateObject



35
36
37
# File 'app/components/shadcn/radio_group/item/component.rb', line 35

def state
  checked ? "checked" : "unchecked"
end