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

Commit 16eaf32

Browse files
committed
React#create_element accept a native component constructor function
1 parent 2d0d1bc commit 16eaf32

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 0.3.0 (Edge)
22
* Set `displayName` of component as the Ruby class name, which make it displayed better in [react-devtools](https://github.com/facebook/react-devtools)
33
* Fix React::Element bridging in React Native environment (#fba2daeb)
4+
* React#create_element accept a native component constructor function
45

56
## 0.2.1
67
* Depends on opal `~> 0.6.0`, which accidentally got loosen in previous release

lib/react/api.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ def self.create_element(type, properties = {}, &block)
66
params = []
77

88
# Component Spec or Nomral DOM
9-
if type.kind_of?(Class)
9+
if `(typeof type === 'function')`
10+
params << type
11+
elsif type.kind_of?(Class)
1012
raise "Provided class should define `render` method" if !(type.method_defined? :render)
1113
params << self.native_component_class(type)
1214
else

spec/react_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
element = React.create_element('div')
2323
expect(React.is_valid_element(element)).to eq(true)
2424
end
25+
26+
it "should allow passed a React.Component class (constructor function)" do
27+
hello_message = `React.createClass({displayName: "HelloMessage",
28+
render: function() {
29+
return React.createElement("div", null, "Hello ", this.props.name);
30+
}
31+
});`
32+
element = React.create_element(hello_message, name: "David")
33+
expect(React.render_to_static_markup(element)).to eq('<div>Hello David</div>')
34+
end
2535

2636
context "with block" do
2737
it "should create a valid element with text as only child when block yield String" do

0 commit comments

Comments
 (0)