Class: CCS::Components::GovUK::Field::Inputs::Item

Inherits:
Base
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers
Defined in:
lib/ccs/components/govuk/field/inputs/item.rb,
lib/ccs/components/govuk/field/inputs/item/radio.rb,
lib/ccs/components/govuk/field/inputs/item/divider.rb,
lib/ccs/components/govuk/field/inputs/item/checkbox.rb,
lib/ccs/components/govuk/field/inputs/item/radio/tag.rb,
lib/ccs/components/govuk/field/inputs/item/radio/form.rb,
lib/ccs/components/govuk/field/inputs/item/checkbox/tag.rb,
lib/ccs/components/govuk/field/inputs/item/checkbox/form.rb

Overview

GOV.UK Field Inputs Item

This class is used to provided the shared HTML for a checkbox item or radio item. It wraps these items with the following:

  • Item label

  • Item hint

  • Item conditional content

Direct Known Subclasses

Checkbox, Radio

Defined Under Namespace

Classes: Checkbox, Divider, Radio

Constant Summary

Constants inherited from Base

Base::DEFAULT_ATTRIBUTES

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, value:, item_class:, hint: nil, conditional: nil, **options) ⇒ Item

Returns a new instance of Item.

Parameters:

  • attribute (String, Symbol)

    the attribute of the item

  • item_class (String)

    the CSS class for the item

  • value (String)

    the value of the item

  • hint (Hash) (defaults to: nil)

    options for an item hint see Hint#initialize for more details. If no hint is given then no hint will be rendered

  • conditional (Hash) (defaults to: nil)

    content that will appear if the item is checked. If no conditional is given then no conditional content will be rendered

  • options (Hash)

    a customizable set of options

Options Hash (conditional:):

  • content (ActiveSupport::SafeBuffer)

    the HTML content

  • attributes[:id] (Hash)

    the id of the conditional section

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the item HTML

  • :checked (Boolean)

    flag to indicate if the item is checked

  • :form (ActionView::Helpers::FormBuilder) — default: nil

    optional form builder used to create item HTML

  • :attributes (Hash) — default: {}

    any additional attributes that will be added as part of the item HTML



59
60
61
62
63
64
65
66
67
# File 'lib/ccs/components/govuk/field/inputs/item.rb', line 59

def initialize(attribute:, value:, item_class:, hint: nil, conditional: nil, **options)
  super(**options)

  initialise_item_hint(attribute, value, hint) if hint
  initialize_item_conditional(attribute, value, conditional) if conditional && conditional[:content]
  @attribute = attribute
  @value = value
  @item_class = item_class
end

Instance Method Details

#render { ... } ⇒ ActiveSupport::SafeBuffer

Generates the HTML to wrap arround a input

Yields:

  • the item input HTML

Returns:

  • (ActiveSupport::SafeBuffer)


77
78
79
80
81
82
83
84
85
86
# File 'lib/ccs/components/govuk/field/inputs/item.rb', line 77

def render
  capture do
    concat(tag.div(class: @item_class) do
      concat(yield)
      concat(label.render)
      concat(hint.render) if hint
    end)
    concat(conditional_content) if conditional_content
  end
end