lib/lifeform/libraries/default/input.rb in lifeform-0.5 vs lib/lifeform/libraries/default/input.rb in lifeform-0.7

- old
+ new

@@ -1,16 +1,20 @@ # frozen_string_literal: true module Lifeform module Libraries class Default - class Input + class Input < Phlex::View + include CapturingRenderable + attr_reader :form, :field_definition, :attributes - INPUT_TAG = :input WRAPPER_TAG = :form_field + INPUT_TAG = :input + 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) @field_type = field_definition.type @@ -38,47 +42,33 @@ def value_for_model @model.send(attributes[:name]) end def handle_labels - Papercraft.html do |label_text, name| - label label_text, for: name - end.render( - attributes[:label].to_s, - (attributes[:id] || attributes[:name]).to_s - ).tap do - @attributes = attributes.filter_map { |k, v| [k, v] unless k == :label }.to_h - end - end + label_text = attributes[:label].to_s + label_name = (attributes[:id] || attributes[:name]).to_s - def render_in(view_context, &block) - @view_context = view_context - @content = block - return "" if !@if.nil? && !@if + @attributes = attributes.filter_map { |k, v| [k, v] unless k == :label }.to_h - template + proc { + label(for: label_name) { raw label_text } + } end - def template # rubocop:disable Metrics/AbcSize - Papercraft.html do |wrapper_tag:, input_tag:, attributes:, field_data:| - field_body = proc { - emit(field_data[:label]) if field_data[:label] - send input_tag, type: field_data[:type], **attributes - emit(field_data[:content]) if field_data[:content] - } - next field_body.call unless wrapper_tag + def template(&block) + return if !@if.nil? && !@if - send wrapper_tag, name: attributes[:name], &field_body - end.render( - wrapper_tag: self.class.const_get(:WRAPPER_TAG), - input_tag: self.class.const_get(:INPUT_TAG), - attributes: attributes, - field_data: { - type: @field_type, - label: @label, - content: @content && @view_context.capture(&@content) - } - ) + wrapper_tag = self.class.const_get(:WRAPPER_TAG) + input_tag = self.class.const_get(:INPUT_TAG) + + field_body = proc { + @label&.() + send input_tag, type: @field_type.to_s, **@attributes + yield_content(&block) + } + return field_body.() unless wrapper_tag + + send wrapper_tag, name: @attributes[:name], &field_body end end end end end