Class: BetterUi::General::Input::Checkbox::Component

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

Constant Summary collapse

CHECKBOX_THEME =

Costanti con classi Tailwind dirette

{
  default: 'border-gray-300 text-gray-800 focus:border-gray-600 focus:ring-gray-600 checked:bg-gray-800 checked:border-gray-800',
  white: 'border-gray-300 text-gray-900 focus:border-gray-500 focus:ring-gray-500 checked:bg-white checked:border-gray-900 checked:text-gray-900',
  red: 'border-gray-300 text-red-600 focus:border-red-500 focus:ring-red-500 checked:bg-red-600 checked:border-red-600',
  rose: 'border-gray-300 text-rose-600 focus:border-rose-500 focus:ring-rose-500 checked:bg-rose-600 checked:border-rose-600',
  orange: 'border-gray-300 text-orange-600 focus:border-orange-500 focus:ring-orange-500 checked:bg-orange-600 checked:border-orange-600',
  green: 'border-gray-300 text-green-600 focus:border-green-500 focus:ring-green-500 checked:bg-green-600 checked:border-green-600',
  blue: 'border-gray-300 text-blue-600 focus:border-blue-500 focus:ring-blue-500 checked:bg-blue-600 checked:border-blue-600',
  yellow: 'border-gray-300 text-yellow-600 focus:border-yellow-500 focus:ring-yellow-500 checked:bg-yellow-600 checked:border-yellow-600',
  violet: 'border-gray-300 text-violet-600 focus:border-violet-500 focus:ring-violet-500 checked:bg-violet-600 checked:border-violet-600'
}.freeze
CHECKBOX_SIZE =
{
  small: 'h-2.5 w-2.5',
  medium: 'h-3 w-3',
  large: 'h-4 w-4'
}.freeze
CHECKBOX_ROUNDED =
{
  none: 'rounded-none',
  small: 'rounded-sm',
  medium: 'rounded',
  large: 'rounded-lg',
  full: 'rounded-full'
}.freeze
CHECKBOX_BASE_CLASSES =
'appearance-none border-2 focus:outline-none focus:ring-2 focus:ring-offset-2 transition-colors duration-200 cursor-pointer disabled:cursor-not-allowed disabled:opacity-50'.freeze
CHECKBOX_LABEL_GAP =
{
  small: 'gap-1.5',
  medium: 'gap-2',
  large: 'gap-2.5'
}.freeze
CHECKBOX_LABEL_TEXT =
{
  small: 'text-xs',
  medium: 'text-sm',
  large: 'text-base'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Component.

Parameters:

  • name (String)

    Nome del campo checkbox (obbligatorio)

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

    Valore del checkbox (default: “1”)

  • checked (Boolean) (defaults to: false)

    Se il checkbox è selezionato

  • required (Boolean) (defaults to: false)

    Se il campo è obbligatorio

  • disabled (Boolean) (defaults to: false)

    Se il campo è disabilitato

  • indeterminate (Boolean) (defaults to: false)

    Se il checkbox è in stato indeterminate

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

    Testo della label associata al checkbox

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

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

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

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

    Classi CSS aggiuntive

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

    Form builder Rails opzionale

  • options (Hash)

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 66

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

  validate_params
end

Instance Attribute Details

#checkedObject (readonly)

Returns the value of attribute checked.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def checked
  @checked
end

#classesObject (readonly)

Returns the value of attribute classes.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def classes
  @classes
end

#disabledObject (readonly)

Returns the value of attribute disabled.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def disabled
  @disabled
end

#formObject (readonly)

Returns the value of attribute form.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def form
  @form
end

#indeterminateObject (readonly)

Returns the value of attribute indeterminate.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def indeterminate
  @indeterminate
end

#labelObject (readonly)

Returns the value of attribute label.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def label
  @label
end

#label_positionObject (readonly)

Returns the value of attribute label_position.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def label_position
  @label_position
end

#nameObject (readonly)

Returns the value of attribute name.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def options
  @options
end

#requiredObject (readonly)

Returns the value of attribute required.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def required
  @required
end

#roundedObject (readonly)

Returns the value of attribute rounded.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def rounded
  @rounded
end

#sizeObject (readonly)

Returns the value of attribute size.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def size
  @size
end

#themeObject (readonly)

Returns the value of attribute theme.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def theme
  @theme
end

#valueObject (readonly)

Returns the value of attribute value.



49
50
51
# File 'app/components/better_ui/general/input/checkbox/component.rb', line 49

def value
  @value
end