Skip to content

Commit f041a4b

Browse files
authored
Merge branch 'master' into master
2 parents 026eea0 + f58e7cd commit f041a4b

File tree

8 files changed

+52
-0
lines changed

8 files changed

+52
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
*Ciprian Redinciuc*
88

9+
* Subclassed components inherit templates from parent.
10+
11+
*Blake Williams*
12+
913
* Fix uninitialized constant error from `with_collection` when `eager_load` is disabled.
1014

1115
*Josh Gross*

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,18 @@ class InlineVariantComponent < ViewComponent::Base
333333
end
334334
```
335335

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+
336348
### Sidecar Assets
337349

338350
ViewComponents supports two options for defining view files.

lib/view_component/base.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ def short_identifier
183183
end
184184

185185
def inherited(child)
186+
# Compile so child will inherit compiled `call_*` template methods that
187+
# `compile` defines
188+
compile
189+
186190
# If we're in Rails, add application url_helpers to the component context
187191
if defined?(Rails)
188192
child.include Rails.application.routes.url_helpers unless child < Rails.application.routes.url_helpers
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
class InheritedTemplateComponent < MyComponent
4+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>hello, my own template</div>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
class InheritedWithOwnTemplateComponent < MyComponent
4+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
class InheritedInlineComponent < InlineComponent
4+
end

test/view_component/view_component_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,4 +645,23 @@ def test_renders_component_using_rails_config
645645

646646
assert_text("http://assets.example.com")
647647
end
648+
649+
def test_inherited_component_inherits_template
650+
render_inline(InheritedTemplateComponent.new)
651+
652+
assert_selector("div", text: "hello,world!")
653+
end
654+
655+
def test_inherited_component_overrides_inherits_template
656+
render_inline(InheritedWithOwnTemplateComponent.new)
657+
658+
assert_selector("div", text: "hello, my own template")
659+
end
660+
661+
def test_inherited_inline_component_inherits_inline_method
662+
render_inline(InheritedInlineComponent.new)
663+
664+
assert_predicate InheritedInlineComponent, :compiled?
665+
assert_selector("input[type='text'][name='name']")
666+
end
648667
end

0 commit comments

Comments
 (0)