Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] React v0.13 #17

Open
wants to merge 47 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
314309b
Upgrade react-source to v0.13
zetachang Mar 31, 2015
997c3a2
Update props validation spec
zetachang Mar 31, 2015
c23ae25
Extract Component API to separate file
zetachang Mar 31, 2015
0e7b7bf
Directly "mixin" React.Component class
zetachang Mar 31, 2015
9cad22d
Props is immutable now
zetachang Mar 31, 2015
82ee8ae
Upgrade propTypes & defaultProps
zetachang Mar 31, 2015
d11772a
Temporary fix test case rely on getDOMNode()
zetachang Mar 31, 2015
22769f8
set_state! ( replaceState() ) is deprecated
zetachang Mar 31, 2015
b7add1a
Deprecate the bridged native element usage
zetachang Mar 31, 2015
704cdee
mounted? ( isMounted() ) is deprecated
zetachang Mar 31, 2015
f43cdc7
Expect Component property exposed as Hash
zetachang Mar 31, 2015
d017107
Fix spec
zetachang Mar 31, 2015
072df76
`props` & `state` will alwasy return a Hash internal & external
zetachang Mar 31, 2015
bcee9ae
React::Element is a toll-free bridge now
zetachang Apr 1, 2015
e111ecc
Use React::Element directly without to_n
zetachang Apr 1, 2015
7147b5a
Rename React::API to React::ComponentFactory
zetachang Apr 1, 2015
cf2cd8b
Merge branch 'master' into react-v0.13
zetachang Apr 3, 2015
edb132e
Remove duplicated test
zetachang Apr 3, 2015
f570a46
Native wrapper is no longer required
zetachang Apr 3, 2015
e7fac7e
getDOMNode() is deprecated
zetachang Apr 3, 2015
3c9894d
Fix the bridging constructor
zetachang Apr 3, 2015
304d7f9
Remove needless access to ReactElement prototype
zetachang Apr 3, 2015
9fc21f2
Add find_dom_node to top level API
zetachang Apr 3, 2015
2d295ad
Clean up testing
zetachang Apr 3, 2015
c4208fd
Update changelog
zetachang Apr 3, 2015
aa3fd9a
native_alias all the life cycle methods
zetachang May 3, 2015
8916501
Deprecate React::Component::API#dom_node
zetachang May 3, 2015
fe108bf
Move Object.assign shim to React::ComponentFactory
zetachang May 3, 2015
90d9a82
Merge branch 'master' into react-v0.13
zetachang May 3, 2015
6a30839
Fix syntax error
zetachang May 3, 2015
cafa213
Fix test
zetachang May 3, 2015
75896a8
Move Opal Ruby code to opal folder
zetachang May 10, 2015
7e26ed6
Make react-source as runtime dependency
zetachang May 10, 2015
f4302a6
camelize all props passed in React#create_element
zetachang May 10, 2015
2b593eb
Use React.cloneElement to implement event subscription #18
zetachang May 10, 2015
f08b8f8
Props will be passed as the first argument of initializer
zetachang May 10, 2015
688fcfa
Clarify integration with react-source
zetachang May 10, 2015
3f80fea
low hanging fruit/opal 0.8 fixes
wied03 May 12, 2015
e6d38ba
Make these tests more robust (fix the console object back in case the…
wied03 May 14, 2015
78eea54
Adapt to new Opal 0.8 hidden methods
wied03 May 14, 2015
aad1557
Adapt to the fact that native_alias fails if some of the methods aren…
wied03 May 14, 2015
1a93972
Update opal / sprockets / opal-rspec
zetachang Jun 10, 2015
5d359b7
Fix displayName not set properly regression
zetachang Jun 14, 2015
3f9560b
React.render now return the original component instance
zetachang Jun 14, 2015
d29b79f
React::Component::API#refs return a shallow Ruby hash now
zetachang Jun 14, 2015
01406a2
Upgrade to opal '0.8.0.rc1'
zetachang Jun 14, 2015
759b76a
[WIP] Try to fix the "on" method
zetachang Oct 11, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## react.js v0.13
* Add `React::Testing` helpers

## 0.3.0
* Depends on `sprockets < 3` thus source map can still work
* Remove sprockets-es6 from dependency
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
source 'https://rubygems.org'
gemspec
gem 'opal', '0.8.0.rc1'
gem 'opal-rspec', github: 'opal/opal-rspec'
gemspec
43 changes: 23 additions & 20 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,63 +1,66 @@
GIT
remote: git://github.com/opal/opal-rspec.git
revision: e2290d56ff5147e5a2b96258ef5bdc2b54a9e6af
specs:
opal-rspec (0.4.2)
opal (>= 0.7.0, < 0.9)

PATH
remote: .
specs:
react.rb (0.3.0)
opal (~> 0.6.0)
opal (>= 0.7.0, < 0.9)
opal-activesupport (~> 0)
react-jsx (~> 0.8.0)
sprockets (>= 2.2.3, < 3.0.0)
react-source (~> 0.13)
sprockets (~> 3.1)
therubyracer (~> 0)

GEM
remote: https://rubygems.org/
specs:
execjs (2.5.2)
hike (1.2.3)
json (1.8.2)
json (1.8.3)
libv8 (3.16.14.7)
multi_json (1.11.0)
opal (0.6.3)
source_map
sprockets
opal (0.8.0.rc1)
hike (~> 1.2)
sourcemap (~> 0.1.0)
sprockets (~> 3.1)
tilt (>= 1.4)
opal-activesupport (0.1.0)
opal (>= 0.5.0, < 1.0.0)
opal-jquery (0.2.0)
opal (>= 0.5.0, < 1.0.0)
opal-rspec (0.3.0.beta3)
opal (>= 0.6.0, < 1.0.0)
rack (1.6.0)
rack (1.6.1)
rack-protection (1.5.3)
rack
rake (10.4.2)
react-jsx (0.8.0)
execjs (>= 2.0.2)
json (>= 1.8.0)
react-source (>= 0.4.1)
react-source (0.12.2)
react-source (0.13.3)
ref (1.0.5)
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
source_map (3.0.1)
json
sprockets (2.12.3)
hike (~> 1.2)
multi_json (~> 1.0)
sourcemap (0.1.1)
sprockets (3.2.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
therubyracer (0.12.2)
libv8 (~> 3.16.14.0)
ref
tilt (1.4.1)
tilt (2.0.1)

PLATFORMS
ruby

DEPENDENCIES
opal (= 0.8.0.rc1)
opal-jquery (~> 0)
opal-rspec (~> 0.3.0.beta3)
opal-rspec!
rake (~> 10)
react-source (~> 0.12.0)
react.rb!
sinatra (~> 1)
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ React.render(React.create_element('h1'){ "Hello World!" }, `document.body`)

For integration with server (Sinatra, etc), see setup of [TodoMVC](example/todos) or the [official docs](http://opalrb.org/docs/) of Opal.

## React.js Dependency

React.js v0.13 is required to use react.rb, you can access the pre-bundled source through `Opal::React.bundled_path` directory, example below shows setup for a basic rack app.

```ruby
#config.ru
run Opal::Server.new {|s|
s.append_path './'
s.append_path Opal::React.bundled_path
s.main = 'example'
s.debug = true
s.index_path = "index.html.erb"
}
```

```erb
<!-- index.html.erb -->
<%= javascript_include_tag "react" %>
<%= javascript_include_tag "example" %>
```

## Usage

### A Simple Component
Expand All @@ -46,25 +67,26 @@ puts React.render_to_static_markup(React.create_element(HelloMessage))

### More complicated one

To hook into native ReactComponent life cycle, the native `this` will be passed to the class's initializer. And all corresponding life cycle methods (`componentDidMount`, etc) will be invoked on the instance using the snake-case method name.
To hook into native ReactComponent life cycle, the props will be passed as the first argument to the class's initializer. And all corresponding life cycle methods (`componentDidMount`, etc) will be invoked on the instance using the snake-case method name.

```ruby
class HelloMessage
def initialize(native)
@native = Native(native)
def initialize(props)
puts props
end

def component_will_mount
puts "will mount!"
end

def render
React.create_element("div") { "Hello #{@native[:props][:name]}!" }
React.create_element("div") { "Hello #{self.props[:name]}!" }
end
end

puts React.render_to_static_markup(React.create_element(HelloMessage, name: 'John'))

# => {"name"=>"John"}
# => will_mount!
# => '<div>Hello John!</div>'
```
Expand Down
5 changes: 2 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ Bundler.require
Bundler::GemHelper.install_tasks

require 'opal/rspec/rake_task'
require "react/source"

Opal::RSpec::RakeTask.new(:default) do |s|
s.append_path File.dirname(::React::Source.bundled_path_for("react-with-addons"))
s.append_path Opal::React.bundled_path
s.append_path 'spec/vendor'
s.index_path = 'spec/reactjs/index.html.erb'
end
end
3 changes: 1 addition & 2 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ require 'bundler'
Bundler.require

require "opal-rspec"
require "react/source"

Opal.append_path File.expand_path('../spec', __FILE__)

run Opal::Server.new { |s|
s.main = 'opal/rspec/sprockets_runner'
s.append_path 'spec'
s.append_path 'spec/vendor'
s.append_path File.dirname(::React::Source.bundled_path_for("react-with-addons.js"))
s.append_path Opal::React.bundled_path
s.debug = true
s.index_path = 'spec/reactjs/index.html.erb'
}
3 changes: 2 additions & 1 deletion examples/todos/app/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

Vienna::Router.new.tap do |router|
router.route('/:filter') do |params|
component.set_props(filter: params[:filter].empty? ? "all" : params[:filter])
element = React.create_element(TodoAppView, filter: params[:filter].empty? ? "all" : params[:filter])
component = React.render(element, `document.getElementById('todoapp')`)
end
end.update

Expand Down
1 change: 1 addition & 0 deletions examples/todos/app/components/app.react.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class TodoAppView
Todo.on(:create) { Todo.adapter.sync_models(Todo); reload_current_filter }
Todo.on(:update) { Todo.adapter.sync_models(Todo); reload_current_filter }
Todo.on(:destroy) { Todo.adapter.sync_models(Todo); reload_current_filter }
reload_current_filter
end

before_receive_props do |next_props|
Expand Down
8 changes: 8 additions & 0 deletions lib/opal/react.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'opal'
require "opal-activesupport"

require "opal/react/jsx_support"
require "opal/react/source"

Opal.append_path File.expand_path('../../../opal', __FILE__).untaint
Opal.append_path File.expand_path('../../../vendor', __FILE__).untaint
22 changes: 22 additions & 0 deletions lib/opal/react/jsx_support.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'react/jsx'
require 'tilt'
require "sprockets"

module Opal
module React
module JSX
class Template < Tilt::Template
self.default_mime_type = 'application/javascript'

def prepare
end

def evaluate(scope, locals, &block)
@output ||= ::React::JSX.compile(data)
end
end
end
end
end

Sprockets.register_engine '.jsx', Opal::React::JSX::Template
9 changes: 9 additions & 0 deletions lib/opal/react/source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "react/source"

module Opal
module React
def self.bundled_path
File.dirname(::React::Source.bundled_path_for("react-with-addons.js"))
end
end
end
5 changes: 5 additions & 0 deletions lib/opal/react/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Opal
module React
VERSION = "0.3.0"
end
end
36 changes: 18 additions & 18 deletions lib/react.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
if RUBY_ENGINE == 'opal'
require "react/top_level"
require "react/component"
require "react/element"
require "react/event"
require "react/version"
require "react/api"
require "react/validator"
else
require "tilt"
require "opal"
require "react/version"
require "opal-activesupport"
require "react/ext/jsx_support"

Opal.append_path File.expand_path('../', __FILE__).untaint
Opal.append_path File.expand_path('../../vendor', __FILE__).untaint
end
# if RUBY_ENGINE == 'opal'
# require "react/top_level"
# require "react/component"
# require "react/element"
# require "react/event"
# require "react/version"
# require "react/component_factory"
# require "react/validator"
# else
# require "tilt"
# require "opal"
# require "react/version"
# require "opal-activesupport"
# require "react/ext/jsx_support"
require "opal/react"
# Opal.append_path File.expand_path('../', __FILE__).untaint
# Opal.append_path File.expand_path('../../vendor', __FILE__).untaint
#end
Loading