Checkbox Group
A control that allows the user to toggle between checked and not checked.
Usage
Example
CheckboxGroup(data_required: true) do div(class: "flex flex-col gap-2") do div(class: "flex flex-row items-center gap-2") do Checkbox(value: "FOO", id: "EXAMPLE_FOO") FormFieldLabel(for: "EXAMPLE_FOO") { "FOO" } end div(class: "flex flex-row items-center gap-2") do Checkbox(value: "BAR", id: "EXAMPLE_BAR") FormFieldLabel(for: "EXAMPLE_BAR") { "BAR" } end end end
Copied!
Copy failed!
With form
form(class: "flex flex-col gap-2") do FormField do FormFieldLabel { "CHECKBOX_GROUP" } FormFieldHint { "HINT_FOR_CHECKBOX_GROUP" } CheckboxGroup(data_required: true) do div(class: "flex flex-col gap-2") do div(class: "flex flex-row items-center gap-2") do Checkbox( id: "FORM_FOO", value: "FOO", checked: false, name: "CHECKBOX_GROUP[]", data: {value_missing: "CUSTOM_MESSAGE"} ) FormFieldLabel(for: "FORM_FOO") { "FOO" } end div(class: "flex flex-row items-center gap-2") do Checkbox( id: "FORM_BAR", value: "BAR", checked: true, name: "CHECKBOX_GROUP[]", data: {value_missing: "CUSTOM_MESSAGE"} ) FormFieldLabel(for: "FORM_BAR") { "BAR" } end end end FormFieldError() end Button(type: "submit") { "SUBMIT_BUTTON" } end
Copied!
Copy failed!
Installation
Using RubyUI CLI
Run the install command
rails g ruby_ui:component Checkbox
Copied!
Copy failed!
Manual installation
1
Add RubyUI::Checkbox
to app/components/ruby_ui/checkbox.rb
# frozen_string_literal: true module RubyUI class Checkbox < Base def view_template input(**attrs) end private def default_attrs { type: "checkbox", data: { ruby_ui__form_field_target: "input", ruby_ui__checkbox_group_target: "checkbox", action: "change->ruby-ui--checkbox-group#onChange change->ruby-ui--form-field#onInput invalid->ruby-ui--form-field#onInvalid" }, class: "peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 accent-primary" } end end end
Copied!
Copy failed!
2
Add RubyUI::CheckboxGroup
to app/components/ruby_ui/checkbox/checkbox_group.rb
# frozen_string_literal: true module RubyUI class CheckboxGroup < Base def view_template(&) div(**attrs, &) end private def default_attrs { role: "group", data: { controller: "ruby-ui--checkbox-group" } } end end end
Copied!
Copy failed!
3
Add checkbox_group_controller.js
to app/javascript/controllers/ruby_ui/checkbox_group_controller.js
import { Controller } from "@hotwired/stimulus"; export default class extends Controller { static targets = ["checkbox"]; connect() { this.#handleRequired(); } onChange() { this.#handleRequired(); } #handleRequired() { if (!this.element.hasAttribute("data-required")) return; const checked = this.checkboxTargets.some(({ checked }) => checked); this.checkboxTargets.forEach((checkbox) => (checkbox.required = !checked)); } }
Copied!
Copy failed!
4
Update the Stimulus controllers manifest file
Importmap!
You don't need to run this command if you are using Importmap
rake stimulus:manifest:update
Copied!
Copy failed!
Components
Component | Built using | Source |
---|---|---|
Checkbox | Phlex | |
CheckboxGroup | Phlex | |
CheckboxGroupController | Stimulus JS |