Class: LightningUiKit::SelectComponent

Inherits:
BaseComponent
  • Object
show all
Includes:
Errors, Labelable
Defined in:
app/components/lightning_ui_kit/select_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:, form: nil, label: nil, errors: nil, description: nil, disabled: false, options_for_select: [], multiple: false, **options) ⇒ SelectComponent

Returns a new instance of SelectComponent.



5
6
7
8
9
10
11
12
13
14
15
# File 'app/components/lightning_ui_kit/select_component.rb', line 5

def initialize(name:, form: nil, label: nil, errors: nil, description: nil, disabled: false, options_for_select: [], multiple: false, **options)
  @name = name
  @form = form
  @label = label
  @errors = errors
  @multiple = multiple
  @description = description
  @disabled = disabled
  @options_for_select = options_for_select
  @options = options
end

Instance Method Details

#classesObject



17
18
19
# File 'app/components/lightning_ui_kit/select_component.rb', line 17

def 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"
end

#control_classesObject



78
79
80
# File 'app/components/lightning_ui_kit/select_component.rb', line 78

def control_classes
  "lui:relative lui:block lui:w-full lui:before:pointer-events-none lui:before:absolute lui:before:inset-px lui:before:rounded-[7px] lui:before:bg-surface-input lui:before:shadow-sm lui:after:pointer-events-none lui:after:absolute lui:after:inset-0 lui:after:rounded-lg lui:after:ring-inset lui:focus-within:after:ring-2 lui:focus-within:after:ring-focus lui:has-data-disabled:opacity-50 lui:has-data-disabled:before:bg-surface-hover lui:has-data-disabled:before:shadow-none"
end

#control_dataObject



31
32
33
34
35
36
# File 'app/components/lightning_ui_kit/select_component.rb', line 31

def control_data
  {slot: "control"}.merge(@options[:control_data] || {}).dup.tap do |data|
    data[:disabled] = "true" if @disabled
    data[:invalid] = "true" if has_errors?
  end
end

#dataObject



21
22
23
# File 'app/components/lightning_ui_kit/select_component.rb', line 21

def data
  {controller: "lui-field"}.merge(@options[:data] || {})
end

#description_dataObject



50
51
52
53
54
# File 'app/components/lightning_ui_kit/select_component.rb', line 50

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

#error_dataObject



38
39
40
41
42
# File 'app/components/lightning_ui_kit/select_component.rb', line 38

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

#label_dataObject



44
45
46
47
48
# File 'app/components/lightning_ui_kit/select_component.rb', line 44

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

#label_html_optionsObject



56
57
58
59
60
61
# File 'app/components/lightning_ui_kit/select_component.rb', line 56

def label_html_options
  {
    class: "lui:text-base/6 lui:text-foreground lui:select-none lui:data-disabled:opacity-50 lui:sm:text-sm/6",
    data: label_data
  }
end

#render_labelObject



63
64
65
66
67
68
69
70
71
72
# File 'app/components/lightning_ui_kit/select_component.rb', line 63

def render_label
  return unless render_label?

  label_text = effective_label
  if @form
    @form.label(@name, label_text, **label_html_options)
  else
    helpers.label_tag(@name, label_text, **label_html_options)
  end
end

#render_selectObject



90
91
92
93
94
95
96
# File 'app/components/lightning_ui_kit/select_component.rb', line 90

def render_select
  if @form
    @form.select(@name, @options_for_select, {}, select_html_options)
  else
    helpers.select_tag(@name, helpers.options_for_select(@options_for_select), select_html_options)
  end
end

#select_classesObject



74
75
76
# File 'app/components/lightning_ui_kit/select_component.rb', line 74

def select_classes
  "lui:relative lui:block lui:w-full lui:appearance-none lui:rounded-lg lui:py-[calc(--spacing(2.5)-1px)] lui:sm:py-[calc(--spacing(1.5)-1px)] lui:pr-[calc(--spacing(10)-1px)] lui:pl-[calc(--spacing(3.5)-1px)] lui:sm:pr-[calc(--spacing(9)-1px)] lui:sm:pl-[calc(--spacing(3)-1px)] lui:[&_optgroup]:font-semibold 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 lui:data-disabled:border-border-hover lui:data-disabled:opacity-100"
end

#select_dataObject



25
26
27
28
29
# File 'app/components/lightning_ui_kit/select_component.rb', line 25

def select_data
  {lui_field_target: "field"}.merge(@options[:select_data] || {}).tap do |data|
    data[:invalid] = "true" if has_errors?
  end
end

#select_html_optionsObject



82
83
84
85
86
87
88
# File 'app/components/lightning_ui_kit/select_component.rb', line 82

def select_html_options
  {
    multiple: @multiple,
    class: select_classes,
    data: select_data
  }
end