Class: Fluxbit::Form::CheckBoxComponent

Inherits:
FieldComponent
  • Object
show all
Includes:
Config::Form::CheckBoxComponent
Defined in:
app/components/fluxbit/form/check_box_component.rb

Overview

The ‘Fluxbit::Form::CheckBoxComponent` is a form input component for check boxes and radio buttons. It extends `Fluxbit::Form::FieldComponent` and provides a styled checkbox/radio with label, helper text, and support for different visual states and groupings. It automatically adds the correct styles for both checkbox and radio types and works with or without Rails form builders.

Examples:

Basic usage

= render Fluxbit::Form::CheckBoxComponent.new(name: :accept_terms, label: "Accept the terms")

See Also:

  • For detailed documentation and examples.

Constant Summary collapse

TYPE_DEFAULT =
:check_box
TYPE_OPTIONS =
%i[check_box checkbox radio_button].freeze

Instance Method Summary collapse

Constructor Details

#initialize(**props) ⇒ CheckBoxComponent

Initializes the check box component with the given properties.

Parameters:

  • name (String)

    Name of the field (required unless using form builder)

  • label (String)

    Label text next to the input (optional)

  • value (String)

    Value for the field (optional)

  • type (String, Symbol)

    Input type (‘“check_box”`, `“checkbox”`, `“radio_button”`)

  • help_text (String)

    Helper or error text below the field

  • disabled (Boolean)

    Disables the input if true

  • checked (Boolean)

    Marks the input as checked if true

  • class (String)

    Additional CSS classes for the input element

  • ...

    any other HTML attribute supported by check_box_tag/radio_button_tag



28
29
30
31
32
33
# File 'app/components/fluxbit/form/check_box_component.rb', line 28

def initialize(**props)
  super(**props)
  @type = options(@props.delete(:type), collection: TYPE_OPTIONS, default: TYPE_DEFAULT)
  add(class: styles[:checkbox], to: @props, first_element: true) if @props[:type] == "checkbox"
  add(class: styles[:base], to: @props, first_element: true)
end

Instance Method Details

#callObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/components/fluxbit/form/check_box_component.rb', line 43

def call
  if @help_text
     :div, { class: "flex" } do
      concat (:div, input, { class: styles[:input_div] })
      concat (:div, safe_join([ label, help_text ]), { class: styles[:helper_div] })
    end
  else
     :div, { class: styles[:no_helper_div] } do
      concat input
      concat label
    end
  end
end

#inputObject



35
36
37
38
39
40
41
# File 'app/components/fluxbit/form/check_box_component.rb', line 35

def input
  if @form.present? && @attribute.present?
    @form.public_send(@type, @attribute, @props)
  else
    public_send("#{@type}_tag", @name, @value, @props)
  end
end