Class: HakumiComponents::Checkbox::Group::Component

Inherits:
BaseComponent
  • Object
show all
Extended by:
T::Sig
Defined in:
app/components/hakumi_components/checkbox/group/component.rb

Constant Summary

Constants inherited from BaseComponent

BaseComponent::ControllerOptions, BaseComponent::DateInput, BaseComponent::DateLikeValue, BaseComponent::DimensionInput, BaseComponent::HtmlPayloadInput, BaseComponent::I18nOptionValue, BaseComponent::PresenceArray, BaseComponent::PresenceScalar, BaseComponent::PresenceValue, BaseComponent::RawHtmlInput, BaseComponent::SIZES, BaseComponent::SizeValue, BaseComponent::SymbolInput

Instance Method Summary collapse

Methods inherited from BaseComponent

#append_data_token, boolean_html_param, #build_inline_style, cast_boolean, #cast_boolean, #class_names, #component_classes, #data_attributes_from, #dimension_to_css, #ensure_dom_id!, float_html_param, #generate_id, #html_classes, html_param, html_primitive_param, #html_style, #i18n_scope, integer_html_param, #merge_attributes, #render_value, #size_to_pixels, #stimulus_attrs, string_html_param, string_or_symbol_array_html_param, symbol_html_param, #t_default, #translate_with_default, #validate_inclusion!, #validate_required!, #value_present?

Constructor Details

#initialize(name: nil, value: [], default_value: [], options: [], disabled: false, **html_options) ⇒ Component

Returns a new instance of Component.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/components/hakumi_components/checkbox/group/component.rb', line 20

def initialize(
  name: nil,
  value: [],
  default_value: [],
  options: [],
  disabled: false,
  **html_options
)
  @name = T.let(name || generate_id("checkbox_group"), HakumiComponents::Types::FormFieldName)
  @value = T.let(value.empty? ? default_value : value, T::Array[HakumiComponents::Types::FormFieldScalar])
  @options = T.let(options, T::Array[T.any(String, HakumiComponents::Checkbox::Group::Option)])
  @disabled = T.let(disabled, T::Boolean)
  @html_options = T.let(html_options, HakumiComponents::Types::HtmlAttributes)
end

Instance Method Details

#normalized_optionsObject



62
63
64
65
66
67
68
69
70
# File 'app/components/hakumi_components/checkbox/group/component.rb', line 62

def normalized_options
  @options.map do |option|
    if option.is_a?(String)
      HakumiComponents::Checkbox::Group::Option.new(label: option, value: option)
    else
      option
    end
  end
end

#option_checked?(option_value) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/components/hakumi_components/checkbox/group/component.rb', line 73

def option_checked?(option_value)
  @value.include?(option_value.to_s) || @value.include?(option_value)
end

#wrapper_attributesObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/components/hakumi_components/checkbox/group/component.rb', line 41

def wrapper_attributes
  merged = merge_attributes(
    {
      class: wrapper_classes,
      role: "group"
    },
    @html_options.except(:data)
  )

  data = existing_data_attributes
  existing_controller = data[:controller]
  controllers = existing_controller.to_s.split(/\s+/).reject(&:blank?)
  controllers << "hakumi--checkbox-group"
  data[:controller] = controllers.uniq.join(" ")
  data[:"hakumi--checkbox-group-value-value"] ||= @value.map(&:to_s).to_json

  merged[:data] = data
  merged
end

#wrapper_classesObject



36
37
38
# File 'app/components/hakumi_components/checkbox/group/component.rb', line 36

def wrapper_classes
  class_names("checkbox-group")
end