app/components/ariadne/form/checkbox/component.rb



# typed: false
# frozen_string_literal: true

module Ariadne
  module Form
    module Checkbox
      class Component < Ariadne::Form::BaseInputComponent
        option :name
        option :value
        option :label, default: proc { false }

        option :checked, default: proc { false }

        # renders_one :leading_visual, BaseComponent::ACCEPT_ANYTHING
        # renders_one :trailing_visual, BaseComponent::ACCEPT_ANYTHING

        def before_render
          raise ArgumentError, "Cannot have both `label` and content block" if @label.present? && content.present?
          raise ArgumentError, "Must have either `label` or content block" if @label.blank? && content.blank?
        end

        option :theme, default: proc { :primary }
        option :size, default: proc { :base }
        option :width, default: proc { :narrow }

        accepts_html_attributes do |html_attrs|
          html_attrs[:class] = merge_tailwind_classes([style(theme:, size:, width:), html_attrs[:class]].join(" "))

          html_attrs[:value] = @value

          html_attrs[:data] ||= {}

          html_attrs[:checked] ||= @checked
          html_attrs
        end

        def type
          :check_box
        end

        def focusable?
          true
        end

        def generic_name
          @name.sub(/\[[^\]]*\]$/, "")
        end

        style do
          base do
            [
              "ariadne:h-4",
              "ariadne:w-4",
              "ariadne:p-px",
              "ariadne:inline-flex",
              "ariadne:justify-center",
              "ariadne:rounded",
              "ariadne:border",
              "ariadne:border-solid",
              "ariadne:border-zinc-300",
              "ariadne:dark:border-zinc-700",
              "ariadne:peer-checked:border-transparent",
              "ariadne:dark:bg-foreground-dark",
              "ariadne:peer-checked:text-white",
              "ariadne:peer-checked:bg-indigo-600",
              "ariadne:dark:peer-checked:bg-indigo-500",
            ]
          end
        end

        style :label do
          base do
            [
              "ariadne:block",
            ]
          end
        end
      end
    end
  end
end