Class: Tramway::Form::TramwaySelectComponent

Inherits:
TailwindComponent show all
Defined in:
app/components/tramway/form/tramway_select_component.rb

Overview

Tailwind-styled multi-select field

Constant Summary

Constants inherited from TailwindComponent

TailwindComponent::SIZE_CLASSES

Constants included from Helpers::ViewsHelper

Helpers::ViewsHelper::FORM_SIZES

Instance Method Summary collapse

Methods included from Helpers::ViewsHelper

#tramway_back_button, #tramway_badge, #tramway_button, #tramway_cell, #tramway_chat, #tramway_container, #tramway_flash, #tramway_form_for, #tramway_header, #tramway_main_container, #tramway_row, #tramway_table, #tramway_title

Methods included from Helpers::ComponentHelper

#component

Methods included from Helpers::DecorateHelper

#tramway_decorate

Instance Method Details

#autocomplete_inputObject

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize



43
44
45
# File 'app/components/tramway/form/tramway_select_component.rb', line 43

def autocomplete_input
  component 'tramway/form/tramway_select/autocomplete_input'
end

#before_renderObject



11
12
13
14
15
# File 'app/components/tramway/form/tramway_select_component.rb', line 11

def before_render
  @collection = collection.map do |(text, value)|
    { text:, value: }
  end.to_json
end

#controllerObject



56
57
58
59
60
61
# File 'app/components/tramway/form/tramway_select_component.rb', line 56

def controller
  controllers = ['tramway-select']
  controllers << external_action.split('->').last.split('#').first if external_action
  controllers += external_controllers
  controllers.join(' ')
end


63
64
65
66
67
68
69
# File 'app/components/tramway/form/tramway_select_component.rb', line 63

def dropdown_data
  (options[:data] || {}).merge(
    'tramway-select-target' => 'dropdown',
    'dropdown-container' => dropdown_container,
    'item-container' => item_container
  )
end


71
72
73
# File 'app/components/tramway/form/tramway_select_component.rb', line 71

def dropdown_options
  options.except(:data).merge(class: input_classes)
end

#input_classesObject



75
76
77
# File 'app/components/tramway/form/tramway_select_component.rb', line 75

def input_classes
  "#{size_class(:tramway_select_input)} #{select_base_classes}"
end

#tramway_select_classesObject



47
48
49
50
51
52
53
54
# File 'app/components/tramway/form/tramway_select_component.rb', line 47

def tramway_select_classes
  classes = ''

  classes += 'select--multiple ' if multiple
  classes += 'select--autocomplete ' if autocomplete

  classes
end

#tramway_select_hashObject

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/components/tramway/form/tramway_select_component.rb', line 19

def tramway_select_hash
  default = {
    controller:,
    selected_item_template:,
    tramway_select_selected_items_value:,
    dropdown_container:,
    item_container:,
    items:,
    action:,
    select_as_input:,
    placeholder:,
    value:,
    on_change:,
    multiple: multiple.to_s,
    autocomplete: autocomplete.to_s
  }.transform_keys { |key| key.to_s.gsub('_', '-') }

  default.merge!(autocomplete_input:) if autocomplete

  default
end