Sha256: 3008a59e62a0c69cac462bcea59253bbc31b9c76df5d0092120a6d9d50de6bf1

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module Lifeform
  module Libraries
    class Default
      class Button < Phlex::View
        include CapturingRenderable

        attr_reader :form, :field_definition, :attributes

        WRAPPER_TAG = :form_button
        BUTTON_TAG = :button

        register_element WRAPPER_TAG

        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

        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
              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
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lifeform-0.9.0 lib/lifeform/libraries/default/button.rb
lifeform-0.8.0 lib/lifeform/libraries/default/button.rb
lifeform-0.7 lib/lifeform/libraries/default/button.rb