view_component 2.18.2 → 2.19.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of view_component might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +95 -79
- data/README.md +89 -11
- data/lib/view_component/base.rb +4 -0
- data/lib/view_component/collection.rb +2 -0
- data/lib/view_component/version.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 365d7ffb66f4ec6321b0d736bf032d5fe2d9911e1aae9081f23fe09f53d2becc
|
4
|
+
data.tar.gz: c547db15e07b7e69d2422cacdf1d450ceeab79f6b9fdb2efcdd93867da68e5d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e73a2c8950e10c09f01231c8ba40906086f943cbcd670b79e807238047f4250ab9e214c8642ff464909913010a58cbc8dd8d7e006655092f565c34420690247
|
7
|
+
data.tar.gz: efb52c65561bf10cf345ddde5ecbfffec4543d568f32a8c4d61ea1152d8200c9fa0564152012414a48739054f568a4b20d5185431f7a84782152166e84a93a60
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,22 @@
|
|
1
|
-
#
|
1
|
+
# CHANGELOG
|
2
2
|
|
3
|
-
|
3
|
+
## master
|
4
|
+
|
5
|
+
## 2.19.0
|
6
|
+
|
7
|
+
* Extend documentation for using Stimulus within sidecar directories.
|
8
|
+
|
9
|
+
*Ciprian Redinciuc*
|
10
|
+
|
11
|
+
* Subclassed components inherit templates from parent.
|
12
|
+
|
13
|
+
*Blake Williams*
|
14
|
+
|
15
|
+
* Fix uninitialized constant error from `with_collection` when `eager_load` is disabled.
|
16
|
+
|
17
|
+
*Josh Gross*
|
18
|
+
|
19
|
+
## 2.18.2
|
4
20
|
|
5
21
|
* Raise an error if controller or view context is accessed during initialize as they are only available in render.
|
6
22
|
|
@@ -10,13 +26,13 @@
|
|
10
26
|
|
11
27
|
*Joel Hawksley*
|
12
28
|
|
13
|
-
|
29
|
+
## 2.18.1
|
14
30
|
|
15
31
|
* Fix bug where previews didn't work when monkey patch was disabled.
|
16
32
|
|
17
33
|
*Mixer Gutierrez*
|
18
34
|
|
19
|
-
|
35
|
+
## 2.18.0
|
20
36
|
|
21
37
|
* Fix auto-loading of previews (changes no longer require a server restart)
|
22
38
|
|
@@ -30,31 +46,31 @@
|
|
30
46
|
|
31
47
|
*Brad Parker*
|
32
48
|
|
33
|
-
|
49
|
+
## 2.17.1
|
34
50
|
|
35
51
|
* Fix bug where rendering Slot with empty block resulted in error.
|
36
52
|
|
37
53
|
*Joel Hawksley*
|
38
54
|
|
39
|
-
|
55
|
+
## 2.17.0
|
40
56
|
|
41
57
|
* Slots return stripped HTML, removing leading and trailing whitespace.
|
42
58
|
|
43
59
|
*Jason Long, Joel Hawksley*
|
44
60
|
|
45
|
-
|
61
|
+
## 2.16.0
|
46
62
|
|
47
63
|
* Add `--sidecar` option to the erb, haml and slim generators. Places the generated template in the sidecar directory.
|
48
64
|
|
49
65
|
*Michael van Rooijen*
|
50
66
|
|
51
|
-
|
67
|
+
## 2.15.0
|
52
68
|
|
53
69
|
* Add support for templates as ViewComponent::Preview examples.
|
54
70
|
|
55
71
|
*Juan Manuel Ramallo
|
56
72
|
|
57
|
-
|
73
|
+
## 2.14.1
|
58
74
|
|
59
75
|
* Allow using `render_inline` in test when the render monkey patch is disabled with `config.view_component.render_monkey_patch_enabled = false` in versions of Rails < 6.1.
|
60
76
|
|
@@ -64,14 +80,14 @@
|
|
64
80
|
|
65
81
|
Fixes:
|
66
82
|
|
67
|
-
```
|
83
|
+
```console
|
68
84
|
view_component/lib/view_component/slotable.rb:98: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
|
69
85
|
view_component/test/app/components/slots_component.rb:18: warning: The called method `initialize' is defined here
|
70
86
|
```
|
71
87
|
|
72
88
|
*Eileen M. Uchitelle*
|
73
89
|
|
74
|
-
|
90
|
+
## 2.14.0
|
75
91
|
|
76
92
|
* Add `config.preview_paths` to support multiple locations of component preview files. Deprecate `config.preview_path`.
|
77
93
|
|
@@ -81,25 +97,25 @@
|
|
81
97
|
|
82
98
|
*Richard Macklin*
|
83
99
|
|
84
|
-
|
100
|
+
## 2.13.0
|
85
101
|
|
86
102
|
* Add the ability to disable the render monkey patch with `config.view_component.render_monkey_patch_enabled`. In versions of Rails < 6.1, add `render_component` and `render_component_to_string` methods which can be used for rendering components instead of `render`.
|
87
103
|
|
88
104
|
*Johannes Engl*
|
89
105
|
|
90
|
-
|
106
|
+
## 2.12.0
|
91
107
|
|
92
108
|
* Implement Slots as potential successor to Content Areas.
|
93
109
|
|
94
110
|
*Jens Ljungblad, Brian Bugh, Jon Palmer, Joel Hawksley*
|
95
111
|
|
96
|
-
|
112
|
+
## 2.11.1
|
97
113
|
|
98
114
|
* Fix kwarg warnings in Ruby 2.7.
|
99
115
|
|
100
116
|
*Joel Hawksley*
|
101
117
|
|
102
|
-
|
118
|
+
## 2.11.0
|
103
119
|
|
104
120
|
* Ensure Rails configuration is available within components.
|
105
121
|
|
@@ -109,25 +125,25 @@
|
|
109
125
|
|
110
126
|
*Franco Sebregondi*
|
111
127
|
|
112
|
-
|
128
|
+
## 2.10.0
|
113
129
|
|
114
130
|
* Raise an `ArgumentError` with a helpful message when Ruby cannot parse a component class.
|
115
131
|
|
116
132
|
*Max Beizer*
|
117
133
|
|
118
|
-
|
134
|
+
## 2.9.0
|
119
135
|
|
120
136
|
* Cache components per-request in development, preventing unnecessary recompilation during a single request.
|
121
137
|
|
122
138
|
*Felipe Sateler*
|
123
139
|
|
124
|
-
|
140
|
+
## 2.8.0
|
125
141
|
|
126
142
|
* Add `before_render`, deprecating `before_render_check`.
|
127
143
|
|
128
144
|
*Joel Hawksley*
|
129
145
|
|
130
|
-
|
146
|
+
## 2.7.0
|
131
147
|
|
132
148
|
* Add `rendered_component` method to `ViewComponent::TestHelpers` which exposes the raw output of the rendered component.
|
133
149
|
|
@@ -137,7 +153,7 @@
|
|
137
153
|
|
138
154
|
*Jon Palmer*
|
139
155
|
|
140
|
-
|
156
|
+
## 2.6.0
|
141
157
|
|
142
158
|
* Add `config.view_component.preview_route` to set the endpoint for component previews. By default `/rails/view_components` is used.
|
143
159
|
|
@@ -147,13 +163,13 @@
|
|
147
163
|
|
148
164
|
*Joel Hawksley*
|
149
165
|
|
150
|
-
|
166
|
+
## 2.5.1
|
151
167
|
|
152
168
|
* Compile component before rendering collection.
|
153
169
|
|
154
170
|
*Rainer Borene*
|
155
171
|
|
156
|
-
|
172
|
+
## v2.5.0
|
157
173
|
|
158
174
|
* Add counter variables when rendering collections.
|
159
175
|
|
@@ -163,7 +179,7 @@
|
|
163
179
|
|
164
180
|
*Fabio Cantoni*
|
165
181
|
|
166
|
-
|
182
|
+
## v2.4.0
|
167
183
|
|
168
184
|
* Add `#render_to_string` support.
|
169
185
|
|
@@ -181,7 +197,7 @@
|
|
181
197
|
|
182
198
|
*Richard Macklin*
|
183
199
|
|
184
|
-
|
200
|
+
## v2.3.0
|
185
201
|
|
186
202
|
* Allow using inline render method(s) defined on a parent.
|
187
203
|
|
@@ -195,19 +211,19 @@
|
|
195
211
|
|
196
212
|
*Juan Manuel Ramallo*
|
197
213
|
|
198
|
-
|
214
|
+
## v2.2.2
|
199
215
|
|
200
216
|
* Add `Base.format` for better compatibility with `ActionView::Template`.
|
201
217
|
|
202
218
|
*Joel Hawksley*
|
203
219
|
|
204
|
-
|
220
|
+
## v2.2.1
|
205
221
|
|
206
222
|
* Fix bug where template could not be found if `inherited` was redefined.
|
207
223
|
|
208
224
|
*Joel Hawksley*
|
209
225
|
|
210
|
-
|
226
|
+
## v2.2.0
|
211
227
|
|
212
228
|
* Add support for `config.action_view.annotate_template_file_names` (coming in Rails 6.1).
|
213
229
|
|
@@ -217,25 +233,25 @@
|
|
217
233
|
|
218
234
|
*Vasiliy Ermolovich*
|
219
235
|
|
220
|
-
|
236
|
+
## v2.1.0
|
221
237
|
|
222
238
|
* Support rendering collections (e.g., `render(MyComponent.with_collection(@items))`).
|
223
239
|
|
224
240
|
*Tim Clem*
|
225
241
|
|
226
|
-
|
242
|
+
## v2.0.0
|
227
243
|
|
228
244
|
* Move to `ViewComponent` namespace, removing all references to `ActionView`.
|
229
245
|
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
246
|
+
* The gem name is now `view_component`.
|
247
|
+
* ViewComponent previews are now accessed at `/rails/view_components`.
|
248
|
+
* ViewComponents can _only_ be rendered with the instance syntax: `render(MyComponent.new)`. Support for all other syntaxes has been removed.
|
249
|
+
* ActiveModel::Validations have been removed. ViewComponent generators no longer include validations.
|
250
|
+
* In Rails 6.1, no monkey patching is used.
|
251
|
+
* `to_component_class` has been removed.
|
252
|
+
* All gem configuration is now in `config.view_component`.
|
237
253
|
|
238
|
-
|
254
|
+
## v1.17.0
|
239
255
|
|
240
256
|
* Support Ruby 2.4 in CI.
|
241
257
|
|
@@ -249,7 +265,7 @@
|
|
249
265
|
|
250
266
|
*Joel Hawksley*
|
251
267
|
|
252
|
-
|
268
|
+
## v1.16.0
|
253
269
|
|
254
270
|
* Add `refute_component_rendered` test helper.
|
255
271
|
|
@@ -263,7 +279,7 @@
|
|
263
279
|
|
264
280
|
*Rainer Borene*
|
265
281
|
|
266
|
-
|
282
|
+
## v1.15.0
|
267
283
|
|
268
284
|
* Re-introduce ActionView::Component::TestHelpers.
|
269
285
|
|
@@ -287,19 +303,19 @@
|
|
287
303
|
|
288
304
|
*Sean Doyle*
|
289
305
|
|
290
|
-
|
306
|
+
## v1.14.1
|
291
307
|
|
292
308
|
* Fix bug where generator created invalid test code.
|
293
309
|
|
294
310
|
*Joel Hawksley*
|
295
311
|
|
296
|
-
|
312
|
+
## v1.14.0
|
297
313
|
|
298
314
|
* Rename ActionView::Component::Base to ViewComponent::Base
|
299
315
|
|
300
316
|
*Joel Hawksley*
|
301
317
|
|
302
|
-
|
318
|
+
## v1.13.0
|
303
319
|
|
304
320
|
* Allow components to be rendered inside controllers.
|
305
321
|
|
@@ -309,7 +325,7 @@
|
|
309
325
|
|
310
326
|
*Blake Williams*
|
311
327
|
|
312
|
-
|
328
|
+
## v1.12.0
|
313
329
|
|
314
330
|
* Revert: Remove initializer requirement for Ruby 2.7+
|
315
331
|
|
@@ -323,13 +339,13 @@
|
|
323
339
|
|
324
340
|
*Joel Hawksley*
|
325
341
|
|
326
|
-
|
342
|
+
## v1.11.1
|
327
343
|
|
328
344
|
* Relax Capybara requirement.
|
329
345
|
|
330
346
|
*Joel Hawksley*
|
331
347
|
|
332
|
-
|
348
|
+
## v1.11.0
|
333
349
|
|
334
350
|
* Add support for Capybara matchers.
|
335
351
|
|
@@ -339,25 +355,25 @@
|
|
339
355
|
|
340
356
|
*Asger Behncke Jacobsen*
|
341
357
|
|
342
|
-
|
358
|
+
## v1.10.0
|
343
359
|
|
344
360
|
* Deprecate all `render` syntaxes except for `render(MyComponent.new(foo: :bar))`
|
345
361
|
|
346
362
|
*Joel Hawksley*
|
347
363
|
|
348
|
-
|
364
|
+
## v1.9.0
|
349
365
|
|
350
366
|
* Remove initializer requirement for Ruby 2.7+
|
351
367
|
|
352
368
|
*Dylan Clark*
|
353
369
|
|
354
|
-
|
370
|
+
## v1.8.1
|
355
371
|
|
356
372
|
* Run validation checks before calling `#render?`.
|
357
373
|
|
358
374
|
*Ash Wilson*
|
359
375
|
|
360
|
-
|
376
|
+
## v1.8.0
|
361
377
|
|
362
378
|
* Remove the unneeded ComponentExamplesController and simplify preview rendering.
|
363
379
|
|
@@ -383,7 +399,7 @@
|
|
383
399
|
|
384
400
|
*Sergey Malykh*
|
385
401
|
|
386
|
-
|
402
|
+
## v1.7.0
|
387
403
|
|
388
404
|
* Simplify validation of templates and compilation.
|
389
405
|
|
@@ -393,7 +409,7 @@
|
|
393
409
|
|
394
410
|
*Jon Palmer*
|
395
411
|
|
396
|
-
|
412
|
+
## v1.6.2
|
397
413
|
|
398
414
|
* Fix Uninitialized Constant error.
|
399
415
|
|
@@ -407,7 +423,7 @@
|
|
407
423
|
|
408
424
|
*Justin Coyne*
|
409
425
|
|
410
|
-
|
426
|
+
## v1.6.1
|
411
427
|
|
412
428
|
* Allow Previews to have no layout.
|
413
429
|
|
@@ -433,7 +449,7 @@
|
|
433
449
|
|
434
450
|
*Ryan Workman*
|
435
451
|
|
436
|
-
|
452
|
+
## v1.6.0
|
437
453
|
|
438
454
|
* Avoid dropping elements in the render_inline test helper.
|
439
455
|
|
@@ -463,7 +479,7 @@
|
|
463
479
|
|
464
480
|
*Vinicius Stock*
|
465
481
|
|
466
|
-
|
482
|
+
## v1.5.3
|
467
483
|
|
468
484
|
* Add support for RSpec to generators.
|
469
485
|
|
@@ -473,19 +489,19 @@
|
|
473
489
|
|
474
490
|
*Joel Hawksley*
|
475
491
|
|
476
|
-
|
492
|
+
## v1.5.2
|
477
493
|
|
478
494
|
* Disable eager loading initializer.
|
479
495
|
|
480
496
|
*Kasper Meyer*
|
481
497
|
|
482
|
-
|
498
|
+
## v1.5.1
|
483
499
|
|
484
500
|
* Update railties class to work with Rails 6.
|
485
501
|
|
486
502
|
*Juan Manuel Ramallo*
|
487
503
|
|
488
|
-
|
504
|
+
## v1.5.0
|
489
505
|
|
490
506
|
Note: `actionview-component` is now loaded by requiring `actionview/component`, not `actionview/component/base`.
|
491
507
|
|
@@ -505,7 +521,7 @@ Note: `actionview-component` is now loaded by requiring `actionview/component`,
|
|
505
521
|
|
506
522
|
*Juan Manuel Ramallo*
|
507
523
|
|
508
|
-
|
524
|
+
## v1.4.0
|
509
525
|
|
510
526
|
* Fix bug where components broke in application paths with periods.
|
511
527
|
|
@@ -527,7 +543,7 @@ Note: `actionview-component` is now loaded by requiring `actionview/component`,
|
|
527
543
|
|
528
544
|
*Elia Schito*
|
529
545
|
|
530
|
-
|
546
|
+
## v1.3.6
|
531
547
|
|
532
548
|
* Allow template file names without format.
|
533
549
|
|
@@ -537,7 +553,7 @@ Note: `actionview-component` is now loaded by requiring `actionview/component`,
|
|
537
553
|
|
538
554
|
*Juan Manuel Ramallo*
|
539
555
|
|
540
|
-
|
556
|
+
## v1.3.5
|
541
557
|
|
542
558
|
* Re-expose `controller` method.
|
543
559
|
|
@@ -551,7 +567,7 @@ Note: `actionview-component` is now loaded by requiring `actionview/component`,
|
|
551
567
|
|
552
568
|
*ars moriendi*
|
553
569
|
|
554
|
-
|
570
|
+
## v1.3.4
|
555
571
|
|
556
572
|
* Template errors surface correct file and line number.
|
557
573
|
|
@@ -561,57 +577,57 @@ Note: `actionview-component` is now loaded by requiring `actionview/component`,
|
|
561
577
|
|
562
578
|
*Joel Hawksley*
|
563
579
|
|
564
|
-
|
580
|
+
## v1.3.3
|
565
581
|
|
566
|
-
*
|
582
|
+
* Do not raise error when sidecar files that are not templates exist.
|
567
583
|
|
568
584
|
*Joel Hawksley*
|
569
585
|
|
570
|
-
|
586
|
+
## v1.3.2
|
571
587
|
|
572
|
-
*
|
588
|
+
* Support rendering views from inside component templates.
|
573
589
|
|
574
590
|
*Patrick Sinclair*
|
575
591
|
|
576
|
-
|
592
|
+
## v1.3.1
|
577
593
|
|
578
|
-
*
|
594
|
+
* Fix bug where rendering nested content caused an error.
|
579
595
|
|
580
596
|
*Joel Hawksley, Aaron Patterson*
|
581
597
|
|
582
|
-
|
598
|
+
## v1.3.0
|
583
599
|
|
584
|
-
*
|
600
|
+
* Components are rendered with enough controller context to support rendering of partials and forms.
|
585
601
|
|
586
602
|
*Patrick Sinclair, Joel Hawksley, Aaron Patterson*
|
587
603
|
|
588
|
-
|
604
|
+
## v1.2.1
|
589
605
|
|
590
|
-
*
|
606
|
+
* `actionview-component` is now tested against Ruby 2.3/2.4 and Rails 5.0.0.
|
591
607
|
|
592
|
-
|
608
|
+
## v1.2.0
|
593
609
|
|
594
|
-
*
|
610
|
+
* The `render_component` test helper has been renamed to `render_inline`. `render_component` has been deprecated and will be removed in v2.0.0.
|
595
611
|
|
596
612
|
*Joel Hawksley*
|
597
613
|
|
598
|
-
*
|
614
|
+
* Components are now rendered with `render MyComponent, foo: :bar` syntax. The existing `render MyComponent.new(foo: :bar)` syntax has been deprecated and will be removed in v2.0.0.
|
599
615
|
|
600
616
|
*Joel Hawksley*
|
601
617
|
|
602
|
-
|
618
|
+
## v1.1.0
|
603
619
|
|
604
|
-
*
|
620
|
+
* Components now inherit from ActionView::Component::Base
|
605
621
|
|
606
622
|
*Joel Hawksley*
|
607
623
|
|
608
|
-
|
624
|
+
## v1.0.1
|
609
625
|
|
610
|
-
*
|
626
|
+
* Always recompile component templates outside production.
|
611
627
|
|
612
628
|
*Joel Hawksley, John Hawthorn*
|
613
629
|
|
614
|
-
|
630
|
+
## v1.0.0
|
615
631
|
|
616
632
|
This release extracts the `ActionView::Component` library from the GitHub application.
|
617
633
|
|
data/README.md
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
# ViewComponent
|
2
|
+
|
2
3
|
ViewComponent is a framework for building view components that are reusable, testable & encapsulated, in Ruby on Rails.
|
3
4
|
|
4
5
|
## Design philosophy
|
6
|
+
|
5
7
|
ViewComponent is designed to integrate as seamlessly as possible [with Rails](https://rubyonrails.org/doctrine/), with the [least surprise](https://www.artima.com/intv/ruby4.html).
|
6
8
|
|
7
9
|
## Compatibility
|
10
|
+
|
8
11
|
ViewComponent is [supported natively](https://edgeguides.rubyonrails.org/layouts_and_rendering.html#rendering-objects) in Rails 6.1, and compatible with Rails 5.0+ via an included [monkey patch](https://github.com/github/view_component/blob/master/lib/view_component/render_monkey_patch.rb).
|
9
12
|
|
10
13
|
ViewComponent is tested for compatibility [with combinations of](https://github.com/github/view_component/blob/22e3d4ccce70d8f32c7375e5a5ccc3f70b22a703/.github/workflows/ruby_on_rails.yml#L10-L11) Ruby 2.4+ and Rails 5+.
|
@@ -67,6 +70,8 @@ Component names end in -`Component`.
|
|
67
70
|
|
68
71
|
Component module names are plural, as for controllers and jobs: `Users::AvatarComponent`
|
69
72
|
|
73
|
+
Name components for what they render, not what they accept. (`AvatarComponent` instead of `UserComponent`)
|
74
|
+
|
70
75
|
#### Quick start
|
71
76
|
|
72
77
|
Use the component generator to create a new ViewComponent.
|
@@ -94,6 +99,7 @@ bin/rails generate component Example title content --template-engine slim
|
|
94
99
|
A ViewComponent is a Ruby file and corresponding template file with the same base name:
|
95
100
|
|
96
101
|
`app/components/test_component.rb`:
|
102
|
+
|
97
103
|
```ruby
|
98
104
|
class TestComponent < ViewComponent::Base
|
99
105
|
def initialize(title:)
|
@@ -103,6 +109,7 @@ end
|
|
103
109
|
```
|
104
110
|
|
105
111
|
`app/components/test_component.html.erb`:
|
112
|
+
|
106
113
|
```erb
|
107
114
|
<span title="<%= @title %>"><%= content %></span>
|
108
115
|
```
|
@@ -128,6 +135,7 @@ Content passed to a ViewComponent as a block is captured and assigned to the `co
|
|
128
135
|
ViewComponents can declare additional content areas. For example:
|
129
136
|
|
130
137
|
`app/components/modal_component.rb`:
|
138
|
+
|
131
139
|
```ruby
|
132
140
|
class ModalComponent < ViewComponent::Base
|
133
141
|
with_content_areas :header, :body
|
@@ -135,6 +143,7 @@ end
|
|
135
143
|
```
|
136
144
|
|
137
145
|
`app/components/modal_component.html.erb`:
|
146
|
+
|
138
147
|
```erb
|
139
148
|
<div class="modal">
|
140
149
|
<div class="header"><%= header %></div>
|
@@ -174,7 +183,7 @@ By default, slots can be rendered once per component. They provide an accessor w
|
|
174
183
|
|
175
184
|
Slots declared with `collection: true` can be rendered multiple times. They provide an accessor with the pluralized name of the slot (`#rows`), which is an Array of `ViewComponent::Slot` instances.
|
176
185
|
|
177
|
-
To learn more about the design of the Slots API, see https://github.com/github/view_component/pull/348 and https://github.com/github/view_component/discussions/325.
|
186
|
+
To learn more about the design of the Slots API, see [#348](https://github.com/github/view_component/pull/348) and [#325](https://github.com/github/view_component/discussions/325).
|
178
187
|
|
179
188
|
##### Defining Slots
|
180
189
|
|
@@ -195,6 +204,7 @@ _Note: Slot classes must be subclasses of `ViewComponent::Slot`._
|
|
195
204
|
##### Example ViewComponent with Slots
|
196
205
|
|
197
206
|
`# box_component.rb`
|
207
|
+
|
198
208
|
```ruby
|
199
209
|
class BoxComponent < ViewComponent::Base
|
200
210
|
include ViewComponent::Slotable
|
@@ -239,6 +249,7 @@ end
|
|
239
249
|
```
|
240
250
|
|
241
251
|
`# box_component.html.erb`
|
252
|
+
|
242
253
|
```erb
|
243
254
|
<div class="Box">
|
244
255
|
<% if header %>
|
@@ -269,6 +280,7 @@ end
|
|
269
280
|
```
|
270
281
|
|
271
282
|
`# index.html.erb`
|
283
|
+
|
272
284
|
```erb
|
273
285
|
<%= render(BoxComponent.new) do |component| %>
|
274
286
|
<% component.slot(:header, classes: "my-class-name") do %>
|
@@ -294,6 +306,7 @@ end
|
|
294
306
|
ViewComponents can render without a template file, by defining a `call` method:
|
295
307
|
|
296
308
|
`app/components/inline_component.rb`:
|
309
|
+
|
297
310
|
```ruby
|
298
311
|
class InlineComponent < ViewComponent::Base
|
299
312
|
def call
|
@@ -320,6 +333,18 @@ class InlineVariantComponent < ViewComponent::Base
|
|
320
333
|
end
|
321
334
|
```
|
322
335
|
|
336
|
+
### Template Inheritance
|
337
|
+
|
338
|
+
Components that subclass another component inherit the parent component's
|
339
|
+
template if they don't define their own template.
|
340
|
+
|
341
|
+
```ruby
|
342
|
+
# If `my_link_component.html.erb` is not defined the component will fall back
|
343
|
+
# to `LinkComponent`s template
|
344
|
+
class MyLinkComponent < LinkComponent
|
345
|
+
end
|
346
|
+
```
|
347
|
+
|
323
348
|
### Sidecar Assets
|
324
349
|
|
325
350
|
ViewComponents supports two options for defining view files.
|
@@ -328,7 +353,7 @@ ViewComponents supports two options for defining view files.
|
|
328
353
|
|
329
354
|
The simplest option is to place the view next to the Ruby component:
|
330
355
|
|
331
|
-
```
|
356
|
+
```console
|
332
357
|
app/components
|
333
358
|
├── ...
|
334
359
|
├── test_component.rb
|
@@ -340,7 +365,7 @@ app/components
|
|
340
365
|
|
341
366
|
As an alternative, views and other assets can be placed in a sidecar directory with the same name as the component, which can be useful for organizing views alongside other assets like Javascript and CSS.
|
342
367
|
|
343
|
-
```
|
368
|
+
```console
|
344
369
|
app/components
|
345
370
|
├── ...
|
346
371
|
├── example_component.rb
|
@@ -349,12 +374,11 @@ app/components
|
|
349
374
|
| ├── example_component.html.erb
|
350
375
|
| └── example_component.js
|
351
376
|
├── ...
|
352
|
-
|
353
377
|
```
|
354
378
|
|
355
379
|
To generate a component with a sidecar directory, use the `--sidecar` flag:
|
356
380
|
|
357
|
-
```
|
381
|
+
```console
|
358
382
|
bin/rails generate component Example title content --sidecar
|
359
383
|
invoke test_unit
|
360
384
|
create test/components/example_component_test.rb
|
@@ -368,7 +392,7 @@ It's also possible to place the Ruby component file inside the sidecar directory
|
|
368
392
|
|
369
393
|
_Note: Avoid giving your containing folder the same name as your `.rb` file or there will be a conflict between Module and Class definitions_
|
370
394
|
|
371
|
-
```
|
395
|
+
```console
|
372
396
|
app/components
|
373
397
|
├── ...
|
374
398
|
├── example
|
@@ -395,7 +419,8 @@ Components can implement a `#render?` method to be called after initialization t
|
|
395
419
|
Traditionally, the logic for whether to render a view could go in either the component template:
|
396
420
|
|
397
421
|
`app/components/confirm_email_component.html.erb`
|
398
|
-
|
422
|
+
|
423
|
+
```erb
|
399
424
|
<% if user.requires_confirmation? %>
|
400
425
|
<div class="alert">Please confirm your email address.</div>
|
401
426
|
<% end %>
|
@@ -404,6 +429,7 @@ Traditionally, the logic for whether to render a view could go in either the com
|
|
404
429
|
or the view that renders the component:
|
405
430
|
|
406
431
|
`app/views/_banners.html.erb`
|
432
|
+
|
407
433
|
```erb
|
408
434
|
<% if current_user.requires_confirmation? %>
|
409
435
|
<%= render(ConfirmEmailComponent.new(user: current_user)) %>
|
@@ -413,6 +439,7 @@ or the view that renders the component:
|
|
413
439
|
Using the `#render?` hook simplifies the view:
|
414
440
|
|
415
441
|
`app/components/confirm_email_component.rb`
|
442
|
+
|
416
443
|
```ruby
|
417
444
|
class ConfirmEmailComponent < ViewComponent::Base
|
418
445
|
def initialize(user:)
|
@@ -426,13 +453,15 @@ end
|
|
426
453
|
```
|
427
454
|
|
428
455
|
`app/components/confirm_email_component.html.erb`
|
429
|
-
|
456
|
+
|
457
|
+
```erb
|
430
458
|
<div class="banner">
|
431
459
|
Please confirm your email address.
|
432
460
|
</div>
|
433
461
|
```
|
434
462
|
|
435
463
|
`app/views/_banners.html.erb`
|
464
|
+
|
436
465
|
```erb
|
437
466
|
<%= render(ConfirmEmailComponent.new(user: current_user)) %>
|
438
467
|
```
|
@@ -444,6 +473,7 @@ _To assert that a component has not been rendered, use `refute_component_rendere
|
|
444
473
|
Components can define a `before_render` method to be called before a component is rendered, when `helpers` is able to be used:
|
445
474
|
|
446
475
|
`app/components/confirm_email_component.rb`
|
476
|
+
|
447
477
|
```ruby
|
448
478
|
class MyComponent < ViewComponent::Base
|
449
479
|
def before_render
|
@@ -457,11 +487,13 @@ end
|
|
457
487
|
Use `with_collection` to render a ViewComponent with a collection:
|
458
488
|
|
459
489
|
`app/view/products/index.html.erb`
|
490
|
+
|
460
491
|
``` erb
|
461
492
|
<%= render(ProductComponent.with_collection(@products)) %>
|
462
493
|
```
|
463
494
|
|
464
495
|
`app/components/product_component.rb`
|
496
|
+
|
465
497
|
``` ruby
|
466
498
|
class ProductComponent < ViewComponent::Base
|
467
499
|
def initialize(product:)
|
@@ -477,6 +509,7 @@ end
|
|
477
509
|
Use `with_collection_parameter` to change the name of the collection parameter:
|
478
510
|
|
479
511
|
`app/components/product_component.rb`
|
512
|
+
|
480
513
|
``` ruby
|
481
514
|
class ProductComponent < ViewComponent::Base
|
482
515
|
with_collection_parameter :item
|
@@ -492,11 +525,13 @@ end
|
|
492
525
|
Additional arguments besides the collection are passed to each component instance:
|
493
526
|
|
494
527
|
`app/view/products/index.html.erb`
|
528
|
+
|
495
529
|
``` erb
|
496
530
|
<%= render(ProductComponent.with_collection(@products, notice: "hi")) %>
|
497
531
|
```
|
498
532
|
|
499
533
|
`app/components/product_component.rb`
|
534
|
+
|
500
535
|
``` ruby
|
501
536
|
class ProductComponent < ViewComponent::Base
|
502
537
|
with_collection_parameter :item
|
@@ -509,6 +544,7 @@ end
|
|
509
544
|
```
|
510
545
|
|
511
546
|
`app/components/product_component.html.erb`
|
547
|
+
|
512
548
|
``` erb
|
513
549
|
<li>
|
514
550
|
<h2><%= @item.name %></h2>
|
@@ -521,6 +557,7 @@ end
|
|
521
557
|
ViewComponent defines a counter variable matching the parameter name above, followed by `_counter`. To access the variable, add it to `initialize` as an argument:
|
522
558
|
|
523
559
|
`app/components/product_component.rb`
|
560
|
+
|
524
561
|
``` ruby
|
525
562
|
class ProductComponent < ViewComponent::Base
|
526
563
|
def initialize(product:, product_counter:)
|
@@ -531,6 +568,7 @@ end
|
|
531
568
|
```
|
532
569
|
|
533
570
|
`app/components/product_component.html.erb`
|
571
|
+
|
534
572
|
``` erb
|
535
573
|
<li>
|
536
574
|
<%= @counter %> <%= @product.name %>
|
@@ -579,7 +617,7 @@ class UserComponent < ViewComponent::Base
|
|
579
617
|
end
|
580
618
|
```
|
581
619
|
|
582
|
-
###
|
620
|
+
### Writing tests
|
583
621
|
|
584
622
|
Unit test components directly, using the `render_inline` test helper, asserting against the rendered output.
|
585
623
|
|
@@ -647,9 +685,11 @@ end
|
|
647
685
|
```
|
648
686
|
|
649
687
|
### Previewing Components
|
688
|
+
|
650
689
|
`ViewComponent::Preview`, like `ActionMailer::Preview`, provides a way to preview components in isolation:
|
651
690
|
|
652
691
|
`test/components/previews/test_component_preview.rb`
|
692
|
+
|
653
693
|
```ruby
|
654
694
|
class TestComponentPreview < ViewComponent::Preview
|
655
695
|
def with_default_title
|
@@ -677,6 +717,7 @@ and <http://localhost:3000/rails/view_components/test_component/with_content_blo
|
|
677
717
|
It's also possible to set dynamic values from the params by setting them as arguments:
|
678
718
|
|
679
719
|
`test/components/previews/test_component_preview.rb`
|
720
|
+
|
680
721
|
```ruby
|
681
722
|
class TestComponentPreview < ViewComponent::Preview
|
682
723
|
def with_dynamic_title(title: "Test component default")
|
@@ -694,6 +735,7 @@ and [`content_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/TagHe
|
|
694
735
|
Previews use the application layout by default, but can use a specific layout with the `layout` option:
|
695
736
|
|
696
737
|
`test/components/previews/test_component_preview.rb`
|
738
|
+
|
697
739
|
```ruby
|
698
740
|
class TestComponentPreview < ViewComponent::Preview
|
699
741
|
layout "admin"
|
@@ -705,6 +747,7 @@ end
|
|
705
747
|
You can also set a custom layout to be used by default for previews as well as the preview index pages via the `default_preview_layout` configuration option:
|
706
748
|
|
707
749
|
`config/application.rb`
|
750
|
+
|
708
751
|
```ruby
|
709
752
|
# Set the default layout to app/views/layouts/component_preview.html.erb
|
710
753
|
config.view_component.default_preview_layout = "component_preview"
|
@@ -713,6 +756,7 @@ config.view_component.default_preview_layout = "component_preview"
|
|
713
756
|
Preview classes live in `test/components/previews`, which can be configured using the `preview_paths` option:
|
714
757
|
|
715
758
|
`config/application.rb`
|
759
|
+
|
716
760
|
```ruby
|
717
761
|
config.view_component.preview_paths << "#{Rails.root}/lib/component_previews"
|
718
762
|
```
|
@@ -720,6 +764,7 @@ config.view_component.preview_paths << "#{Rails.root}/lib/component_previews"
|
|
720
764
|
Previews are served from <http://localhost:3000/rails/view_components> by default. To use a different endpoint, set the `preview_route` option:
|
721
765
|
|
722
766
|
`config/application.rb`
|
767
|
+
|
723
768
|
```ruby
|
724
769
|
config.view_component.preview_route = "/previews"
|
725
770
|
```
|
@@ -731,6 +776,7 @@ This example will make the previews available from <http://localhost:3000/previe
|
|
731
776
|
Given a preview `test/components/previews/cell_component_preview.rb`, template files can be defined at `test/components/previews/cell_component_preview/`:
|
732
777
|
|
733
778
|
`test/components/previews/cell_component_preview.rb`
|
779
|
+
|
734
780
|
```ruby
|
735
781
|
class CellComponentPreview < ViewComponent::Preview
|
736
782
|
def default
|
@@ -739,6 +785,7 @@ end
|
|
739
785
|
```
|
740
786
|
|
741
787
|
`test/components/previews/cell_component_preview/default.html.erb`
|
788
|
+
|
742
789
|
```erb
|
743
790
|
<table class="table">
|
744
791
|
<tbody>
|
@@ -753,6 +800,7 @@ To use a different location for preview templates, pass the `template` argument:
|
|
753
800
|
(the path should be relative to `config.view_component.preview_path`):
|
754
801
|
|
755
802
|
`test/components/previews/cell_component_preview.rb`
|
803
|
+
|
756
804
|
```ruby
|
757
805
|
class CellComponentPreview < ViewComponent::Preview
|
758
806
|
def default
|
@@ -764,6 +812,7 @@ end
|
|
764
812
|
Values from `params` can be accessed through `locals`:
|
765
813
|
|
766
814
|
`test/components/previews/cell_component_preview.rb`
|
815
|
+
|
767
816
|
```ruby
|
768
817
|
class CellComponentPreview < ViewComponent::Preview
|
769
818
|
def default(title: "Default title", subtitle: "A subtitle")
|
@@ -782,6 +831,7 @@ Which enables passing in a value with <http://localhost:3000/rails/components/ce
|
|
782
831
|
Component tests and previews assume the existence of an `ApplicationController` class, which be can be configured using the `test_controller` option:
|
783
832
|
|
784
833
|
`config/application.rb`
|
834
|
+
|
785
835
|
```ruby
|
786
836
|
config.view_component.test_controller = "BaseController"
|
787
837
|
```
|
@@ -791,6 +841,7 @@ config.view_component.test_controller = "BaseController"
|
|
791
841
|
To use RSpec, add the following:
|
792
842
|
|
793
843
|
`spec/rails_helper.rb`
|
844
|
+
|
794
845
|
```ruby
|
795
846
|
require "view_component/test_helpers"
|
796
847
|
|
@@ -804,6 +855,7 @@ Specs created by the generator have access to test helpers like `render_inline`.
|
|
804
855
|
To use component previews:
|
805
856
|
|
806
857
|
`config/application.rb`
|
858
|
+
|
807
859
|
```ruby
|
808
860
|
config.view_component.preview_paths << "#{Rails.root}/spec/components/previews"
|
809
861
|
```
|
@@ -816,7 +868,7 @@ In order to [avoid conflicts](https://github.com/github/view_component/issues/28
|
|
816
868
|
|
817
869
|
With the monkey patch disabled, use `render_component` (or `render_component_to_string`) instead:
|
818
870
|
|
819
|
-
```
|
871
|
+
```erb
|
820
872
|
<%= render_component Component.new(message: "bar") %>
|
821
873
|
```
|
822
874
|
|
@@ -854,6 +906,7 @@ One approach is to use Web Components, which contain all Javascript functionalit
|
|
854
906
|
For example:
|
855
907
|
|
856
908
|
`app/components/comment_component.rb`
|
909
|
+
|
857
910
|
```ruby
|
858
911
|
class CommentComponent < ViewComponent::Base
|
859
912
|
def initialize(comment:)
|
@@ -883,6 +936,7 @@ end
|
|
883
936
|
```
|
884
937
|
|
885
938
|
`app/components/comment_component.html.erb`
|
939
|
+
|
886
940
|
```erb
|
887
941
|
<my-comment comment-id="<%= comment.id %>">
|
888
942
|
<time slot="posted" datetime="<%= comment.created_at.iso8601 %>"><%= comment.created_at.strftime("%b %-d") %></time>
|
@@ -896,6 +950,7 @@ end
|
|
896
950
|
```
|
897
951
|
|
898
952
|
`app/components/comment_component.js`
|
953
|
+
|
899
954
|
```js
|
900
955
|
class Comment extends HTMLElement {
|
901
956
|
styles() {
|
@@ -949,6 +1004,29 @@ application.load(
|
|
949
1004
|
|
950
1005
|
This enables the creation of files such as `app/components/widget_controller.js`, where the controller identifier matches the `data-controller` attribute in the component's HTML template.
|
951
1006
|
|
1007
|
+
When placing a Stimulus controller inside a sidecar directory, be aware that when referencing the controller [each forward slash in a namespaced controller file’s path becomes two dashes in its identifier](
|
1008
|
+
https://stimulusjs.org/handbook/installing#controller-filenames-map-to-identifiers):
|
1009
|
+
|
1010
|
+
```console
|
1011
|
+
app/components
|
1012
|
+
├── ...
|
1013
|
+
├── example
|
1014
|
+
| ├── component.rb
|
1015
|
+
| ├── component.css
|
1016
|
+
| ├── component.html.erb
|
1017
|
+
| └── component_controller.js
|
1018
|
+
├── ...
|
1019
|
+
```
|
1020
|
+
|
1021
|
+
`component_controller.js`'s Stimulus identifier becomes: `example--component`:
|
1022
|
+
|
1023
|
+
```erb
|
1024
|
+
<div data-controller="example--component">
|
1025
|
+
<input type="text">
|
1026
|
+
<button data-action="click->example--component#greet">Greet</button>
|
1027
|
+
</div>
|
1028
|
+
```
|
1029
|
+
|
952
1030
|
## Frequently Asked Questions
|
953
1031
|
|
954
1032
|
### Can I use other templating languages besides ERB?
|
@@ -980,7 +1058,7 @@ ViewComponent is far from a novel idea! Popular implementations of view componen
|
|
980
1058
|
|
981
1059
|
## Contributing
|
982
1060
|
|
983
|
-
|
1061
|
+
This project is intended to be a safe, welcoming space for collaboration. Contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. We recommend reading the [contributing guide](./CONTRIBUTING.md) as well.
|
984
1062
|
|
985
1063
|
## Contributors
|
986
1064
|
|
data/lib/view_component/base.rb
CHANGED
@@ -183,6 +183,10 @@ module ViewComponent
|
|
183
183
|
end
|
184
184
|
|
185
185
|
def inherited(child)
|
186
|
+
# Compile so child will inherit compiled `call_*` template methods that
|
187
|
+
# `compile` defines
|
188
|
+
compile
|
189
|
+
|
186
190
|
# If we're in Rails, add application url_helpers to the component context
|
187
191
|
if defined?(Rails)
|
188
192
|
child.include Rails.application.routes.url_helpers unless child < Rails.application.routes.url_helpers
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: view_component
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.19.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GitHub Open Source
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-09-
|
11
|
+
date: 2020-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|