Class: HakumiComponents::Notification::Component

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

Constant Summary collapse

Type =
T.type_alias { T.any(Symbol, String) }
Placement =
T.type_alias { T.any(Symbol, String) }
TYPES =
T.let(%i[success info warning error].freeze, T::Array[Symbol])
PLACEMENTS =
T.let(%i[top topLeft topRight bottom bottomLeft bottomRight].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

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(title: nil, description: nil, type: nil, duration: nil, key: nil, placement: nil, icon: nil, close_icon: nil, btn: nil, max_count: nil, top: nil, bottom: nil, pause_on_hover: nil, show_progress: nil, stack_threshold: nil, **html_options) ⇒ Component

Returns a new instance of Component.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/components/hakumi_components/notification/component.rb', line 59

def initialize(
  title: nil,
  description: nil,
  type: nil,
  duration: nil,
  key: nil,
  placement: nil,
  icon: nil,
  close_icon: nil,
  btn: nil,
  max_count: nil,
  top: nil,
  bottom: nil,
  pause_on_hover: nil,
  show_progress: nil,
  stack_threshold: nil,
  **html_options
)
  @title = T.let(title, HakumiComponents::Types::HtmlPrimitive)
  @description = T.let(description, HakumiComponents::Types::HtmlPrimitive)
  @type = T.let(type&.to_sym, T.nilable(Symbol))
  @duration = T.let(duration, T.nilable(Float))
  @key = T.let(key, HakumiComponents::Types::HtmlPrimitive)
  @placement = T.let(placement&.to_sym, T.nilable(Symbol))
  @icon = T.let(icon, HakumiComponents::Types::HtmlPrimitive)
  @close_icon = T.let(close_icon, HakumiComponents::Types::HtmlPrimitive)
  @btn = T.let(btn, HakumiComponents::Types::HtmlPrimitive)
  @max_count = T.let(max_count, T.nilable(Integer))
  @top = T.let(top, T.nilable(Integer))
  @bottom = T.let(bottom, T.nilable(Integer))
  @pause_on_hover = T.let(pause_on_hover, T.nilable(T::Boolean))
  @show_progress = T.let(show_progress, T.nilable(T::Boolean))
  @stack_threshold = T.let(stack_threshold, T.nilable(Integer))
  @html_options = T.let(html_options, HakumiComponents::Types::HtmlAttributes)

  validate_props!
end

Class Method Details

.extract_controller_locals(params) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/components/hakumi_components/notification/component.rb', line 19

def self.extract_controller_locals(params)
  {
    title: html_primitive_param(html_param(params, :title)) || html_primitive_param(html_param(params, :message)),
    description: html_primitive_param(html_param(params, :description)),
    type: symbol_html_param(html_param(params, :type)),
    duration: float_html_param(html_param(params, :duration)),
    key: html_primitive_param(html_param(params, :key)),
    placement: symbol_html_param(html_param(params, :placement)),
    icon: html_primitive_param(html_param(params, :icon)),
    close_icon: html_primitive_param(html_param(params, :close_icon)),
    btn: html_primitive_param(html_param(params, :btn)),
    max_count: integer_html_param(html_param(params, :max_count)),
    top: integer_html_param(html_param(params, :top)),
    bottom: integer_html_param(html_param(params, :bottom)),
    pause_on_hover: boolean_html_param(html_param(params, :pause_on_hover)),
    show_progress: boolean_html_param(html_param(params, :show_progress)),
    stack_threshold: integer_html_param(html_param(params, :stack_threshold))
  }
end

Instance Method Details

#callObject



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

def call
  (:div, nil, root_attributes)
end

#data_attributesObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/components/hakumi_components/notification/component.rb', line 109

def data_attributes
  attrs = T.let({
    controller: "hakumi--notification"
  }, HakumiComponents::Types::DataAttributes)

  attrs[:hakumi__notification_duration_value] = @duration unless @duration.nil?
  attrs[:hakumi__notification_max_count_value] = @max_count unless @max_count.nil?
  attrs[:hakumi__notification_top_value] = @top unless @top.nil?
  attrs[:hakumi__notification_bottom_value] = @bottom unless @bottom.nil?
  attrs[:hakumi__notification_placement_value] = @placement if @placement
  attrs[:hakumi__notification_pause_on_hover_value] = @pause_on_hover unless @pause_on_hover.nil?
  attrs[:hakumi__notification_show_progress_value] = @show_progress unless @show_progress.nil?
  attrs[:hakumi__notification_stack_threshold_value] = @stack_threshold unless @stack_threshold.nil?

  payload = initial_payload
  attrs[:hakumi__notification_payload_value] = payload.to_json if payload.present?

  attrs
end

#root_attributesObject



98
99
100
101
102
103
104
105
106
# File 'app/components/hakumi_components/notification/component.rb', line 98

def root_attributes
  base_attrs = {
    id: "notification",
    class: component_classes("notification", {}, @html_options),
    data: data_attributes
  }

  merge_attributes(base_attrs, @html_options)
end