Skip to content

Commit 1a79d40

Browse files
committed
Merge branch 'master' of github.com:reactjs/react-rails
2 parents 917c2c8 + 1fb7012 commit 1a79d40

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ $ rails g react:component HelloWorld greeting:string
8787

8888
Your component is added to `app/javascript/components/` by default.
8989

90+
You can also generate your component in a subdirectory:
91+
92+
```
93+
$ rails g react:component my_subdirectory/HelloWorld greeting:string
94+
```
95+
9096
[Render it in a Rails view](#view-helper):
9197

9298
```erb
@@ -135,7 +141,7 @@ $ bundle exec rails webpacker:install:typescript
135141
$ yarn add @types/react @types/react-dom
136142
```
137143

138-
Doing this will allow React-Rails to support the .tsx extension.
144+
Doing this will allow React-Rails to support the .tsx extension.
139145

140146
## Use with Asset Pipeline
141147

lib/generators/react/component_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def create_component_file
112112
target_dir = 'app/assets/javascripts/components'
113113
end
114114

115-
file_path = File.join(target_dir, "#{new_file_name}.#{extension}")
115+
file_path = File.join(target_dir, class_path, "#{new_file_name}.#{extension}")
116116
template("component.#{template_extension}", file_path)
117117
end
118118

test/generators/component_generator_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ class ComponentGeneratorTest < Rails::Generators::TestCase
1010
def filename
1111
'app/javascript/components/GeneratedComponent.js'
1212
end
13+
def filename_with_subfolder
14+
'app/javascript/components/generated_folder/GeneratedComponent.js'
15+
end
1316
else
1417
def filename
1518
'app/assets/javascripts/components/generated_component.js.jsx'
1619
end
20+
def filename_with_subfolder
21+
'app/assets/javascripts/components/generated_folder/generated_component.js.jsx'
22+
end
1723
end
1824

1925
test 'creates the component file' do
@@ -27,6 +33,17 @@ def filename
2733
end
2834
end
2935

36+
test 'creates the component file in a subdirectory' do
37+
puts WebpackerHelpers.available?
38+
run_generator %w(generated_folder/GeneratedComponent)
39+
assert_file filename_with_subfolder do |contents|
40+
if WebpackerHelpers.available?
41+
assert_match /^import React from "react"/, contents
42+
assert_match /export default GeneratedComponent\n$/m, contents
43+
end
44+
end
45+
end
46+
3047
test 'creates the component file with a node argument' do
3148
run_generator %w(GeneratedComponent name)
3249
assert_file filename, %r{name: PropTypes.node}

0 commit comments

Comments
 (0)