Class: HakumiComponents::Timeline::Item::Component

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

Constant Summary collapse

ColorValue =
T.type_alias { Types::HtmlKey }
ContentBlock =
T.type_alias { T.nilable(T.proc.returns(Types::RenderedContent)) }
COLORS =
T.let(
  {
    blue: "var(--color-primary)",
    red: "var(--color-error)",
    green: "var(--color-success)",
    gray: "var(--color-text-secondary)"
  }.freeze,
  T::Hash[Symbol, String]
)
POSITIONS =
T.let(%i[left right].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 Attribute 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, body: nil, color: :blue, dot: nil, position: nil, loading: false, parent: nil, pending: false, **html_options, &block) ⇒ Component

Returns a new instance of Component.



38
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
# File 'app/components/hakumi_components/timeline/item/component.rb', line 38

def initialize(
  title: nil,
  body: nil,
  color: :blue,
  dot: nil,
  position: nil,
  loading: false,
  parent: nil,
  pending: false,
  **html_options,
  &block
)
  @title = T.let(title, Types::Renderable)
  @body = T.let(body, Types::Renderable)
  @color = T.let(color.is_a?(String) ? color : color.to_sym, ColorValue)
  @dot = T.let(dot, Types::Renderable)
  @position = T.let(position, T.nilable(Symbol))
  @loading = T.let(cast_boolean(loading) ? true : false, T::Boolean)
  @parent = T.let(parent, T.nilable(HakumiComponents::Timeline::Component))
  @pending = T.let(cast_boolean(pending) ? true : false, T::Boolean)
  @html_options = T.let(html_options, Types::HtmlAttributes)
  @content_block = T.let(block, ContentBlock)
  @index = T.let(nil, T.nilable(Integer))
  @total = T.let(nil, T.nilable(Integer))
  @resolved_position = T.let(nil, T.nilable(Symbol))
  @reverse = T.let(false, T::Boolean)
  validate_props!
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



68
69
70
# File 'app/components/hakumi_components/timeline/item/component.rb', line 68

def body
  @body
end

#colorObject (readonly)

Returns the value of attribute color.



71
72
73
# File 'app/components/hakumi_components/timeline/item/component.rb', line 71

def color
  @color
end

#dotObject (readonly)

Returns the value of attribute dot.



68
69
70
# File 'app/components/hakumi_components/timeline/item/component.rb', line 68

def dot
  @dot
end

#loadingObject (readonly)

Returns the value of attribute loading.



77
78
79
# File 'app/components/hakumi_components/timeline/item/component.rb', line 77

def loading
  @loading
end

#parentObject (readonly)

Returns the value of attribute parent.



80
81
82
# File 'app/components/hakumi_components/timeline/item/component.rb', line 80

def parent
  @parent
end

#pendingObject (readonly)

Returns the value of attribute pending.



77
78
79
# File 'app/components/hakumi_components/timeline/item/component.rb', line 77

def pending
  @pending
end

#positionObject (readonly)

Returns the value of attribute position.



74
75
76
# File 'app/components/hakumi_components/timeline/item/component.rb', line 74

def position
  @position
end

#titleObject (readonly)

Returns the value of attribute title.



68
69
70
# File 'app/components/hakumi_components/timeline/item/component.rb', line 68

def title
  @title
end

Instance Method Details

#dot_contentObject



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

def dot_content
  rendered_dot = render_value(@dot)
  return rendered_dot if rendered_dot.present?
  return render(HakumiComponents::Icon::Component.new(name: :loading, spin: true, size: 12)) if loading

  nil
end

#item_contentObject



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

def item_content
  rendered_body = render_value(@body)
  return rendered_body if rendered_body.present?
  return content if content.present?

  if @content_block && view_context
    captured = view_context.capture(&@content_block)
    return captured if captured.present?
  end

  nil
end

#last_item?Boolean

Returns:

  • (Boolean)


135
136
137
138
139
140
141
# File 'app/components/hakumi_components/timeline/item/component.rb', line 135

def last_item?
  index = @index
  total = @total
  return false if index.nil? || total.nil?

  @reverse ? index.zero? : index == (total - 1)
end

#resolved_positionObject



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

def resolved_position
  @resolved_position || @position || :left
end

#title_contentObject



111
112
113
# File 'app/components/hakumi_components/timeline/item/component.rb', line 111

def title_content
  render_value(@title)
end

#with_context(index:, total:, position:, reverse: false) ⇒ Object



90
91
92
93
94
95
96
# File 'app/components/hakumi_components/timeline/item/component.rb', line 90

def with_context(index:, total:, position:, reverse: false)
  @index = index
  @total = total
  @resolved_position = position
  @reverse = reverse
  self
end

#wrapper_attributesObject



99
100
101
102
103
104
105
106
107
108
# File 'app/components/hakumi_components/timeline/item/component.rb', line 99

def wrapper_attributes
  merge_attributes(
    {
      class: item_classes,
      data: item_data,
      style: dot_style
    },
    @html_options
  )
end