Skip to content

Commit

Permalink
React#create_element accept a native component constructor function
Browse files Browse the repository at this point in the history
  • Loading branch information
zetachang committed Apr 12, 2015
1 parent 2d0d1bc commit 16eaf32
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.3.0 (Edge)
* Set `displayName` of component as the Ruby class name, which make it displayed better in [react-devtools](https://github.com/facebook/react-devtools)
* Fix React::Element bridging in React Native environment (#fba2daeb)
* React#create_element accept a native component constructor function

## 0.2.1
* Depends on opal `~> 0.6.0`, which accidentally got loosen in previous release
Expand Down
4 changes: 3 additions & 1 deletion lib/react/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ def self.create_element(type, properties = {}, &block)
params = []

# Component Spec or Nomral DOM
if type.kind_of?(Class)
if `(typeof type === 'function')`
params << type
elsif type.kind_of?(Class)
raise "Provided class should define `render` method" if !(type.method_defined? :render)
params << self.native_component_class(type)
else
Expand Down
10 changes: 10 additions & 0 deletions spec/react_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
element = React.create_element('div')
expect(React.is_valid_element(element)).to eq(true)
end

it "should allow passed a React.Component class (constructor function)" do
hello_message = `React.createClass({displayName: "HelloMessage",
render: function() {
return React.createElement("div", null, "Hello ", this.props.name);
}
});`
element = React.create_element(hello_message, name: "David")
expect(React.render_to_static_markup(element)).to eq('<div>Hello David</div>')
end

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

0 comments on commit 16eaf32

Please sign in to comment.