Class: BetterUi::General::Input::Select::Component

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/better_ui/general/input/select/component.rb

Constant Summary collapse

SELECT_THEME =

Costanti con classi Tailwind dirette

{
  default: 'border-gray-300 text-gray-800 focus:border-gray-600 focus:ring-gray-600',
  white: 'border-gray-300 text-gray-900 focus:border-gray-500 focus:ring-gray-500',
  red: 'border-gray-300 text-red-600 focus:border-red-500 focus:ring-red-500',
  rose: 'border-gray-300 text-rose-600 focus:border-rose-500 focus:ring-rose-500',
  orange: 'border-gray-300 text-orange-600 focus:border-orange-500 focus:ring-orange-500',
  green: 'border-gray-300 text-green-600 focus:border-green-500 focus:ring-green-500',
  blue: 'border-gray-300 text-blue-600 focus:border-blue-500 focus:ring-blue-500',
  yellow: 'border-gray-300 text-yellow-600 focus:border-yellow-500 focus:ring-yellow-500',
  violet: 'border-gray-300 text-violet-600 focus:border-violet-500 focus:ring-violet-500'
}.freeze
SELECT_SIZE =
{
  small: 'px-2 py-1.5 text-xs',
  medium: 'px-3 py-2 text-sm',
  large: 'px-4 py-3 text-base'
}.freeze
SELECT_ROUNDED =
{
  none: 'rounded-none',
  small: 'rounded-sm',
  medium: 'rounded',
  large: 'rounded-lg',
  full: 'rounded-full'
}.freeze
SELECT_BASE_CLASSES =
'block w-full bg-white border-2 shadow-sm focus:outline-none focus:ring-2 focus:ring-offset-2 transition-colors duration-200'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, options:, selected: nil, required: false, disabled: false, multiple: false, searchable: true, theme: :default, size: :medium, rounded: :medium, placeholder: nil, search_placeholder: nil, max_height: "300px", form: nil, classes: '', **html_options) ⇒ Component

Returns a new instance of Component.

Parameters:

  • name (String)

    Nome del campo select (obbligatorio)

  • options (Array<Hash>)

    Array di opzioni nel formato [‘value’, label: ‘label’, …]

  • selected (String, Array, nil) (defaults to: nil)

    Valore/i selezionato/i

  • required (Boolean) (defaults to: false)

    Se il campo è obbligatorio

  • disabled (Boolean) (defaults to: false)

    Se il campo è disabilitato

  • multiple (Boolean) (defaults to: false)

    Se multiple opzioni possono essere selezionate

  • searchable (Boolean) (defaults to: true)

    Se abilitare la ricerca

  • theme (Symbol) (defaults to: :default)

    Tema del componente (:default, :white, :red, :rose, :orange, :green, :blue, :yellow, :violet)

  • size (Symbol) (defaults to: :medium)

    Dimensione del componente (:small, :medium, :large)

  • rounded (Symbol) (defaults to: :medium)

    Border radius (:none, :small, :medium, :large, :full)

  • placeholder (String) (defaults to: nil)

    Testo placeholder per il trigger

  • search_placeholder (String) (defaults to: nil)

    Testo placeholder per il campo search

  • max_height (String) (defaults to: "300px")

    Altezza massima del dropdown (default: “300px”)

  • form (ActionView::Helpers::FormBuilder, nil) (defaults to: nil)

    Form builder Rails opzionale

  • classes (String) (defaults to: '')

    Classi CSS aggiuntive

  • html_options (Hash)

    Attributi HTML aggiuntivi



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/components/better_ui/general/input/select/component.rb', line 57

def initialize(
  name:, 
  options:, 
  selected: nil, 
  required: false, 
  disabled: false, 
  multiple: false,
  searchable: true,
  theme: :default, 
  size: :medium, 
  rounded: :medium, 
  placeholder: nil,
  search_placeholder: nil,
  max_height: "300px",
  form: nil,
  classes: '', 
  **html_options
)
  @name = name
  @options = options
  @selected = normalize_selected(selected)
  @required = required
  @disabled = disabled
  @multiple = multiple
  @searchable = searchable
  @theme = theme.to_sym
  @size = size.to_sym
  @rounded = rounded.to_sym
  @placeholder = placeholder || (multiple ? "Seleziona opzioni..." : "Seleziona...")
  @search_placeholder = search_placeholder || "Cerca..."
  @max_height = max_height
  @form = form
  @classes = classes
  @html_options = html_options

  validate_params
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def classes
  @classes
end

#disabledObject (readonly)

Returns the value of attribute disabled.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def disabled
  @disabled
end

#formObject (readonly)

Returns the value of attribute form.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def form
  @form
end

#html_optionsObject (readonly)

Returns the value of attribute html_options.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def html_options
  @html_options
end

#max_heightObject (readonly)

Returns the value of attribute max_height.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def max_height
  @max_height
end

#multipleObject (readonly)

Returns the value of attribute multiple.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def multiple
  @multiple
end

#nameObject (readonly)

Returns the value of attribute name.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def options
  @options
end

#placeholderObject (readonly)

Returns the value of attribute placeholder.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def placeholder
  @placeholder
end

#requiredObject (readonly)

Returns the value of attribute required.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def required
  @required
end

#roundedObject (readonly)

Returns the value of attribute rounded.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def rounded
  @rounded
end

#search_placeholderObject (readonly)

Returns the value of attribute search_placeholder.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def search_placeholder
  @search_placeholder
end

#searchableObject (readonly)

Returns the value of attribute searchable.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def searchable
  @searchable
end

#selectedObject (readonly)

Returns the value of attribute selected.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def selected
  @selected
end

#sizeObject (readonly)

Returns the value of attribute size.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def size
  @size
end

#themeObject (readonly)

Returns the value of attribute theme.



37
38
39
# File 'app/components/better_ui/general/input/select/component.rb', line 37

def theme
  @theme
end