Class: LiveComponent::RenderComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- LiveComponent::RenderComponent
- Defined in:
- app/components/live_component/render_component.rb
Instance Method Summary collapse
-
#initialize(state, reflexes, prop_overrides = {}) ⇒ RenderComponent
constructor
A new instance of RenderComponent.
- #render_in(view_context, &block) ⇒ Object
Constructor Details
#initialize(state, reflexes, prop_overrides = {}) ⇒ RenderComponent
Returns a new instance of RenderComponent.
5 6 7 8 |
# File 'app/components/live_component/render_component.rb', line 5 def initialize(state, reflexes, prop_overrides = {}) @state = LiveComponent::State.build(state, prop_overrides) @reflexes = reflexes end |
Instance Method Details
#render_in(view_context, &block) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'app/components/live_component/render_component.rb', line 10 def render_in(view_context, &block) component = @state.klass.new(**@state.props.symbolize_keys) @reflexes.each do |reflex| method_name = reflex["method_name"] || reflex[:method_name] props = (reflex["props"] || reflex[:props] || {}).each_with_object({}) do |(k, v), memo| memo[k.to_sym] = LiveComponent.serializer.deserialize(v) end SafeDispatcher.send_safely(component, method_name, **props) end component.render_in(view_context) do apply_slots(component, @state.slots) block.call(component) if block @state.content end end |