Class: Shadcn::CheckboxComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/shadcn/checkbox_component.rb

Overview

Checkbox component using native Styled with CSS to match shadcn/ui design Works without JavaScript

Examples:

Basic checkbox

<%= render Shadcn::CheckboxComponent.new(name: "terms", id: "terms") %>
<%= render Shadcn::LabelComponent.new(for: "terms") { "Accept terms" } %>

Checked by default

<%= render Shadcn::CheckboxComponent.new(name: "subscribe", checked: true) %>

Disabled

<%= render Shadcn::CheckboxComponent.new(name: "locked", disabled: true) %>

With integrated label

<%= render Shadcn::CheckboxComponent.new(name: "terms", id: "terms") { "Accept terms and conditions" } %>

Constant Summary collapse

BASE_CLASSES =
[
  "shadcn-checkbox",
  "peer h-4 w-4 shrink-0 rounded-sm border border-primary",
  "focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-1",
  "disabled:cursor-not-allowed disabled:opacity-50",
  "cursor-pointer"
].join(" ")

Instance Attribute Summary

Attributes inherited from BaseComponent

#class_name, #data, #html_options

Instance Method Summary collapse

Methods inherited from BaseComponent

#content

Methods included from Rails::Helpers::ClassNameHelper

#cn

Constructor Details

#initialize(name: nil, id: nil, value: "1", checked: false, disabled: false, required: false, **options) ⇒ CheckboxComponent

Returns a new instance of CheckboxComponent.

Parameters:

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

    Input name attribute

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

    Input id attribute

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

    Value when checked

  • checked (Boolean) (defaults to: false)

    Whether checkbox is checked

  • disabled (Boolean) (defaults to: false)

    Whether checkbox is disabled

  • required (Boolean) (defaults to: false)

    Whether checkbox is required



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/components/shadcn/checkbox_component.rb', line 36

def initialize(
  name: nil,
  id: nil,
  value: "1",
  checked: false,
  disabled: false,
  required: false,
  **options
)
  super(**options)
  @name = name
  @id = id || (name ? "checkbox-#{name}" : nil)
  @value = value
  @checked = checked
  @disabled = disabled
  @required = required
end