Skip to content

Commit 245235e

Browse files
committed
closes #43 #44 #46
1 parent 33224f1 commit 245235e

File tree

9 files changed

+77
-53
lines changed

9 files changed

+77
-53
lines changed

HYPERLOOP_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.99.1
1+
0.99.2

ruby/hyper-component/Gemfile.lock

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
GIT
2-
remote: https://github.com/opal/opal-jquery.git
3-
revision: 6249dfaf406ecd7199079b4f4d9dc67cfdf4d602
4-
branch: master
5-
specs:
6-
opal-jquery (0.4.3)
7-
opal (>= 0.10.0, < 0.12.0)
8-
91
PATH
102
remote: ../hyper-spec
113
specs:
12-
hyper-spec (1.0.0.lap28)
4+
hyper-spec (0.99.1)
135
capybara
14-
chromedriver-helper
6+
chromedriver-helper (= 1.2.0)
157
libv8 (~> 6.3.0)
168
method_source
179
mini_racer (~> 0.1.15)
@@ -28,14 +20,14 @@ PATH
2820
PATH
2921
remote: ../hyper-store
3022
specs:
31-
hyper-store (1.0.0.lap28)
32-
hyperloop-config (= 1.0.0.lap28)
23+
hyper-store (0.99.1)
24+
hyperloop-config (= 0.99.1)
3325
opal (>= 0.11.0, < 0.12.0)
3426

3527
PATH
3628
remote: ../hyperloop-config
3729
specs:
38-
hyperloop-config (1.0.0.lap28)
30+
hyperloop-config (0.99.1)
3931
libv8 (~> 6.3.0)
4032
mini_racer (~> 0.1.15)
4133
opal (>= 0.11.0, < 0.12.0)
@@ -45,9 +37,9 @@ PATH
4537
PATH
4638
remote: .
4739
specs:
48-
hyper-component (1.0.0.lap28)
49-
hyper-store (= 1.0.0.lap28)
50-
hyperloop-config (= 1.0.0.lap28)
40+
hyper-component (0.99.1)
41+
hyper-store (= 0.99.1)
42+
hyperloop-config (= 0.99.1)
5143
libv8 (~> 6.3.0)
5244
mini_racer (~> 0.1.15)
5345
opal (>= 0.11.0, < 0.12.0)
@@ -113,7 +105,7 @@ GEM
113105
babel-source (>= 4.0, < 6)
114106
execjs (~> 2.0)
115107
builder (3.2.3)
116-
capybara (3.7.2)
108+
capybara (3.8.2)
117109
addressable
118110
mini_mime (>= 0.1.3)
119111
nokogiri (~> 1.8)
@@ -148,7 +140,7 @@ GEM
148140
rails-dom-testing (>= 1, < 3)
149141
railties (>= 4.2.0)
150142
thor (>= 0.14, < 2.0)
151-
libv8 (6.3.292.48.1)
143+
libv8 (6.3.292.48.1-x86_64-darwin-15)
152144
listen (3.1.5)
153145
rb-fsevent (~> 0.9, >= 0.9.4)
154146
rb-inotify (~> 0.9, >= 0.9.7)
@@ -185,6 +177,8 @@ GEM
185177
opal-browser (0.2.0)
186178
opal
187179
paggio
180+
opal-jquery (0.4.3)
181+
opal (>= 0.10.0, < 0.12.0)
188182
opal-rails (0.9.5)
189183
jquery-rails
190184
opal (>= 0.11.0, < 0.12)
@@ -309,7 +303,7 @@ GEM
309303
equalizer (~> 0.0.9)
310304
parser (>= 2.3.1.2, < 2.6)
311305
procto (~> 0.0.2)
312-
webdrivers (3.4.0)
306+
webdrivers (3.4.1)
313307
nokogiri (~> 1.6)
314308
rubyzip (~> 1.0)
315309
selenium-webdriver (~> 3.0)
@@ -333,7 +327,7 @@ DEPENDENCIES
333327
listen
334328
mime-types
335329
nokogiri
336-
opal-jquery!
330+
opal-jquery
337331
opal-rails (~> 0.9.4)
338332
opal-rspec
339333
pry

ruby/hyper-component/lib/hyper-component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Component
2525
require 'react/state'
2626
require 'react/object'
2727
require 'react/to_key'
28-
require 'react/ext/opal-jquery/element'
28+
#require 'react/ext/opal-jquery/element' # now have to manually require this
2929
require 'reactive-ruby/isomorphic_helpers'
3030
require 'react/top_level'
3131
require 'react/top_level_render'

ruby/hyper-component/lib/react/api.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ def self.convert_props(args)
238238
elsif key == "key"
239239
props["key"] = value.to_key
240240

