Class: Shadcn::ToggleGroup::Component

Inherits:
ApplicationViewComponent show all
Includes:
Concerns::SubmitsValue
Defined in:
app/components/shadcn/toggle_group/component.rb

Overview

Port of registry/new-york-v4/ui/toggle-group.tsx

React passes variant/size/spacing down through context; here the group exposes them to its items through the slot, which is the same idea.

Constant Summary

Constants inherited from ApplicationViewComponent

ApplicationViewComponent::CONTENTS_STYLE, ApplicationViewComponent::VOID_TAGS

Instance Attribute Summary collapse

Attributes inherited from ApplicationViewComponent

#attributes

Instance Method Summary collapse

Methods included from Concerns::SubmitsValue

included

Methods inherited from ApplicationViewComponent

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

Constructor Details

#initialize(type: :single, variant: :default, size: :default, spacing: 0, value: nil, name: nil, **attributes) ⇒ Component

Returns a new instance of Component.



26
27
28
29
30
31
32
33
34
35
# File 'app/components/shadcn/toggle_group/component.rb', line 26

def initialize(type: :single, variant: :default, size: :default, spacing: 0,
               value: nil, name: nil, **attributes)
  @type = type&.to_sym || :single
  @variant = variant&.to_sym || :default
  @size = size&.to_sym || :default
  @spacing = spacing
  @value = Array(value).compact.join(",")
  @name = name
  super(**attributes)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'app/components/shadcn/toggle_group/component.rb', line 24

def name
  @name
end

#sizeObject (readonly)

Returns the value of attribute size.



24
25
26
# File 'app/components/shadcn/toggle_group/component.rb', line 24

def size
  @size
end

#spacingObject (readonly)

Returns the value of attribute spacing.



24
25
26
# File 'app/components/shadcn/toggle_group/component.rb', line 24

def spacing
  @spacing
end

#typeObject (readonly)

Returns the value of attribute type.



24
25
26
# File 'app/components/shadcn/toggle_group/component.rb', line 24

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



24
25
26
# File 'app/components/shadcn/toggle_group/component.rb', line 24

def value
  @value
end

#variantObject (readonly)

Returns the value of attribute variant.



24
25
26
# File 'app/components/shadcn/toggle_group/component.rb', line 24

def variant
  @variant
end

Instance Method Details

#callObject



49
50
51
# File 'app/components/shadcn/toggle_group/component.rb', line 49

def call
  render_element(body: safe_join([ items, content, hidden_input ].flatten.compact))
end

#element_attributes(**defaults) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'app/components/shadcn/toggle_group/component.rb', line 37

def element_attributes(**defaults)
  super(**{
    "data-variant" => variant,
    "data-size" => size,
    "data-spacing" => spacing,
    style: [ attributes[:style], "--gap: #{spacing}" ].compact.join(";"),
    "data-controller" => "shadcn--toggle-group",
    "data-shadcn--toggle-group-type-value" => type,
    "data-shadcn--toggle-group-value-value" => value
  }.merge(defaults))
end