Class: HakumiComponents::Radio::Group::Component

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

Constant Summary collapse

OptionHash =
T.type_alias do
  T::Hash[T.any(Symbol, String), T.nilable(T.any(String, Symbol, Numeric, T::Boolean))]
end
OptionInput =
T.type_alias do
  T.any(HakumiComponents::Radio::Group::Option, OptionHash, String, Symbol, Numeric)
end
VARIANTS =
T.let([ :default, :button, :solid ].freeze, T::Array[Symbol])
DIRECTIONS =
T.let([ :horizontal, :vertical ].freeze, T::Array[Symbol])

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 included from Concerns::FormField

#describedby_ids, #error?, #error_message, #form_field_caption, #form_field_contract, #form_field_errors, #form_field_html_options, #form_field_label, #form_field_name, #form_field_required, #form_field_rules, #form_field_standalone, #form_item_attributes, #form_item_classes, #input_id, #render_caption, #render_error, #render_explain, #render_label, #standalone?

Methods included from Concerns::FormFieldInterface

#form_field_contract

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: nil, options: [], label: nil, caption: nil, variant: :default, direction: :horizontal, size: :default, disabled: false, standalone: true, required: false, errors: [], **html_options) ⇒ Component

Returns a new instance of Component.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/components/hakumi_components/radio/group/component.rb', line 39

def initialize(
  name: nil,
  value: nil,
  options: [],
  label: nil,
  caption: nil,
  variant: :default,
  direction: :horizontal,
  size: :default,
  disabled: false,
  standalone: true,
  required: false,
  errors: [],
  **html_options
)
  @name = T.let(name || generate_id("radio_group"), Types::FormFieldName)
  @value = T.let(value, HakumiComponents::Types::FormFieldScalar)
  @options = T.let(normalize_options(options), T::Array[HakumiComponents::Radio::Group::Option])
  @label = label
  @caption = caption
  @variant = T.let(variant.to_sym, Symbol)
  @direction = T.let(direction.to_sym, Symbol)
  @size = size
  @disabled = disabled
  @standalone = standalone
  @required = required
  @errors = T.let(errors, Types::FormFieldErrors)
  @html_options = T.let(html_options, Types::HtmlAttributes)
  initialize_form_field_contract!(
    name: @name,
    label: @label,
    caption: @caption,
    errors: @errors,
    standalone: @standalone,
    required: @required,
    html_options: @html_options
  )

  validate_props!
end

Instance Method Details

#button_group?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'app/components/hakumi_components/radio/group/component.rb', line 125

def button_group?
  @variant == :button || @variant == :solid
end

#button_wrapper_classes(option) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'app/components/hakumi_components/radio/group/component.rb', line 109

def button_wrapper_classes(option)
  modifiers = {
    checked: option_checked?(option.value),
    disabled: option_disabled?(option)
  }
  modifiers[@size] = true if @size != :default

  class_names("radio-button-wrapper", modifiers)
end

#normalized_optionsObject



92
93
94
# File 'app/components/hakumi_components/radio/group/component.rb', line 92

def normalized_options
  @options
end

#option_checked?(option_value) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
# File 'app/components/hakumi_components/radio/group/component.rb', line 97

def option_checked?(option_value)
  return false if @value.nil?

  @value.to_s == option_value.to_s
end

#option_disabled?(option) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'app/components/hakumi_components/radio/group/component.rb', line 104

def option_disabled?(option)
  @disabled || option.disabled
end

#option_id(index) ⇒ Object



120
121
122
# File 'app/components/hakumi_components/radio/group/component.rb', line 120

def option_id(index)
  "#{form_field_name}_#{index}"
end

#wrapper_attributesObject



81
82
83
84
85
86
87
88
89
# File 'app/components/hakumi_components/radio/group/component.rb', line 81

def wrapper_attributes
  attrs = {
    class: wrapper_classes,
    role: "radiogroup",
    "aria-orientation": (@direction == :vertical ? "vertical" : nil)
  }.compact

  merge_attributes(attrs, @html_options)
end