Class: LightningUiKit::ComboboxComponent

Inherits:
BaseComponent
  • Object
show all
Includes:
Errors, Labelable
Defined in:
app/components/lightning_ui_kit/combobox_component.rb

Instance Method Summary collapse

Methods included from Labelable

#effective_label, #render_label?

Methods included from Errors

#error_messages, #has_errors?, #infer_errors_from_association

Methods inherited from BaseComponent

#merge_classes

Methods included from HeroiconHelper

#heroicon

Constructor Details

#initialize(name: nil, form: nil, label: nil, description: nil, placeholder: nil, disabled: false, error: nil, multiple: false, allow_custom: false, options: [], selected: nil, url: nil, min_chars: 0, debounce: 300, association: nil, foreign_key: nil, nested_model: nil, collection: nil, label_method: :to_s, value_method: :id, **options_hash) ⇒ ComboboxComponent

Returns a new instance of ComboboxComponent.



5
6
7
8
9
10
11
12
13
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 5

def initialize(
  name: nil,
  form: nil,
  label: nil,
  description: nil,
  placeholder: nil,
  disabled: false,
  error: nil,
  multiple: false,
  allow_custom: false,
  options: [],
  selected: nil,
  url: nil,
  min_chars: 0,
  debounce: 300,
  association: nil,
  foreign_key: nil,
  nested_model: nil,
  collection: nil,
  label_method: :to_s,
  value_method: :id,
  **options_hash
)
  @form = form
  @label = label
  @description = description
  @placeholder = placeholder
  @disabled = disabled
  @error = error
  @allow_custom = allow_custom
  @url = url
  @min_chars = min_chars
  @debounce = debounce
  @options_hash = options_hash

  @association = association
  @foreign_key = foreign_key
  @nested_model = nested_model
  @collection = collection
  @label_method = label_method
  @value_method = value_method

  if association_mode?
    @name = nested_attributes_name
    @multiple = true
    @options = derive_options_from_collection
    @selected = derive_selected_from_association
  else
    @name = name
    @multiple = multiple
    @options = options
    @selected = selected
  end
end

Instance Method Details

#association_mode?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 99

def association_mode?
  @association.present? && @foreign_key.present?
end

#classesObject



60
61
62
63
64
65
66
67
68
69
70
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 60

def classes
  merge_classes([
    "lui:[&>[data-slot=label]+[data-slot=control]]:mt-3",
    "lui:[&>[data-slot=label]+[data-slot=description]]:mt-1",
    "lui:[&>[data-slot=description]+[data-slot=control]]:mt-3",
    "lui:[&>[data-slot=control]+[data-slot=description]]:mt-3",
    "lui:[&>[data-slot=control]+[data-slot=error]]:mt-3",
    "lui:*:data-[slot=label]:font-medium",
    @options_hash[:class]
  ].compact.join(" "))
end

#combobox_valuesObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 80

def combobox_values
  base = {
    "lui-combobox-multiple-value" => @multiple,
    "lui-combobox-allow-custom-value" => @allow_custom,
    "lui-combobox-url-value" => @url,
    "lui-combobox-min-chars-value" => @min_chars,
    "lui-combobox-debounce-value" => @debounce,
    "lui-combobox-options-value" => options_json,
    "lui-combobox-selected-value" => selected_json
  }

  if association_mode?
    base["lui-combobox-foreign-key-value"] = @foreign_key.to_s
    base["lui-combobox-nested-model-value"] = @nested_model.to_s if @nested_model
  end

  base.compact
end

#dataObject



72
73
74
75
76
77
78
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 72

def data
  default_data = {
    controller: "lui-combobox",
    action: "click@window->lui-combobox#clickOutside"
  }
  default_data.merge(@options_hash[:data] || {}).merge(combobox_values)
end

#description_dataObject



132
133
134
135
136
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 132

def description_data
  {slot: "description"}.merge(@options_hash[:description_data] || {}).tap do |data|
    data[:disabled] = "true" if @disabled
  end
end

#error_dataObject



138
139
140
141
142
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 138

def error_data
  {slot: "error"}.merge(@options_hash[:error_data] || {}).tap do |data|
    data[:disabled] = "true" if @disabled
  end
end

#hidden_field_nameObject



144
145
146
147
148
149
150
151
152
153
154
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 144

def hidden_field_name
  base_name = scoped_field_name

  if association_mode?
    base_name
  elsif @multiple
    "#{base_name}[]"
  else
    base_name
  end
end

#input_classesObject



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 112

def input_classes
  merge_classes([
    "lui:relative lui:block lui:w-full lui:appearance-none lui:rounded-lg",
    "lui:px-[calc(--spacing(3.5)-1px)] lui:py-[calc(--spacing(2.5)-1px)]",
    "lui:sm:px-[calc(--spacing(3)-1px)] lui:sm:py-[calc(--spacing(1.5)-1px)]",
    "lui:text-base/6 lui:text-foreground lui:placeholder:text-foreground-muted lui:sm:text-sm/6",
    "lui:border lui:border-border lui:data-[hover]:border-border-hover",
    "lui:bg-transparent lui:focus:outline-hidden",
    "lui:data-invalid:border-destructive-border lui:data-invalid:data-[hover]:border-destructive-border/60",
    "lui:data-disabled:border-border-hover",
    "lui:pr-10"
  ].join(" "))
end

#input_dataObject



103
104
105
106
107
108
109
110
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 103

def input_data
  {
    "lui-combobox-target" => "input",
    :action => "input->lui-combobox#onInput focus->lui-combobox#onFocus keydown->lui-combobox#onKeydown"
  }.tap do |d|
    d[:invalid] = "true" if has_errors?
  end
end

#label_dataObject



126
127
128
129
130
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 126

def label_data
  {slot: "label"}.merge(@options_hash[:label_data] || {}).tap do |data|
    data[:disabled] = "true" if @disabled
  end
end

#scoped_field_nameObject



156
157
158
159
160
161
162
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 156

def scoped_field_name
  if @form
    "#{@form.object_name}[#{@name}]"
  else
    @name.to_s
  end
end