Class: React::Sinatra::Component
- Inherits:
-
Object
- Object
- React::Sinatra::Component
- Includes:
- Padrino::Helpers::OutputHelpers, Padrino::Helpers::TagHelpers
- Defined in:
- lib/react/sinatra/component.rb
Overview
Class for expressing react component.
This is main entrypoint for rendering react component
and is referred from {React::Sinatra::Helpers#react_component}.
Instance Attribute Summary collapse
-
#component ⇒ Object
readonly
Returns the value of attribute component.
-
#prerender ⇒ Object
readonly
Returns the value of attribute prerender.
-
#props ⇒ Object
readonly
Returns the value of attribute props.
Class Method Summary collapse
-
.camelize_props(props) ⇒ Hash
Camelizes props for enabling Component.camelize_props option.
-
.render(component, props, prerender: false, **options) ⇒ ActiveSupport::SafeBuffer
Renders react component from given arguments.
Instance Method Summary collapse
-
#initialize(component, prerender: false, **options) ⇒ React::Sinatra::Component
constructor
Constructs an instance of React::Sinatra::Component.
-
#render(props, camelize_props: false, tag: :div, **options, &block) ⇒ ActiveSupport::SafeBuffer
Renders react component from given arguments and component.
Constructor Details
#initialize(component, prerender: false, **options) ⇒ React::Sinatra::Component
Constructs an instance of React::Sinatra::Component.
60 61 62 63 |
# File 'lib/react/sinatra/component.rb', line 60 def initialize(component, prerender: false, **) @component = component @prerender = prerender end |
Instance Attribute Details
#component ⇒ Object (readonly)
Returns the value of attribute component.
17 18 19 |
# File 'lib/react/sinatra/component.rb', line 17 def component @component end |
#prerender ⇒ Object (readonly)
Returns the value of attribute prerender.
17 18 19 |
# File 'lib/react/sinatra/component.rb', line 17 def prerender @prerender end |
#props ⇒ Object (readonly)
Returns the value of attribute props.
17 18 19 |
# File 'lib/react/sinatra/component.rb', line 17 def props @props end |
Class Method Details
.camelize_props(props) ⇒ Hash
Camelizes props for enabling camelize_props option.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/react/sinatra/component.rb', line 40 def self.camelize_props(props) case props when Hash props.each_with_object({}) do |(key, value), new_props| new_key = key.to_s.camelize(:lower) new_value = camelize_props(value) new_props[new_key] = new_value end when Array props.map(&method(:camelize_props)) else props end end |
.render(component, props, prerender: false, **options) ⇒ ActiveSupport::SafeBuffer
Renders react component from given arguments.
32 33 34 |
# File 'lib/react/sinatra/component.rb', line 32 def self.render(component, props, prerender: false, **) new(component, prerender: prerender).render(props, **) end |
Instance Method Details
#render(props, camelize_props: false, tag: :div, **options, &block) ⇒ ActiveSupport::SafeBuffer
Renders react component from given arguments and component.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/react/sinatra/component.rb', line 72 def render(props, camelize_props: false, tag: :div, **, &block) props = self.class.camelize_props(props) if camelize_props block = -> { concat React::Sinatra::Pool.render(component, props.to_json) } if prerender? .reverse_merge!(data: {}) [:data].tap do |data| data[:react_class] = component data[:react_props] = props.is_a?(String) ? props : props.to_json end unless prerender == :static content_tag(tag, '', , &block) end |