lib/lifeform/libraries/default/button.rb in lifeform-0.11.0 vs lib/lifeform/libraries/default/button.rb in lifeform-0.12.0

- old
+ new

@@ -1,44 +1,45 @@ # frozen_string_literal: true module Lifeform module Libraries class Default - class Button < Phlex::HTML - using RefineProcToString - include CapturingRenderable + class Button + include Streamlined::Renderable 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 + return "" if !@if.nil? && !@if - wrapper_tag = self.class.const_get(:WRAPPER_TAG) - button_tag = self.class.const_get(:BUTTON_TAG) + wrapper_tag = dashed self.class.const_get(:WRAPPER_TAG) + button_tag = dashed 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 + label_text = block ? capture(self, &block) : @label.is_a?(Proc) ? @label.pipe : @label # rubocop:disable Style/NestedTernaryOperator + + field_body = html -> { <<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc + <#{button_tag}#{html_attributes @attributes, prefix_space: true}>#{text -> { label_text }}</#{button_tag}> + HTML } - return field_body.() unless wrapper_tag - send wrapper_tag, name: @attributes[:name], &field_body + return field_body unless wrapper_tag + + html -> { <<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc + <#{wrapper_tag} #{html_attributes name: @attributes[:name]}>#{field_body}</#{wrapper_tag}> + HTML + } end end end end end