241-
elsif key == 'ref' && value.is_a?(Proc)
241+
elsif key == 'ref' && value.respond_to?(:call)
242+
# currently react still accepts the syntax ref: :foo meaning set refs.foo to the ref.
243+
# in hyperstack release 0.1 we can put this behavior on a switch or use the notation `ref_key: :foo` for old school
242244
props[key] = %x{
243245
function(dom_node){
244246
if (dom_node !== null && dom_node.__opalInstance !== undefined && dom_node.__opalInstance !== null) {

ruby/hyper-component/lib/react/ext/opal-jquery/element.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@ def self.[](selector)
1313
end
1414

1515
define_method :render do |container = nil, params = {}, &block|
16+
# create an invisible component class and hang it off the DOM element
1617
if `#{self.to_n}._reactrb_component_class === undefined`
17-
`#{self.to_n}._reactrb_component_class = #{Class.new(Hyperloop::Component)}`
18+
klass = Class.new(Hyperloop::Component) do
19+
# react won't rerender the components unless it sees some params
20+
# changing, so we just copy them all in, but we still just reuse
21+
# the render macro to define the action
22+
others :all_the_params
23+
end
24+
`#{self.to_n}._reactrb_component_class = #{klass}`
25+
else
26+
klass = `#{self.to_n}._reactrb_component_class`
1827
end
19-
klass = `#{self.to_n}._reactrb_component_class`
20-
klass.class_eval do
21-
render(container, params, &block)
22-
end
23-
24-
React.render(React.create_element(`#{self.to_n}._reactrb_component_class`), self)
28+
# define / redefine the render method
29+
klass.render(container, params, &block)
30+
React.render(React.create_element(klass, {container: container, params: params, block: block}), self)
2531
end
2632

2733
# mount_components is useful for dynamically generated page segments for example
@@ -34,4 +40,4 @@ def self.[](selector)
3440
}
3541
}
3642
Element.expose :mount_components
37-
end if Object.const_defined?('Element')
43+
end

ruby/hyper-component/lib/react/top_level_render.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def self.render(element, container)
1616
else
1717
native = `ReactDOM.render(#{element.to_n}, container)`
1818
end
19-
19+
20+
return unless `#{native} !== null`
21+
2022
if `#{native}.__opalInstance !== undefined && #{native}.__opalInstance !== null`
2123
`#{native}.__opalInstance`
2224
elsif `ReactDOM.findDOMNode !== undefined && #{native}.nodeType === undefined`

ruby/hyper-component/spec/react/opal_jquery_extensions_spec.rb

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,39 @@
22

33
describe 'opal-jquery extensions', js: true do
44
describe 'Element' do
5-
xit 'will reuse the wrapper component class for the same Element' do
6-
evaluate_ruby do
5+
it 'renders a top level component using render with a block' do
6+
expect_evaluate_ruby do
77
class Foo < Hyperloop::Component
88
param :name
99
def render
1010
"hello #{params.name}"
1111
end
12+
end
13+
test_div = Element.new(:div)
14+
test_div.render { Foo(name: 'fred') }
15+
Element[test_div].find('span').html
16+
end.to eq('hello fred')
17+
end
18+
19+
it 'renders a top level component using render with a container and params ' do
20+
expect_evaluate_ruby do
21+
test_div = Element.new(:div)
22+
test_div.render(SPAN, id: :render_test_span) { 'hello' }
23+
Element[test_div].find('#render_test_span').html
24+
end.to eq('hello')
25+
end
26+
27+
it 'will reuse the wrapper component class for the same Element' do
28+
evaluate_ruby do
29+
class Foo < Hyperloop::Component
30+
param :name
31+
before_mount do
32+
@render_count = 0
33+
end
34+
35+
def render
36+
"hello #{params.name} render-count: #{@render_count += 1}"
37+
end
1238

1339
def self.rec_cnt
1440
@@rec_cnt ||= 0
@@ -24,28 +50,21 @@ def self.rec_cnt
2450
test_div.render { Foo(name: 'fred') }
2551
test_div.render { Foo(name: 'freddy') }
2652
[ Element[test_div].find('span').html, Foo.rec_cnt]
27-
end.to eq(['hello freddy', 0])
28-
end
29-
30-
it 'renders a top level component using render with a block' do
53+
end.to eq(['hello freddy render-count: 2', 0])
3154
expect_evaluate_ruby do
32-
class Foo < Hyperloop::Component
33-
param :name
34-
def render
35-
"hello #{params.name}"
36-
end
37-
end
3855
test_div = Element.new(:div)
39-
test_div.render { Foo(name: 'fred') }
40-
Element[test_div].find('span').html
41-
end.to eq('hello fred')
56+
test_div.render(Foo, name: 'fred')
57+
test_div.render(Foo, name: 'freddy')
58+
[ Element[test_div].find('span').html, Foo.rec_cnt]
59+
end.to eq(['hello freddy render-count: 2', 0])
4260
end
4361

44-
it 'renders a top level component using render with a container and params ' do
45-
expect_evaluate_ruby do
62+
it 'will use the ref call back to get the component' do
63+
expect_promise do
4664
test_div = Element.new(:div)
47-
test_div.render(:span, id: :render_test_span) { 'hello' }
48-
Element[test_div].find('#render_test_span').html
65+
Promise.new.then { |c| Element[c].html }.tap do |p|
66+
test_div.render(SPAN, id: :render_test_span, ref: p.method(:resolve)) { 'hello' }
67+
end
4968
end.to eq('hello')
5069
end
5170

ruby/hyper-component/spec/react/refs_callback_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def my_bar=(bars)
3030
end
3131

3232
def render
33-
React.create_element(Bar, ref: method(:my_bar=).to_proc)
33+
React.create_element(Bar, ref: method(:my_bar=))
3434
end
3535
end
3636

@@ -85,7 +85,7 @@ def self.rec_cnt
8585
after_mount { mutate.unmount true }
8686

8787
render do
88-
Unmountable(ref: method(:ref_rec).to_proc) unless state.unmount
88+
Unmountable(ref: method(:ref_rec).to_proc) unless state.unmount
8989
end
9090
end
9191
end

ruby/hyper-component/spec/test_app/app/views/components.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
if React::IsomorphicHelpers.on_opal_client?
33
require 'browser'
44
require 'browser/delay'
5+
require 'react/ext/opal-jquery/element'
56
end
67
require 'react/server'
78
require 'react/test/utils'

0 commit comments

Comments
 (0)