Class: Field::Component

Inherits:
Lookbook::BaseComponent
  • Object
show all
Defined in:
app/components/lookbook/display_options/field/component.rb,
app/components/lookbook/params/field/component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param:, index:, **html_attrs) ⇒ Component

Returns a new instance of Component.



6
7
8
9
10
11
# File 'app/components/lookbook/display_options/field/component.rb', line 6

def initialize(name:, opts:, value:, **html_attrs)
  @name = name
  @opts = opts
  @value = value
  super(**html_attrs)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'app/components/lookbook/display_options/field/component.rb', line 4

def name
  @name
end

#paramObject (readonly)

Returns the value of attribute param.



4
5
6
# File 'app/components/lookbook/params/field/component.rb', line 4

def param
  @param
end

#rendered_inputObject (readonly)

Returns the value of attribute rendered_input.



4
5
6
# File 'app/components/lookbook/params/field/component.rb', line 4

def rendered_input
  @rendered_input
end

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'app/components/lookbook/display_options/field/component.rb', line 4

def value
  @value
end

Instance Method Details

#before_renderObject



13
14
15
16
17
18
19
20
21
22
23
# File 'app/components/lookbook/params/field/component.rb', line 13

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

  escaped_value = json_escape(param.value.to_s).gsub("\n", '\n')
  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

#choicesObject



13
14
15
# File 'app/components/lookbook/display_options/field/component.rb', line 13

def choices
  @opts.is_a?(Hash) ? @opts[:choices].to_a : @opts
end