Skip to content

Commit 8178716

Browse files
author
Robert Mosolgo
committed
Merge pull request #332 from borisrorsvort/feature/add_es6_option_to_generator
Add es6 option to generator
2 parents 90fece2 + 4ad2117 commit 8178716

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ end
204204
### Component generator
205205

206206
`react-rails` ships with a Rails generator to help you get started with a simple component scaffold.
207-
You can run it using `rails generate react:component ComponentName`.
207+
You can run it using `rails generate react:component ComponentName (--es6)`.
208208
The generator takes an optional list of arguments for default propTypes,
209209
which follow the conventions set in the [Reusable Components](http://facebook.github.io/react/docs/reusable-components.html)
210210
section of the React documentation.
@@ -239,6 +239,18 @@ var Post = React.createClass({
239239
});
240240
```
241241

242+
#### Options
243+
244+
**--es6** : Generate the same component but using cutting edge es6 class
245+
246+
For example:
247+
248+
```shell
249+
rails generate react:component Label label:string --es6
250+
```
251+
252+
#### Arguments
253+
242254
The generator can use the following arguments to create basic propTypes:
243255

244256
* any

lib/generators/react/component_generator.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
5050
:default => [],
5151
:banner => "field[:type] field[:type] ..."
5252

53+
class_option :es6,
54+
type: :boolean,
55+
default: false,
56+
desc: 'Output es6 class based component'
57+
5358
REACT_PROP_TYPES = {
5459
"node" => 'React.PropTypes.node',
5560
"bool" => 'React.PropTypes.bool',
@@ -80,7 +85,7 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
8085
}
8186

8287
def create_component_file
83-
extension = "js.jsx"
88+
extension = options[:es6] ? "es6.jsx" : "js.jsx"
8489
file_path = File.join('app/assets/javascripts/components', "#{file_name}.#{extension}")
8590
template("component.#{extension}", file_path)
8691
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class <%= file_name.camelize %> extends React.Component {
2+
render () {
3+
<% if attributes.size > 0 -%>
4+
return (
5+
<div>
6+
<% attributes.each do |attribute| -%>
7+
<div><%= attribute[:name].titleize %>: {this.props.<%= attribute[:name].camelize(:lower) %>}</div>
8+
<% end -%>
9+
</div>
10+
);
11+
<% else -%>
12+
return <div />;
13+
<% end -%>
14+
}
15+
}
16+
17+
<% if attributes.size > 0 -%>
18+
<%= file_name.camelize %>.propTypes = {
19+
<% attributes.each_with_index do |attribute, idx| -%>
20+
<%= attribute[:name].camelize(:lower) %>: <%= attribute[:type] %><% if (idx < attributes.length-1) %>,<% end %>
21+
<% end -%>
22+
};
23+
<% end -%>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'test_helper'
2+
require 'generators/react/component_generator'
3+
4+
class Es6ComponentGeneratorTest < Rails::Generators::TestCase
5+
destination File.join(Rails.root, 'tmp', 'component_generator_test_output')
6+
setup :prepare_destination
7+
tests React::Generators::ComponentGenerator
8+
9+
def filename
10+
'app/assets/javascripts/components/generated_component.es6.jsx'
11+
end
12+
13+
def class_name
14+
'GeneratedComponent'
15+
end
16+
17+
test "uses es6 syntax" do
18+
run_generator %w(GeneratedComponent name --es6)
19+
20+
assert_file filename, /^class\s#{class_name}\sextends\sReact\.Component/
21+
end
22+
23+
test "assigns defaultProps after class definintion" do
24+
run_generator %w(GeneratedComponent name --es6)
25+
26+
assert_file filename, /\s^#{class_name}\.propTypes/
27+
end
28+
29+
test "generates working jsx" do
30+
expected_name_div = /React\.createElement\(\s*"div",\s*null,\s*\"Name:\s*\",\s*this\.props\.name\s*\)/x
31+
expected_shape_div = /React\.createElement\(\s*"div",\s*null,\s*\"Address:\s*\",\s*this\.props\.address\s*\)/x
32+
33+
run_generator %w(GeneratedComponent name:string address:shape --es6)
34+
jsx = React::JSX.transform(File.read(File.join(destination_root, filename)))
35+
36+
assert_match(Regexp.new(expected_name_div), jsx)
37+
assert_match(Regexp.new(expected_shape_div), jsx)
38+
end
39+
end

0 commit comments

Comments
 (0)