Skip to content

Commit c1791e6

Browse files
committed
Make generator es6 and ts usable at same time
1 parent 910bb99 commit c1791e6

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Changes since the last non-beta release.
99

1010
_Please add entries here for your pull requests that are not yet released._
1111

12+
#### Changed
13+
- Make es6 and ts usable at same time. #1299
14+
1215
## [3.1.0] - 2023-08-15
1316

1417
#### Added

lib/generators/react/component_generator.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,20 @@ def lookup(type = "node", options = "")
256256
def detect_template_extension
257257
if options[:coffee]
258258
"js.jsx.coffee"
259+
elsif options[:ts] && es6_enabled?
260+
"es6.tsx"
259261
elsif options[:ts]
260262
"js.jsx.tsx"
261-
elsif options[:es6] || shakapacker?
263+
elsif es6_enabled?
262264
"es6.jsx"
263265
else
264266
"js.jsx"
265267
end
266268
end
269+
270+
def es6_enabled?
271+
options[:es6] || shakapacker?
272+
end
267273
end
268274
end
269275
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<%= file_header %>
2+
interface I<%= component_name %>Props {
3+
<% if attributes.size > 0 -%>
4+
<% attributes.each do | attribute | -%>
5+
<% if attribute[:union] -%>
6+
<%= attribute[:name].camelize(:lower) %>: <%= attribute[:name].titleize %>;
7+
<% else -%>
8+
<%= attribute[:name].camelize(:lower) %>: <%= attribute[:type] %>;
9+
<% end -%>
10+
<% end -%>
11+
<% end -%>
12+
}
13+
14+
const <%= component_name %> = (props: I<%= component_name %>Props) => {
15+
return (
16+
<React.Fragment>
17+
<% attributes.each do |attribute| -%>
18+
<%= attribute[:name].titleize %>: {props.<%= attribute[:name].camelize(:lower) %>}
19+
<% end -%>
20+
</React.Fragment>
21+
)
22+
}
23+
24+
<%= file_footer %>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
require "generators/react/component_generator"
5+
6+
class TsEs6ComponentGeneratorTest < Rails::Generators::TestCase
7+
destination File.join(Rails.root, "tmp", "component_generator_test_output")
8+
setup :prepare_destination
9+
tests React::Generators::ComponentGenerator
10+
11+
if ShakapackerHelpers.available?
12+
def filename
13+
"app/javascript/components/GeneratedComponent.tsx"
14+
end
15+
else
16+
def filename
17+
"app/assets/javascripts/components/generated_component.es6.tsx"
18+
end
19+
end
20+
21+
def component_name
22+
"GeneratedComponent"
23+
end
24+
25+
test "uses ts and es6 syntax" do
26+
run_generator %w[GeneratedComponent name:string --ts --es6]
27+
28+
assert_file filename, /const #{component_name} = \(props: I#{component_name}Props\) => {/
29+
end
30+
31+
test "defines props type" do
32+
run_generator %w[GeneratedComponent name:string --ts --es6]
33+
34+
assert_file filename, /name: string;/
35+
end
36+
end

0 commit comments

Comments
 (0)