Class: ShadcnPhlexcomponents::RadioGroup

Inherits:
Base
  • Object
show all
Defined in:
lib/shadcn_phlexcomponents/components/radio_group.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: nil, dir: "ltr", include_hidden: true, **attributes) ⇒ RadioGroup

Returns a new instance of RadioGroup.



14
15
16
17
18
19
20
# File 'lib/shadcn_phlexcomponents/components/radio_group.rb', line 14

def initialize(name: nil, value: nil, dir: "ltr", include_hidden: true, **attributes)
  @name = name
  @value = value
  @dir = dir
  @include_hidden = include_hidden
  super(**attributes)
end

Instance Method Details

#default_attributes ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/shadcn_phlexcomponents/components/radio_group.rb', line 70

def default_attributes
  {
    role: "radiogroup",
    dir: @dir,
    aria: {
      required: false,
    },
    data: {
      controller: "radio-group",
      radio_group_selected_value: @value,
    },
  }
end

#item(name: nil, value: nil, checked: false, **attributes) ⇒ Object



32
33
34
# File 'lib/shadcn_phlexcomponents/components/radio_group.rb', line 32

def item(name: nil, value: nil, checked: false, **attributes)
  RadioGroupItem(name: name || @name, value: value, checked: checked || @value == value, **attributes)
end

#items(collection, value_method:, text_method:, container_class: nil, disabled_items: nil, id_prefix: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/shadcn_phlexcomponents/components/radio_group.rb', line 36

def items(collection, value_method:, text_method:, container_class: nil, disabled_items: nil, id_prefix: nil, &)
  vanish(&)

  container_class = TAILWIND_MERGER.merge("flex items-center gap-3 #{container_class}")

  if collection.first&.is_a?(Hash)
    collection = convert_collection_hash_to_struct(collection, value_method: value_method, text_method: text_method)
  end

  collection.each do |item|
    value = item.public_send(value_method)
    text = item.public_send(text_method)
    id = if id_prefix
      "#{id_prefix.parameterize.underscore}_#{value}"
    else
      "#{@name.parameterize.underscore}_#{value}"
    end

    div(class: container_class) do
      RadioGroupItem(
        name: @name,
        value: value,
        checked: @value == value,
        id: id,
        disabled: item_disabled?(disabled_items, value),
        **@radio_attributes,
      )
      Label(for: id, **@label_attributes) { text }
    end
  end

  nil
end

#label(**attributes) ⇒ Object



22
23
24
25
# File 'lib/shadcn_phlexcomponents/components/radio_group.rb', line 22

def label(**attributes)
  @label_attributes = attributes
  nil
end

#radio(**attributes) ⇒ Object



27
28
29
30
# File 'lib/shadcn_phlexcomponents/components/radio_group.rb', line 27

def radio(**attributes)
  @radio_attributes = attributes
  nil
end

#view_template ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/shadcn_phlexcomponents/components/radio_group.rb', line 84

def view_template(&)
  div(**@attributes) do
    if @include_hidden
      input(type: "hidden", name: @name, autocomplete: "off")
    end

    yield
  end
end