Class: Flowbite::Input::Checkbox

Inherits:
Field
  • Object
show all
Defined in:
app/components/flowbite/input/checkbox.rb

Overview

The checkbox component can be used to receive one or more selected options from the user in the form of a square box available in multiple styles, sizes, colors, and variants coded with the utility classes from Tailwind CSS and with support for dark mode.

flowbite.com/docs/forms/checkbox/

Constant Summary collapse

DEFAULT_CHECKED_VALUE =
"1"
DEFAULT_UNCHECKED_VALUE =
"0"

Constants inherited from Field

Field::SIZES, Field::STATES

Instance Attribute Summary

Attributes inherited from Field

#options, #size, #style

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Field

classes, #classes

Constructor Details

#initialize(attribute:, form:, class: nil, disabled: false, options: {}, size: :default, unchecked_value: DEFAULT_UNCHECKED_VALUE, value: DEFAULT_CHECKED_VALUE) ⇒ Checkbox

Returns a new instance of Checkbox.



48
49
50
51
52
# File 'app/components/flowbite/input/checkbox.rb', line 48

def initialize(attribute:, form:, class: nil, disabled: false, options: {}, size: :default, unchecked_value: DEFAULT_UNCHECKED_VALUE, value: DEFAULT_CHECKED_VALUE)
  super(attribute: attribute, class: binding.local_variable_get(:class), form: form, disabled: disabled, options: options, size: size)
  @unchecked_value = unchecked_value
  @value = value
end

Class Method Details

.sizesObject

Checkboxes only have their default size.



17
18
19
20
21
# File 'app/components/flowbite/input/checkbox.rb', line 17

def sizes
  {
    default: ["w-4", "h-4"]
  }
end

.stylesObject

rubocop:disable Layout/LineLength



24
25
26
27
28
29
30
31
32
33
34
# File 'app/components/flowbite/input/checkbox.rb', line 24

def styles
  Flowbite::Styles.from_hash(
    {
      default: {
        default: ["text-brand", "bg-neutral-secondary-medium", "border-default-medium", "rounded-sm", "focus:ring-brand", "focus:ring-2"],
        disabled: ["text-brand", "bg-neutral-secondary-medium", "border-default-medium", "rounded-sm", "focus:ring-brand", "focus:ring-2", "cursor-not-allowed"],
        error: ["text-danger", "bg-danger-soft", "border-danger-subtle", "rounded-sm", "focus:ring-danger", "focus:ring-2"]
      }
    }.freeze
  )
end

Instance Method Details

#callObject

Returns the HTML to use for the actual input field element.



38
39
40
41
42
43
44
45
46
# File 'app/components/flowbite/input/checkbox.rb', line 38

def call
  @form.send(
    input_field_type,
    @attribute,
    input_options,
    checked_value,
    unchecked_value
  )
end

#input_field_typeObject



54
55
56
# File 'app/components/flowbite/input/checkbox.rb', line 54

def input_field_type
  :check_box
end

#input_optionsObject

Returns the options argument for the input field



59
60
61
62
63
64
# File 'app/components/flowbite/input/checkbox.rb', line 59

def input_options
  {
    class: classes,
    disabled: disabled?
  }.merge(options)
end