Class: ShadcnPhlexcomponents::Select

Inherits:
Base
  • Object
show all
Defined in:
lib/shadcn_phlexcomponents/components/select.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(id: nil, name: nil, value: nil, placeholder: nil, native: false, include_blank: false, disabled: false, **attributes) ⇒ Select

Returns a new instance of Select.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/shadcn_phlexcomponents/components/select.rb', line 7

def initialize(
  id: nil,
  name: nil,
  value: nil,
  placeholder: nil,
  native: false,
  include_blank: false,
  disabled: false,
  **attributes
)
  @id = id
  @name = name
  @value = value
  @placeholder = placeholder
  @native = native
  @include_blank = include_blank
  @disabled = disabled
  @aria_id = "select-#{SecureRandom.hex(5)}"
  super(**attributes)
end

Instance Method Details

#build_native_options(content_element) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/shadcn_phlexcomponents/components/select.rb', line 145

def build_native_options(content_element)
  content_element.children.each do |content_child|
    next if content_child.is_a?(Nokogiri::XML::Text) || content_child.is_a?(Nokogiri::XML::Comment)

    if content_child.attributes["data-select-target"]&.value == "group"
      group_label = content_child.at_css('[data-shadcn-phlexcomponents="select-label"]')&.text

      optgroup(label: group_label, class: NATIVE_OPTION_STYLES) do
        content_child.css('[data-select-target="item"]').each do |i|
          option(
            value: i.attributes["data-value"].value,
            class: NATIVE_OPTION_STYLES,
            selected: i.attributes["data-value"].value == @value,
            disabled: i.attributes["data-disabled"]&.value == "",
          ) do
            i.text
          end
        end
      end
    elsif content_child.attributes["data-select-target"]&.value == "item"

      option(
        value: content_child.attributes["data-value"].value,
        class: NATIVE_OPTION_STYLES,
        selected: content_child.attributes["data-value"].value == @value,
        disabled: content_child.attributes["data-disabled"]&.value == "",
      ) do
        content_child.text
      end
    end
  end
end

#class_variants(**args) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/shadcn_phlexcomponents/components/select.rb', line 28

def class_variants(**args)
  if @native
    Input.new.class_variants(class: "appearance-none #{args[:class]}")
  else
    TAILWIND_MERGER.merge("w-full #{args[:class]}")
  end
end

#content(**attributes) ⇒ Object



47
48
49
50
51
# File 'lib/shadcn_phlexcomponents/components/select.rb', line 47

def content(**attributes, &)
  SelectContent(
    aria_id: @aria_id, include_blank: @include_blank, native: @native, **attributes, &
  )
end

#default_attributesObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/shadcn_phlexcomponents/components/select.rb', line 127

def default_attributes
  if @native
    {
      id: @id,
      name: @name,
      disabled: @disabled,
    }
  else
    {
      data: {
        aria_id: @aria_id,
        controller: "select",
        select_selected_value: @value,
      },
    }
  end
end

#group(**attributes) ⇒ Object



61
62
63
# File 'lib/shadcn_phlexcomponents/components/select.rb', line 61

def group(**attributes, &)
  SelectGroup(aria_id: @aria_id, **attributes, &)
end

#item(**attributes) ⇒ Object



53
54
55
# File 'lib/shadcn_phlexcomponents/components/select.rb', line 53

def item(**attributes, &)
  SelectItem(aria_id: @aria_id, **attributes, &)
end

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/shadcn_phlexcomponents/components/select.rb', line 65

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

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

  SelectTrigger(
    id: @id,
    aria_id: @aria_id,
    value: @value,
    placeholder: @placeholder,
    disabled: @disabled,
  )

  SelectContent(aria_id: @aria_id, include_blank: @include_blank, native: @native) do
    collection.each do |item|
      value = item.public_send(value_method)
      text = item.public_send(text_method)

      SelectItem(value: value, aria_id: @aria_id, disabled: item_disabled?(disabled_items, value)) { text }
    end
  end
end

#label(**attributes) ⇒ Object



57
58
59
# File 'lib/shadcn_phlexcomponents/components/select.rb', line 57

def label(**attributes, &)
  SelectLabel(**attributes, &)
end

#trigger(**attributes) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/shadcn_phlexcomponents/components/select.rb', line 36

def trigger(**attributes)
  SelectTrigger(
    id: @id,
    aria_id: @aria_id,
    value: @value,
    placeholder: @placeholder,
    disabled: @disabled,
    **attributes,
  )
end

#view_templateObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/shadcn_phlexcomponents/components/select.rb', line 90

def view_template(&)
  content = capture(&)
  element = Nokogiri::HTML.fragment(content.to_s)
  content_element = element.css('[data-select-target="content"]')

  if @native
    div(class: "relative") do
      select(**@attributes) do
        if @placeholder || @include_blank
          option(value: "", class: NATIVE_OPTION_STYLES) { @placeholder }
        end

        build_native_options(content_element)
      end

      icon("chevron-down", class: "size-4 absolute opacity-50 top-1/2 -translate-y-1/2 right-3 pointer-events-none")
    end
  else
    div(**@attributes) do
      yield

      select(
        name: @name,
        disabled: @disabled,
        class: "sr-only",
        tabindex: -1,
        data: {
          select_target: "select",
        },
      ) do
        option(value: "")
        build_native_options(content_element)
      end
    end
  end
end