Class: HakumiComponents::Segmented::Option

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
app/components/hakumi_components/segmented/option.rb

Constant Summary collapse

IconValue =
T.type_alias { T.nilable(T.any(String, Symbol, ViewComponent::Base, ViewComponent::Slot)) }
JsonValue =
T.type_alias { T.nilable(T.any(String, Numeric, T::Boolean, T::Array[T.untyped], T::Hash[String, T.untyped])) }
ParsedEntry =
T.type_alias { T.any(HakumiComponents::Segmented::Option, HakumiComponents::Types::FormFieldScalar, Types::HtmlAttributes) }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, value:, disabled: false, icon: nil, class_name: nil, aria_label: nil) ⇒ Option

Returns a new instance of Option.



23
24
25
26
27
28
29
30
# File 'app/components/hakumi_components/segmented/option.rb', line 23

def initialize(label:, value:, disabled: false, icon: nil, class_name: nil, aria_label: nil)
  @label = T.let(label, T.nilable(Types::Renderable))
  @value = T.let(value, Types::FormFieldScalar)
  @disabled = T.let(disabled, T::Boolean)
  @icon = T.let(icon, IconValue)
  @class_name = T.let(class_name, T.nilable(String))
  @aria_label = T.let(aria_label, T.nilable(String))
end

Instance Attribute Details

#aria_labelObject (readonly)

Returns the value of attribute aria_label.



45
46
47
# File 'app/components/hakumi_components/segmented/option.rb', line 45

def aria_label
  @aria_label
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



45
46
47
# File 'app/components/hakumi_components/segmented/option.rb', line 45

def class_name
  @class_name
end

#disabledObject (readonly)

Returns the value of attribute disabled.



39
40
41
# File 'app/components/hakumi_components/segmented/option.rb', line 39

def disabled
  @disabled
end

#iconObject (readonly)

Returns the value of attribute icon.



42
43
44
# File 'app/components/hakumi_components/segmented/option.rb', line 42

def icon
  @icon
end

#labelObject (readonly)

Returns the value of attribute label.



33
34
35
# File 'app/components/hakumi_components/segmented/option.rb', line 33

def label
  @label
end

#valueObject (readonly)

Returns the value of attribute value.



36
37
38
# File 'app/components/hakumi_components/segmented/option.rb', line 36

def value
  @value
end

Class Method Details

.coerce(option, index) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/components/hakumi_components/segmented/option.rb', line 68

def self.coerce(option, index)
  return option if option.is_a?(HakumiComponents::Segmented::Option)

  if option.is_a?(Hash)
    label_value = extract_label(option, index)
    value_value = extract_value(option, label_value)

    return new(
      label: label_value,
      value: value_value,
      disabled: HakumiComponents::BaseComponent.boolean_html_param(option[:disabled]) == true,
      icon: coerce_icon(option[:icon]),
      class_name: coerce_string(option[:class_name]),
      aria_label: coerce_string(option[:aria_label])
    )
  end

  new(label: option&.to_s, value: option, disabled: false)
end

.coerce_all(options) ⇒ Object



58
59
60
61
62
63
64
65
# File 'app/components/hakumi_components/segmented/option.rb', line 58

def self.coerce_all(options)
  source = parse_options(options)
  coerced = T.let([], T::Array[HakumiComponents::Segmented::Option])
  Array(source).each_with_index do |option, index|
    coerced << coerce(option, index)
  end
  coerced
end

Instance Method Details

#icon_only?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/components/hakumi_components/segmented/option.rb', line 53

def icon_only?
  @icon.present? && (@label.nil? || (@label.is_a?(String) && @label.empty?))
end

#string_valueObject



48
49
50
# File 'app/components/hakumi_components/segmented/option.rb', line 48

def string_value
  @value.to_s
end