Skip to content

Commit 51f0b15

Browse files
Add TypeScript support (#2117)
* Add TypeScript support * Small fixes * Trailling whitespace * Markdown fix * Apply suggestions from code review Thanks @joelhawksley Co-authored-by: Joel Hawksley <[email protected]> --------- Co-authored-by: Joel Hawksley <[email protected]>
1 parent c7eca74 commit 51f0b15

File tree

9 files changed

+55
-5
lines changed

9 files changed

+55
-5
lines changed

docs/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ nav_order: 5
1010

1111
## main
1212

13+
* Fix bug where stimulus controller was not added to ERB when stimulus was activated by default.
14+
15+
*Denis Pasin*
16+
17+
* Add typescript support to stimulus generator.
18+
19+
*Denis Pasin*
20+
1321
## 3.16.0
1422

1523
* Add template information to multiple template error messages.

docs/guide/generators.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ bin/rails generate component Example title --stimulus
110110

111111
To always generate a Stimulus controller, set `config.view_component.generate.stimulus_controller = true`.
112112

113+
To generate a TypeScript controller instead of a JavaScript controller, either:
114+
115+
- Pass the `--typescript` option
116+
- Set `config.view_component.generate.typescript = true`
117+
113118
### Generate [locale files](/guide/translations.html)
114119

115120
Since 2.47.0

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ ViewComponent is built by over a hundred members of the community, including:
228228
<img src="https://avatars.githubusercontent.com/allan-pires?s=64" alt="allan-pires" width="32" />
229229
<img src="https://avatars.githubusercontent.com/jasonkim?s=64" alt="jasonkim" width="32" />
230230
<img src="https://avatars.githubusercontent.com/tkowalewski" alt="tkowalewski" width="32" />
231+
<img src="https://avatars.githubusercontent.com/zaratan" alt="zaratan" width="32" />
231232

232233
## Who uses ViewComponent?
233234

lib/rails/generators/abstract_generator.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def component_path
3333
end
3434

3535
def stimulus_controller
36-
if options["stimulus"]
36+
if stimulus?
3737
File.join(destination_directory, destination_file_name)
3838
.sub("#{component_path}/", "")
3939
.tr("_", "-")
@@ -44,5 +44,13 @@ def stimulus_controller
4444
def sidecar?
4545
options["sidecar"] || ViewComponent::Base.config.generate.sidecar
4646
end
47+
48+
def stimulus?
49+
options["stimulus"] || ViewComponent::Base.config.generate.stimulus_controller
50+
end
51+
52+
def typescript?
53+
options["typescript"] || ViewComponent::Base.config.generate.typescript
54+
end
4755
end
4856
end

lib/rails/generators/erb/component_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def copy_view_file
2424
private
2525

2626
def data_attributes
27-
if options["stimulus"]
27+
if stimulus?
2828
" data-controller=\"#{stimulus_controller}\""
2929
end
3030
end

lib/rails/generators/stimulus/component_generator.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
99

1010
source_root File.expand_path("templates", __dir__)
1111
class_option :sidecar, type: :boolean, default: false
12+
class_option :typescript, type: :boolean, default: false
1213

1314
def create_stimulus_controller
14-
template "component_controller.js", destination
15+
template "component_controller.#{filetype}", destination
1516
end
1617

1718
def stimulus_module
@@ -22,11 +23,15 @@ def stimulus_module
2223

2324
private
2425

26+
def filetype
27+
typescript? ? "ts" : "js"
28+
end
29+
2530
def destination
2631
if sidecar?
27-
File.join(component_path, class_path, "#{file_name}_component", "#{file_name}_component_controller.js")
32+
File.join(component_path, class_path, "#{file_name}_component", "#{file_name}_component_controller.#{filetype}")
2833
else
29-
File.join(component_path, class_path, "#{file_name}_component_controller.js")
34+
File.join(component_path, class_path, "#{file_name}_component_controller.#{filetype}")
3035
end
3136
end
3237

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Controller } from "<%= stimulus_module %>";
2+
3+
export default class extends Controller {
4+
declare element: HTMLElement;
5+
6+
connect() {
7+
console.log("Hello, Stimulus!", this.element);
8+
}
9+
}

lib/view_component/base.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,14 @@ def compiler
412412
# config.view_component.generate.stimulus_controller = true
413413
# ```
414414
#
415+
# #### `#typescript`
416+
#
417+
# Generate TypeScript files instead of JavaScript files:
418+
#
419+
# ```ruby
420+
# config.view_component.generate.typescript = true
421+
# ```
422+
#
415423
# #### #locale
416424
#
417425
# Always generate translations file alongside the component:

lib/view_component/config.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ def defaults
4848
#
4949
# config.view_component.generate.stimulus_controller = true
5050
#
51+
# #### `#typescript`
52+
#
53+
# Generate TypeScript files instead of JavaScript files:
54+
#
55+
# config.view_component.generate.typescript = true
56+
#
5157
# #### `#locale`
5258
#
5359
# Always generate translations file alongside the component:

0 commit comments

Comments
 (0)