Skip to content

Commit 123182d

Browse files
committed
Add option for generating functional component
1 parent 8c491af commit 123182d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/generators/react/component_generator.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
6565
default: false,
6666
desc: 'Output coffeescript based component'
6767

68+
class_option :function,
69+
type: :boolean,
70+
default: false,
71+
aliases: '--func',
72+
desc: 'Output function based component'
73+
6874
REACT_PROP_TYPES = {
6975
'node' => 'PropTypes.node',
7076
'bool' => 'PropTypes.bool',
@@ -126,6 +132,8 @@ def create_component_file
126132
'js.jsx.coffee'
127133
elsif options[:ts]
128134
'js.jsx.tsx'
135+
elsif options[:function]
136+
'func.jsx'
129137
elsif options[:es6] || webpacker?
130138
'es6.jsx'
131139
else
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<%= file_header %>
2+
function <%= component_name %>() {
3+
return (
4+
<React.Fragment>
5+
<% attributes.each do |attribute| -%>
6+
<%= attribute[:name].titleize %>: {this.props.<%= attribute[:name].camelize(:lower) %>}
7+
<% end -%>
8+
</React.Fragment>
9+
);
10+
}
11+
12+
<% if attributes.size > 0 -%>
13+
<%= file_name.camelize %>.propTypes = {
14+
<% attributes.each_with_index do |attribute, idx| -%>
15+
<%= attribute[:name].camelize(:lower) %>: <%= attribute[:type] %><% if (idx < attributes.length-1) %>,<% end %>
16+
<% end -%>
17+
};
18+
<% end -%>
19+
20+
<%= file_footer %>

0 commit comments

Comments
 (0)