Class: HakumiComponents::FloatButton::GroupSpec

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

Constant Summary collapse

RenderFactory =
T.type_alias { T.proc.returns(Types::Renderable) }
ItemInput =
T.type_alias { T.any(HakumiComponents::FloatButton::ItemSpec::InputAttributes, HakumiComponents::FloatButton::ItemSpec) }
GroupInput =
T.type_alias { T.any(InputAttributes, Types::Renderable, RenderFactory, HakumiComponents::FloatButton::GroupSpec) }
InputValue =
T.type_alias do
  T.nilable(T.any(
    Types::ValidationPrimitive,
    Types::Renderable,
    Types::HtmlAttributes,
    RenderFactory,
    T::Array[ItemInput]
  ))
end
InputAttributes =
T.type_alias { T::Hash[Symbol, InputValue] }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(icon: :plus, tooltip: nil, shape: :circle, type: :default, trigger: :click, open: nil, default_open: false, expand_direction: :top, items: [], floating: false, placement: :bottom_right, offset: nil, button_options: {}, html_options: {}, content: nil, renderable: nil, render_factory: nil) ⇒ GroupSpec

Returns a new instance of GroupSpec.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/components/hakumi_components/float_button/group_spec.rb', line 44

def initialize(
  icon: :plus,
  tooltip: nil,
  shape: :circle,
  type: :default,
  trigger: :click,
  open: nil,
  default_open: false,
  expand_direction: :top,
  items: [],
  floating: false,
  placement: :bottom_right,
  offset: nil,
  button_options: {},
  html_options: {},
  content: nil,
  renderable: nil,
  render_factory: nil
)
  @icon = T.let(icon, HakumiComponents::FloatButton::Component::IconValue)
  @tooltip = T.let(tooltip, T.nilable(String))
  @shape = T.let(shape, Symbol)
  @type = T.let(type, Symbol)
  @trigger = T.let(trigger, Symbol)
  @open = T.let(open, T.nilable(T::Boolean))
  @default_open = T.let(default_open, T::Boolean)
  @expand_direction = T.let(expand_direction, Symbol)
  @items = T.let(items, T::Array[HakumiComponents::FloatButton::ItemSpec])
  @floating = T.let(floating, T::Boolean)
  @placement = T.let(placement, Symbol)
  @offset = T.let(offset, T.nilable(HakumiComponents::FloatButton::PositionConfig::OffsetMap))
  @button_options = T.let(button_options, Types::HtmlAttributes)
  @html_options = T.let(html_options, Types::HtmlAttributes)
  @content = T.let(content, T.nilable(RenderFactory))
  @renderable = T.let(renderable, Types::Renderable)
  @render_factory = T.let(render_factory, T.nilable(RenderFactory))
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



86
87
88
# File 'app/components/hakumi_components/float_button/group_spec.rb', line 86

def content
  @content
end

#renderableObject (readonly)

Returns the value of attribute renderable.



83
84
85
# File 'app/components/hakumi_components/float_button/group_spec.rb', line 83

def renderable
  @renderable
end

Class Method Details

.coerce(group) ⇒ Object

Raises:

  • (ArgumentError)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/components/hakumi_components/float_button/group_spec.rb', line 122

def self.coerce(group)
  return group if group.is_a?(HakumiComponents::FloatButton::GroupSpec)
  renderable = coerce_renderable(group)
  return new(renderable: renderable) if renderable
  return new(render_factory: group) if group.is_a?(Proc)

  raise ArgumentError, "Expected group definition to be a Hash, proc, or renderable component" unless group.is_a?(Hash)

  normalized = group.deep_symbolize_keys

  new(
    icon: coerce_icon(normalized[:icon]),
    tooltip: coerce_string(normalized[:tooltip]),
    shape: coerce_symbol(normalized[:shape]) || :circle,
    type: coerce_symbol(normalized[:type]) || :default,
    trigger: coerce_symbol(normalized[:trigger]) || :click,
    open: coerce_boolean(normalized[:open]),
    default_open: normalized[:default_open] == true,
    expand_direction: coerce_symbol(normalized[:expand_direction]) || :top,
    items: HakumiComponents::FloatButton::ItemSpec.coerce_all(coerce_items(normalized[:items])),
    floating: normalized[:floating] == true,
    placement: coerce_symbol(normalized[:placement]) || :bottom_right,
    offset: coerce_offset_map(normalized[:offset]),
    button_options: coerce_html_attributes(normalized[:button_options]),
    html_options: coerce_html_options(normalized.except(
      :icon, :tooltip, :shape, :type, :trigger, :open, :default_open,
      :expand_direction, :items, :floating, :placement, :offset,
      :button_options, :content
    )),
    content: coerce_render_factory(normalized[:content])
  )
end

.coerce_all(groups) ⇒ Object



117
118
119
# File 'app/components/hakumi_components/float_button/group_spec.rb', line 117

def self.coerce_all(groups)
  groups.map { |group| coerce(group) }
end

Instance Method Details

#build_group_componentObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/components/hakumi_components/float_button/group_spec.rb', line 97

def build_group_component
  HakumiComponents::FloatButton::Group::Component.new(
    icon: @icon,
    tooltip: @tooltip,
    shape: @shape,
    type: @type,
    trigger: @trigger,
    open: @open,
    default_open: @default_open,
    expand_direction: @expand_direction,
    items: @items,
    floating: @floating,
    placement: @placement,
    offset: @offset,
    button_options: @button_options,
    **@html_options
  )
end

#resolve_renderableObject



89
90
91
92
93
94
# File 'app/components/hakumi_components/float_button/group_spec.rb', line 89

def resolve_renderable
  return @renderable if @renderable
  return @render_factory.call if @render_factory

  nil
end