Class: HakumiComponents::Spin::Component

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

Constant Summary collapse

DelayInput =
T.type_alias { BaseComponent::DimensionInput }
ProgressPercent =
T.type_alias { T.nilable(T.any(Numeric, Symbol)) }
PercentInput =
T.type_alias { T.nilable(T.any(Numeric, Symbol, String)) }
TipContent =
T.type_alias { T.nilable(Types::Renderable) }
ControllerLocalValue =
T.type_alias { Types::HtmlPrimitive }

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

Class Method Summary collapse

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(spinning: true, size: :default, tip: nil, delay: 0, indicator: nil, fullscreen: false, percent: nil, **html_options) ⇒ Component

Returns a new instance of Component.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/components/hakumi_components/spin/component.rb', line 65

def initialize(
  spinning: true,
  size: :default,
  tip: nil,
  delay: 0,
  indicator: nil,
  fullscreen: false,
  percent: nil,
  **html_options
)
  @spinning = T.let(cast_boolean(spinning) == true, T::Boolean)
  @size = T.let(size.nil? ? :default : size.to_sym, Symbol)
  @tip = T.let(tip, TipContent)
  @delay = T.let(normalize_delay(delay), Integer)
  @indicator = T.let(indicator, TipContent)
  @fullscreen = T.let(fullscreen, T::Boolean)
  @percent = T.let(normalize_percent(percent), ProgressPercent)
  @html_options = T.let(html_options, Types::HtmlAttributes)

  validate_props!
end

Class Method Details

.extract_controller_locals(params) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/components/hakumi_components/spin/component.rb', line 18

def self.extract_controller_locals(params)
  spinning_value = html_param(params, :spinning)

  {
    spinning: spinning_value.nil? ? true : boolean_html_param(spinning_value) != false,
    size: symbol_html_param(html_param(params, :size)),
    tip: string_html_param(html_param(params, :tip)),
    delay: parse_delay(html_param(params, :delay)),
    fullscreen: boolean_html_param(html_param(params, :fullscreen)) == true,
    percent: parse_percent(html_param(params, :percent))
  }
end

.parse_delay(value) ⇒ Object



32
33
34
35
36
37
38
# File 'app/components/hakumi_components/spin/component.rb', line 32

def self.parse_delay(value)
  return nil if value.nil?
  return value if value.is_a?(Integer)
  return value.to_i if value.is_a?(Float)

  value.to_s.strip.presence&.to_i
end

.parse_percent(value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'app/components/hakumi_components/spin/component.rb', line 41

def self.parse_percent(value)
  return nil if value.nil?

  string_value = value.to_s.strip
  return :auto if string_value == "auto"

  return value if value.is_a?(Integer)
  return value if value.is_a?(Float)

  string_value.presence&.to_f
end

Instance Method Details

#indicator_markupObject



107
108
109
110
111
112
# File 'app/components/hakumi_components/spin/component.rb', line 107

def indicator_markup
  return render_value(@indicator) if @indicator.present?
  return progress_indicator if progress?

  default_indicator
end

#nested?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'app/components/hakumi_components/spin/component.rb', line 102

def nested?
  content.present? && !@fullscreen
end

#progress?Boolean

Returns:

  • (Boolean)


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

def progress?
  !@percent.nil?
end

#progress_auto?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'app/components/hakumi_components/spin/component.rb', line 130

def progress_auto?
  @percent == :auto
end

#progress_componentObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/components/hakumi_components/spin/component.rb', line 135

def progress_component
  percent = numeric_percent
  raise "progress_component requires numeric percent" if percent.nil?

  HakumiComponents::Progress::Component.new(
    percent: percent,
    type: :circle,
    size: progress_size,
    stroke_width: 6,
    show_info: true
  )
end

#show_text?Boolean

Returns:

  • (Boolean)


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

def show_text?
  @tip.present?
end

#tip_markupObject



115
116
117
# File 'app/components/hakumi_components/spin/component.rb', line 115

def tip_markup
  render_value(@tip)
end

#wrapper_attributesObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/components/hakumi_components/spin/component.rb', line 88

def wrapper_attributes
  merge_attributes(
    {
      class: wrapper_classes,
      role: "status",
      "aria-live": "polite",
      "aria-busy": @spinning,
      data: data_attributes
    },
    @html_options
  )
end