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:, disabled: false, options: {}, size: :default, unchecked_value: DEFAULT_UNCHECKED_VALUE, value: DEFAULT_CHECKED_VALUE) ⇒ Checkbox

Returns a new instance of Checkbox.



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

def initialize(attribute:, form:, disabled: false, options: {}, size: :default, unchecked_value: DEFAULT_UNCHECKED_VALUE, value: DEFAULT_CHECKED_VALUE)
  super(attribute: attribute, 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
# File 'app/components/flowbite/input/checkbox.rb', line 24

def styles
  {
    default: Flowbite::Style.new(
      default: ["text-blue-600", "bg-gray-100", "border-gray-300", "rounded-sm", "focus:ring-blue-500", "dark:focus:ring-blue-600", "dark:ring-offset-gray-800", "focus:ring-2", "dark:bg-gray-700", "dark:border-gray-600"],
      disabled: ["text-blue-600", "bg-gray-100", "border-gray-300", "rounded-sm", "focus:ring-blue-500", "dark:focus:ring-blue-600", "dark:ring-offset-gray-800", "focus:ring-2", "dark:bg-gray-700", "dark:border-gray-600"],
      error: ["text-red-600", "bg-red-50", "border-red-500", "rounded-sm", "focus:ring-red-500", "dark:focus:ring-red-600", "dark:ring-offset-gray-800", "focus:ring-2", "dark:bg-gray-700", "dark:border-red-500"]
    )
  }.freeze
end

Instance Method Details

#callObject

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



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

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

#input_field_typeObject



52
53
54
# File 'app/components/flowbite/input/checkbox.rb', line 52

def input_field_type
  :check_box
end

#input_optionsObject

Returns the options argument for the input field



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

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