Skip to content

Instantly share code, notes, and snippets.

@pocke
Created July 15, 2019 05:54
Show Gist options
  • Save pocke/368891892417384fbc73fe5987128185 to your computer and use it in GitHub Desktop.
Save pocke/368891892417384fbc73fe5987128185 to your computer and use it in GitHub Desktop.
diff --git a/examples/static/Gemfile.lock b/examples/static/Gemfile.lock
index f2985fe..01959f6 100644
--- a/examples/static/Gemfile.lock
+++ b/examples/static/Gemfile.lock
@@ -2,7 +2,7 @@ PATH
remote: ../..
specs:
ovto (0.4.1)
- opal (~> 0.11)
+ opal (>= 0.11, < 2)
rack (~> 2.0)
thor (~> 0.20)
@@ -33,4 +33,4 @@ DEPENDENCIES
rake
BUNDLED WITH
- 2.0.1
+ 2.1.0.pre.1
diff --git a/examples/static/app.rb b/examples/static/app.rb
index 091d4a2..93444cd 100644
--- a/examples/static/app.rb
+++ b/examples/static/app.rb
@@ -20,21 +20,34 @@ class MyApp < Ovto::App
end
end
+ class ComponentWithChildren < Ovto::Component
+ def render(&children)
+ o 'div' do
+ o 'h1', 'hello'
+ instance_exec &children
+ end
+ end
+ end
+
class MainComponent < Ovto::Component
def render
- o 'div' do
- o 'span', 'Celcius:'
- o 'input', {
- type: 'text',
- onchange: ->(e){ actions.set_celsius(value: e.target.value.to_i) },
- value: state.celsius
- }
- o 'span', 'Fahrenheit:'
- o 'input', {
- type: 'text',
- onchange: ->(e){ actions.set_fahrenheit(value: e.target.value.to_i) },
- value: state.fahrenheit
- }
+ o ComponentWithChildren do
+ p 'in block'
+ o 'div' do
+ o 'h2', 'hi'
+ o 'span', 'Celcius:'
+ o 'input', {
+ type: 'text',
+ onchange: ->(e){ actions.set_celsius(value: e.target.value.to_i) },
+ value: state.celsius
+ }
+ o 'span', 'Fahrenheit:'
+ o 'input', {
+ type: 'text',
+ onchange: ->(e){ actions.set_fahrenheit(value: e.target.value.to_i) },
+ value: state.fahrenheit
+ }
+ end
end
end
end
diff --git a/lib/ovto/component.rb b/lib/ovto/component.rb
index a6a378c..0bd43b1 100644
--- a/lib/ovto/component.rb
+++ b/lib/ovto/component.rb
@@ -35,7 +35,7 @@ module Ovto
do_render(state: state)
end
- def do_render(**args)
+ def do_render(**args, &children)
Ovto.debug_trace_log("rendering #{self}")
@vdom_tree = []
@done_render = false
@@ -43,12 +43,12 @@ module Ovto
parameters = method(:render).parameters
if `!parameters` || parameters.nil? || accepts_state?(parameters)
# We can pass `state:` safely
- return render(**args)
+ return render(**args, &children)
else
# Remove `state:` keyword
args_wo_state = args.reject{|k, v| k == :state}
# Check it is empty (see https://github.com/opal/opal/issues/1872)
- return args_wo_state.empty? ? render() : render(**args_wo_state)
+ return args_wo_state.empty? ? render(&children) : render(**args_wo_state, &children)
end
end
@@ -90,10 +90,9 @@ module Ovto
raise ArgumentError
end
- children = render_children(content, block)
case _tag_name
when Class
- result = render_component(_tag_name, attributes, children)
+ result = render_component(_tag_name, attributes, block)
when 'text'
unless attributes.empty?
raise ArgumentError, "text cannot take attributes"
@@ -103,6 +102,7 @@ module Ovto
tag_name, base_attributes = *extract_attrs(_tag_name)
# Ignore nil/false
more_attributes = attributes.reject{|k, v| !v}
+ children = render_children(content, block)
result = render_tag(tag_name, merge_attrs(base_attributes, more_attributes), children)
else
raise TypeError, "tag_name must be a String or Component but got "+
@@ -193,7 +193,7 @@ module Ovto
def render_component(comp_class, args, children)
comp = comp_class.new(@wired_actions)
render_args = {state: @current_state}.merge(args)
- return comp.do_render(**render_args){ children }
+ return comp.do_render(**render_args, &children)
end
def render_tag(tag_name, attributes, children)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment