Sha256: df22f102894567104411e741353f989fc1baf3cf2e0a4a16918a8498e23f803d

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

module Lookbook
  module Params
    class Field::Component < Lookbook::BaseComponent
      attr_reader :param, :rendered_input

      def initialize(param:, index:, **html_attrs)
        @param = param
        @index = index
        @rendered_input = nil
        super(**html_attrs)
      end

      def before_render
        styles, html = StylesExtractor.call(render_input)
        Editor::Component.add_styles(param.input, styles)

        escaped_value = json_escape(param.value.to_json)
        wrapper_attrs = {
          data: {"param-input": param.input},
          "x-data": "paramsInputComponent({name: '#{param.name}', value: #{escaped_value}})"
        }
        @rendered_input = tag.div(**wrapper_attrs) { html.html_safe }
      end

      protected

      def render_input
        input_options = param.input_options.to_h
        input_options[:id] = "param-#{param.name}"

        render(param.input_partial,
          name: param.name,
          input: param.input,
          value: param.value,
          value_type: param.value_type,
          value_default: param.value_default,
          input_options: input_options.except(:choices),
          choices: input_options[:choices])
      end

      def alpine_component
        "paramsFieldComponent"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lookbook-1.4.1 app/components/lookbook/params/field/component.rb
lookbook-1.4.0 app/components/lookbook/params/field/component.rb
lookbook-1.3.4 app/components/lookbook/params/field/component.rb
lookbook-1.3.3 app/components/lookbook/params/field/component.rb
lookbook-1.3.1 app/components/lookbook/params/field/component.rb
lookbook-1.3.0 app/components/lookbook/params/field/component.rb