Sha256: 88cfdd475a6a9caff70d2e642a371884dac29a2b2638ae042d3a1c8d18e9da24

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 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_s)
        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.to_s,
          value_type: param.value_type,
          value_default: param.value_default,
          input_options: input_options.except(:choices, :opts),
          choices: input_options[:choices])
      end

      def alpine_component
        "paramsFieldComponent"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lookbook-2.0.0.rc.2 app/components/lookbook/params/field/component.rb
lookbook-2.0.0.rc.1 app/components/lookbook/params/field/component.rb
lookbook-2.0.0.beta.9 app/components/lookbook/params/field/component.rb
lookbook-2.0.0.beta.8 app/components/lookbook/params/field/component.rb