Class: Primer::Alpha::CheckBox
- Defined in:
- app/components/primer/alpha/check_box.rb
Overview
Check boxes are true/false inputs rendered as ‘<input type=“checkbox”>` in HTML.
## Schemes
Check boxes can submit values to the server using one of two schemes, either ‘:array` or `:boolean` (the default). Check boxes with a scheme of `:boolean` function like normal HTML check boxes. If they are checked, a value of “1” is sent to the server; if they are unchecked, a value of “0” is sent to the server. The checked and unchecked values can be customized via the `:value` and `:unchecked_value` arguments respectively.
Whereas ‘:boolean` check boxes must have unique names, `:array` check boxes all have the same name. On form submission, Rails will aggregate the values of the check boxes with the same name and provide them to the controller as an array. If `:scheme:` is `:array`, the `:value` argument must also be provided. The `:unchecked_value` argument is ignored. If a check box is checked on submit, its corresponding value will appear in the array. If it is not checked, its value will not appear in the array.
## Caption templates
Caption templates for ‘:array`-type check boxes work a little differently than they do for other input types. Because the name must be the same for all check boxes that make up an array, caption template file names are comprised of both the name and the value of each check box. For example, a check box with the name `foo` and value `bar` must have a caption template named `foo_bar_caption.html.erb`.
## Nested Forms
Check boxes can have “nested” forms that are rendered below the caption. A common use-case is a form that is hidden until the check box is checked. Nested forms are indented slightly to align with the label and caption.
Define a nested form via the ‘#nested_form` method, which is expected to return an instance of a Primer form (see the usage section below).
Any fields defined in the nested form are submitted along with the parent form’s fields.
NOTE: Check boxes do not automatically show or hide nested forms. If such behavior is desired, it must be done by hand.
Constant Summary
Constants inherited from Component
Component::INVALID_ARIA_LABEL_TAGS
Constants included from Status::Dsl
Constants included from ViewHelper
Constants included from TestSelectorHelper
TestSelectorHelper::TEST_SELECTOR_TAG
Constants included from FetchOrFallbackHelper
FetchOrFallbackHelper::InvalidValueError
Constants included from Primer::AttributesHelper
Primer::AttributesHelper::PLURAL_ARIA_ATTRIBUTES, Primer::AttributesHelper::PLURAL_DATA_ATTRIBUTES
Instance Method Summary collapse
- #initialize ⇒ Object constructor
- #nested_form ⇒ Object
Methods inherited from Component
Methods included from JoinStyleArgumentsHelper
Methods included from TestSelectorHelper
Methods included from FetchOrFallbackHelper
#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?
Methods included from ClassNameHelper
Methods included from Primer::AttributesHelper
#aria, #data, #extract_data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes
Methods included from ExperimentalSlotHelpers
Methods included from ExperimentalRenderHelpers
Constructor Details
#initialize ⇒ Object
|
# File 'app/components/primer/alpha/check_box.rb', line 59
|
Instance Method Details
#nested_form ⇒ Object
|
# File 'app/components/primer/alpha/check_box.rb', line 68
|