Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 4f5ea84

Browse files
committed
Fix #162
1 parent 1172096 commit 4f5ea84

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ Whitespace conventions:
1818
- 1 spaces before normal text
1919
-->
2020

21-
## [0.9.0]
21+
## [0.9.1] - Unreleased
22+
23+
### Fixed
24+
25+
- ReactJS functional stateless component could not be imported from `NativeLibrary`. Note that functional component is only supported in React v14+. (#162)
26+
27+
## [0.9.0] - 2016-10-19
2228

2329
### Added
2430

lib/react/api.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ def self.import_native_component(opal_class, native_class)
2323
def self.eval_native_react_component(name)
2424
component = `eval(name)`
2525
raise "#{name} is not defined" if `#{component} === undefined`
26-
unless `#{component}.prototype !== undefined` &&
27-
(`!!#{component}.prototype.isReactComponent` || `!!#{component}.prototype.render`)
26+
is_component_class = `#{component}.prototype !== undefined` &&
27+
(`!!#{component}.prototype.isReactComponent` ||
28+
`!!#{component}.prototype.render`)
29+
is_functional_component = `typeof #{component} === "function"`
30+
is_not_using_react_v13 = `!window.React.version.match(/0\.13/)`
31+
unless is_component_class || (is_not_using_react_v13 && is_functional_component)
2832
raise 'does not appear to be a native react component'
2933
end
3034
component

spec/react/native_library_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ class NestedComponent < React::Component::Base
3131
Object.send :remove_const, :NativeComponent
3232
end
3333

34+
describe "functional stateless component (supported in reactjs v14+ only)" do
35+
it "is not detected as native React.js component by `native_react_component?`", v13_only: true do
36+
expect(React::API.native_react_component?(`function C(){ return null }`)).to be_falsy
37+
end
38+
39+
it "is detected as native React.js component by `native_react_component?`", v13_exclude: true do
40+
expect(React::API.native_react_component?(`function C(){ return null }`)).to be_truthy
41+
end
42+
43+
it "imports a React.js functional stateless component", v13_exclude: true do
44+
%x{
45+
window.NativeLibrary = {
46+
FunctionalComponent: function HelloMessage(props){
47+
return React.createElement("div", null, "Hello ", props.name);
48+
}
49+
}
50+
}
51+
stub_const 'Foo', Class.new(React::Component::Base)
52+
Foo.class_eval do
53+
imports "NativeLibrary.FunctionalComponent"
54+
end
55+
expect(React.render_to_static_markup(
56+
React.create_element(Foo, name: "There"))).to eq('<div>Hello There</div>')
57+
end
58+
end
59+
3460
it "can use native_react_component? to detect a native React.js component" do
3561
%x{
3662
window.NativeComponent = React.createClass({

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def self.should_immediately_generate(opts={}, &block)
7474
config.filter_run_excluding :ruby
7575
if `(React.version.search(/^0\.13/) === -1)`
7676
config.filter_run_excluding :v13_only
77+
else
78+
config.filter_run_excluding :v13_exclude
7779
end
7880
end
7981
end

0 commit comments

Comments
 (0)