Class: HakumiComponents::QrCode::Component

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

Constant Summary collapse

NumberValue =
T.type_alias { Numeric }
NumberInput =
T.type_alias { BaseComponent::DimensionInput }
OptionInput =
T.type_alias { BaseComponent::SymbolInput }
IconRenderable =
T.type_alias { T.nilable(Types::ComponentRenderable) }
IconInput =
T.type_alias { T.nilable(T.any(Symbol, Types::Renderable)) }
StatusRender =
T.type_alias { T.nilable(T.any(Types::Renderable, T.proc.params(status: Symbol).returns(Types::RenderedContent))) }
ControllerOptions =
T.type_alias { BaseComponent::ControllerOptions }
ImageAttributes =
T.type_alias { T::Hash[Symbol, Types::HtmlPrimitive] }
TYPES =
T.let([ :canvas, :svg ].freeze, T::Array[Symbol])
STATUSES =
T.let([ :active, :expired, :loading, :scanned ].freeze, T::Array[Symbol])
ERROR_LEVELS =
T.let([ :L, :M, :Q, :H ].freeze, T::Array[Symbol])

Constants inherited from BaseComponent

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(value:, size: 160, type: :canvas, color: nil, bg_color: nil, icon: nil, icon_size: 40, status: :active, status_render: nil, bordered: true, error_level: :M, download_name: nil, aria_label: nil, **html_options) ⇒ Component

Returns a new instance of Component.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/components/hakumi_components/qr_code/component.rb', line 80

def initialize(
  value:,
  size: 160,
  type: :canvas,
  color: nil,
  bg_color: nil,
  icon: nil,
  icon_size: 40,
  status: :active,
  status_render: nil,
  bordered: true,
  error_level: :M,
  download_name: nil,
  aria_label: nil,
  **html_options
)
  @value = T.let(value, String)
  @size = T.let(self.class.parse_number(size) || 160, NumberValue)
  @type = T.let(normalize_symbol(type) || :canvas, Symbol)
  @color = T.let(normalize_string(color), T.nilable(String))
  @bg_color = T.let(normalize_string(bg_color), T.nilable(String))
  @icon_input = T.let(icon, IconInput)
  @icon_size = T.let(self.class.parse_number(icon_size) || 40, NumberValue)
  resolve_icon!
  @status = T.let(normalize_symbol(status) || :active, Symbol)
  @status_render = T.let(status_render, StatusRender)
  @bordered = T.let(bordered.nil? ? true : cast_boolean(bordered) == true, T::Boolean)
  @error_level = T.let(normalize_error_level(error_level) || :M, Symbol)
  @download_name = T.let(normalize_string(download_name), T.nilable(String))
  @aria_label = T.let(normalize_string(aria_label), T.nilable(String))
  @html_options = T.let(html_options, Types::HtmlAttributes)

  validate_props!
end

Class Method Details

.extract_controller_locals(params) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/components/hakumi_components/qr_code/component.rb', line 24

def self.extract_controller_locals(params)
  bordered_value = html_param(params, :bordered)

  {
    value: string_html_param(html_param(params, :value)),
    size: parse_number(html_param(params, :size)),
    type: symbol_html_param(html_param(params, :type)),
    color: string_html_param(html_param(params, :color)),
    bg_color: string_html_param(html_param(params, :bg_color)),
    icon: html_param(params, :icon),
    icon_size: parse_number(html_param(params, :icon_size)),
    status: symbol_html_param(html_param(params, :status)),
    status_render: string_html_param(html_param(params, :status_render)),
    bordered: bordered_value.nil? ? nil : boolean_html_param(bordered_value),
    error_level: parse_error_level(html_param(params, :error_level)),
    download_name: string_html_param(html_param(params, :download_name)),
    aria_label: string_html_param(html_param(params, :aria_label))
  }
end

.parse_error_level(value) ⇒ Object



56
57
58
59
60
# File 'app/components/hakumi_components/qr_code/component.rb', line 56

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

  value.to_s.strip.presence&.upcase&.to_sym
end

.parse_number(value) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'app/components/hakumi_components/qr_code/component.rb', line 45

def self.parse_number(value)
  return nil if value.nil?
  return value if value.is_a?(Integer)
  return value if value.is_a?(Float)
  raw = value.to_s.strip
  return nil if raw.empty?

  raw.include?(".") ? raw.to_f : raw.to_i
end

Instance Method Details

#icon?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'app/components/hakumi_components/qr_code/component.rb', line 139

def icon?
  icon_component? || icon_image?
end

#icon_image_attributesObject



154
155
156
157
158
159
160
161
162
# File 'app/components/hakumi_components/qr_code/component.rb', line 154

def icon_image_attributes
  T.let({
    src: @icon_url,
    alt: "",
    class: "hakumi-qr-code-icon-image",
    "aria-hidden": true,
    role: "presentation"
  }, ImageAttributes)
end

#icon_markupObject



144
145
146
147
148
149
150
151
# File 'app/components/hakumi_components/qr_code/component.rb', line 144

def icon_markup
  icon_component = @icon_component
  if icon_image?
    tag.img(**icon_image_attributes)
  elsif !icon_component.nil?
    render(icon_component)
  end
end

#renderer_attributesObject



131
132
133
134
135
136
# File 'app/components/hakumi_components/qr_code/component.rb', line 131

def renderer_attributes
  T.let({
    class: "hakumi-qr-code-inner",
    data: { "hakumi--qr-code-target": "renderer" }
  }, Types::HtmlAttributes)
end

#status_descriptionObject



192
193
194
195
196
197
198
199
200
201
# File 'app/components/hakumi_components/qr_code/component.rb', line 192

def status_description
  case @status
  when :expired
    t_default("description.expired", default: "Please refresh to continue")
  when :scanned
    t_default("description.scanned", default: "You can now continue")
  else
    nil
  end
end

#status_overlay?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'app/components/hakumi_components/qr_code/component.rb', line 165

def status_overlay?
  @status != :active
end

#status_overlay_markupObject



170
171
172
173
174
175
176
177
# File 'app/components/hakumi_components/qr_code/component.rb', line 170

def status_overlay_markup
  return nil unless status_overlay?

  custom = custom_status_overlay
  return custom if custom

  default_status_overlay
end

#status_textObject



180
181
182
183
184
185
186
187
188
189
# File 'app/components/hakumi_components/qr_code/component.rb', line 180

def status_text
  key = "status.#{@status}"
  default = case @status
  when :expired then "Expired"
  when :loading then "Loading"
  when :scanned then "Scanned"
  else "Active"
  end
  t_default(key, default: default)
end

#wrapper_attributesObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/components/hakumi_components/qr_code/component.rb', line 116

def wrapper_attributes
  merge_attributes(
    {
      class: wrapper_classes,
      style: wrapper_styles,
      role: "img",
      "aria-label": @aria_label || t_default(:aria_label, default: "QR Code"),
      "aria-busy": loading? ? "true" : "false",
      data: data_attributes
    },
    @html_options
  )
end