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

Inherits:
Object
  • Object
show all
Includes:
Streamlined::Renderable
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

Constructor Details

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

Returns a new instance of Button.



14
15
16
17
18
19
20
21
# File 'lib/lifeform/libraries/default/button.rb', line 14

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.



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

def attributes
  @attributes
end

#field_definitionObject (readonly)

Returns the value of attribute field_definition.



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

def field_definition
  @field_definition
end

#formObject (readonly)

Returns the value of attribute form.



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

def form
  @form
end

Instance Method Details

#template(&block) ⇒ Object



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

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

  button_tag = dashed self.class.const_get(:BUTTON_TAG)
  label_text = block ? capture(self, &block) : @label

  field_body = html -> { <<~HTML # rubocop:disable Bridgetown/InsecureHeredoc
    <#{button_tag}#{html_attributes @attributes, prefix_space: true}>#{text label_text}</#{button_tag}>
  HTML
  }

  return field_body unless self.class.const_get(:WRAPPER_TAG)

  wrapper_tag = dashed self.class.const_get(:WRAPPER_TAG)
  html -> { <<~HTML # rubocop:disable Bridgetown/InsecureHeredoc
    <#{wrapper_tag} #{html_attributes name: @attributes[:name]}>#{field_body}</#{wrapper_tag}>
  HTML
  }
end