Class: BetterUi::General::Input::Toggle::Component

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

Constant Summary collapse

TOGGLE_THEME =

Costanti con classi Tailwind dirette

{
  default: 'peer-checked:bg-gray-800',
  white: 'peer-checked:bg-white border peer-checked:border-gray-900',
  red: 'peer-checked:bg-red-600',
  rose: 'peer-checked:bg-rose-600',
  orange: 'peer-checked:bg-orange-600',
  green: 'peer-checked:bg-green-600',
  blue: 'peer-checked:bg-blue-600',
  yellow: 'peer-checked:bg-yellow-600',
  violet: 'peer-checked:bg-violet-600'
}.freeze
TOGGLE_TRACK_SIZE =
{
  small: 'w-9 h-5',
  medium: 'w-11 h-6',
  large: 'w-14 h-7'
}.freeze
TOGGLE_THUMB_SIZE =
{
  small: 'w-4 h-4',
  medium: 'w-5 h-5',
  large: 'w-6 h-6'
}.freeze
TOGGLE_THUMB_POSITION =
{
  small: 'peer-checked:translate-x-4',
  medium: 'peer-checked:translate-x-5',
  large: 'peer-checked:translate-x-7'
}.freeze
TOGGLE_BASE_TRACK =
'relative bg-gray-300 rounded-full transition-colors duration-200 ease-in-out'.freeze
TOGGLE_BASE_THUMB =
'bg-white rounded-full shadow-sm transform transition-transform duration-200 ease-in-out absolute top-0.5 left-0.5'.freeze
TOGGLE_LABEL_GAP =
{
  small: 'gap-2',
  medium: 'gap-2.5',
  large: 'gap-3'
}.freeze
TOGGLE_LABEL_TEXT =
{
  small: 'text-sm',
  medium: 'text-base',
  large: 'text-lg'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, value: '1', checked: false, required: false, disabled: false, label: nil, label_position: :right, theme: :default, size: :medium, form: nil, classes: '', **options) ⇒ Component

Returns a new instance of Component.

Parameters:

  • name (String)

    Nome del campo toggle (obbligatorio)

  • value (String) (defaults to: '1')

    Valore del toggle quando è attivo (default: “1”)

  • checked (Boolean) (defaults to: false)

    Se il toggle è attivo

  • required (Boolean) (defaults to: false)

    Se il campo è obbligatorio

  • disabled (Boolean) (defaults to: false)

    Se il campo è disabilitato

  • label (String, nil) (defaults to: nil)

    Testo della label associata al toggle

  • label_position (Symbol) (defaults to: :right)

    Posizione della label (:left, :right)

  • 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)

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

    Form builder Rails opzionale

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

    Classi CSS aggiuntive

  • options (Hash)

    Opzioni aggiuntive per l’input (es. data attributes)



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/components/better_ui/general/input/toggle/component.rb', line 70

def initialize(name:, value: '1', checked: false, required: false, disabled: false, 
               label: nil, label_position: :right, theme: :default, 
               size: :medium, form: nil, classes: '', **options)
  @name = name
  @value = value
  @checked = checked
  @required = required
  @disabled = disabled
  @label = label
  @label_position = label_position.to_sym
  @theme = theme.to_sym
  @size = size.to_sym
  @form = form
  @classes = classes
  @options = options

  validate_params
end

Instance Attribute Details

#checkedObject (readonly)

Returns the value of attribute checked.



54
55
56
# File 'app/components/better_ui/general/input/toggle/component.rb', line 54

def checked
  @checked
end

#classesObject (readonly)

Returns the value of attribute classes.



54
55
56
# File 'app/components/better_ui/general/input/toggle/component.rb', line 54

def classes
  @classes
end

#disabledObject (readonly)

Returns the value of attribute disabled.



54
55
56
# File 'app/components/better_ui/general/input/toggle/component.rb', line 54

def disabled
  @disabled
end

#formObject (readonly)

Returns the value of attribute form.



54
55
56
# File 'app/components/better_ui/general/input/toggle/component.rb', line 54

def form
  @form
end

#labelObject (readonly)

Returns the value of attribute label.



54
55
56
# File 'app/components/better_ui/general/input/toggle/component.rb', line 54

def label
  @label
end

#label_positionObject (readonly)

Returns the value of attribute label_position.



54
55
56
# File 'app/components/better_ui/general/input/toggle/component.rb', line 54

def label_position
  @label_position
end

#nameObject (readonly)

Returns the value of attribute name.



54
55
56
# File 'app/components/better_ui/general/input/toggle/component.rb', line 54

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



54
55
56
# File 'app/components/better_ui/general/input/toggle/component.rb', line 54

def options
  @options
end

#requiredObject (readonly)

Returns the value of attribute required.



54
55
56
# File 'app/components/better_ui/general/input/toggle/component.rb', line 54

def required
  @required
end

#sizeObject (readonly)

Returns the value of attribute size.



54
55
56
# File 'app/components/better_ui/general/input/toggle/component.rb', line 54

def size
  @size
end

#themeObject (readonly)

Returns the value of attribute theme.



54
55
56
# File 'app/components/better_ui/general/input/toggle/component.rb', line 54

def theme
  @theme
end

#valueObject (readonly)

Returns the value of attribute value.



54
55
56
# File 'app/components/better_ui/general/input/toggle/component.rb', line 54

def value
  @value
end