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

Commit 59c8344

Browse files
committed
adds es6 option to generator
1 parent d433c0d commit 59c8344

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-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 -%>

0 commit comments

Comments
 (0)