Class: Lifeform::Libraries::Default::Button

Inherits:
Phlex::HTML
  • Object
show all
Includes:
CapturingRenderable
Defined in:
lib/lifeform/libraries/default/button.rb

Direct Known Subclasses

SubmitButton, Shoelace::Button

Constant Summary collapse

WRAPPER_TAG =
:form_button
BUTTON_TAG =
:button

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CapturingRenderable

#render_in

Constructor Details

#initialize(form, field_definition, **attributes) ⇒ Button

Returns a new instance of Button.



17
18
19
20
21
22
23
24
# File 'lib/lifeform/libraries/default/button.rb', line 17

def initialize(form, field_definition, **attributes)
  @form = form
  @field_definition = field_definition
  @attributes = Lifeform::Form.parameters_to_attributes(field_definition.parameters).merge(attributes)
  @if = @attributes.delete(:if)
  @label = @attributes.delete(:label) || "Unlabeled Button"
  @attributes[:type] ||= "button"
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



10
11
12
# File 'lib/lifeform/libraries/default/button.rb', line 10

def attributes
  @attributes
end

#field_definitionObject (readonly)

Returns the value of attribute field_definition.



10
11
12
# File 'lib/lifeform/libraries/default/button.rb', line 10

def field_definition
  @field_definition
end

#formObject (readonly)

Returns the value of attribute form.



10
11
12
# File 'lib/lifeform/libraries/default/button.rb', line 10

def form
  @form
end

Instance Method Details

#template(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lifeform/libraries/default/button.rb', line 26

def template(&block)
  return if !@if.nil? && !@if

  wrapper_tag = self.class.const_get(:WRAPPER_TAG)
  button_tag = self.class.const_get(:BUTTON_TAG)

  field_body = proc {
    send(button_tag, **@attributes) do
      unsafe_raw(@label.to_s) unless block
      yield_content(&block)
    end
  }
  return field_body.() unless wrapper_tag

  send wrapper_tag, name: @attributes[:name], &field_body
end