Class: ShadcnPhlexcomponents::Combobox

Inherits:
Base
  • Object
show all
Defined in:
lib/shadcn_phlexcomponents/components/combobox.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, include_blank: false, disabled: false, search_path: nil, search_error_text: "Something went wrong, please try again.", search_empty_text: "No results found", search_placeholder_text: "Search...", **attributes) ⇒ Combobox

Returns a new instance of Combobox.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/shadcn_phlexcomponents/components/combobox.rb', line 14

def initialize(
  id: nil,
  name: nil,
  value: nil,
  placeholder: nil,
  include_blank: false,
  disabled: false,
  search_path: nil,
  search_error_text: "Something went wrong, please try again.",
  search_empty_text: "No results found",
  search_placeholder_text: "Search...",
  **attributes
)
  @id = id
  @name = name
  @value = value
  @placeholder = placeholder
  @include_blank = include_blank
  @disabled = disabled
  @search_path = search_path
  @search_error_text = search_error_text
  @search_empty_text = search_empty_text
  @search_placeholder_text = search_placeholder_text
  @aria_id = "combobox-#{SecureRandom.hex(5)}"
  super(**attributes)
end

Instance Method Details

#content(**attributes) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/shadcn_phlexcomponents/components/combobox.rb', line 52

def content(**attributes, &)
  ComboboxContent(
    include_blank: @include_blank,
    search_error_text: @search_error_text,
    search_empty_text: @search_empty_text,
    search_placeholder_text: @search_placeholder_text,
    aria_id: @aria_id,
    **attributes,
    &
  )
end

#default_attributesObject



120
121
122
123
124
125
126
127
128
129
# File 'lib/shadcn_phlexcomponents/components/combobox.rb', line 120

def default_attributes
  {
    data: {
      aria_id: @aria_id,
      controller: "combobox",
      search_path: @search_path,
      combobox_selected_value: @value,
    },
  }
end

#group(**attributes) ⇒ Object



72
73
74
# File 'lib/shadcn_phlexcomponents/components/combobox.rb', line 72

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

#item(**attributes) ⇒ Object



64
65
66
# File 'lib/shadcn_phlexcomponents/components/combobox.rb', line 64

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

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/shadcn_phlexcomponents/components/combobox.rb', line 76

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

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

  ComboboxContent(
    aria_id: @aria_id,
    include_blank: @include_blank,
    search_error_text: @search_error_text,
    search_empty_text: @search_empty_text,
    search_placeholder_text: @search_placeholder_text,
  ) do
    collection.each do |item|
      value = item.public_send(value_method)
      text = item.public_send(text_method)

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

#label(**attributes) ⇒ Object



68
69
70
# File 'lib/shadcn_phlexcomponents/components/combobox.rb', line 68

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

#trigger(**attributes) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/shadcn_phlexcomponents/components/combobox.rb', line 41

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

#view_templateObject



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/shadcn_phlexcomponents/components/combobox.rb', line 107

def view_template(&)
  div(**@attributes) do
    input(
      type: :hidden,
      name: @name,
      value: @value,
      data: { combobox_target: "hiddenInput" },
    )

    yield
  end
end