Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow assigning a spacer component between items in a rendered collection #2137

Merged
merged 8 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Allow rendering `with_collection` to accept an optional `spacer_component` to be rendered between each item.

*Nick Coyne*

## 3.19.0

* Relax Active Support version constraint in gemspec.
Expand Down
14 changes: 14 additions & 0 deletions docs/guide/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,17 @@ class ProductComponent < ViewComponent::Base
end
end
```

## Spacer Components
joelhawksley marked this conversation as resolved.
Show resolved Hide resolved

Since 3.20.0
{: .label }

You can also specify a component instance to be rendered between instances of the main component by using the :spacer_component option:
joelhawksley marked this conversation as resolved.
Show resolved Hide resolved

```erb
<%= render(ProductComponent.with_collection(@products, spacer_component: SpacerComponent.new)) %>

joelhawksley marked this conversation as resolved.
Show resolved Hide resolved
```

ViewComponent will render the SpacerComponent component between each pair of ProductComponent components.
joelhawksley marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 3 additions & 2 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,10 @@ def sidecar_files(extensions)
# ```
#
# @param collection [Enumerable] A list of items to pass the ViewComponent one at a time.
# @param spacer_component [ViewComponent::Base] A spacer component instance to be rendered.
joelhawksley marked this conversation as resolved.
Show resolved Hide resolved
# @param args [Arguments] Arguments to pass to the ViewComponent every time.
def with_collection(collection, **args)
Collection.new(self, collection, **args)
def with_collection(collection, spacer_component: nil, **args)
Collection.new(self, collection, spacer_component, **args)
end

# @private
Expand Down
14 changes: 12 additions & 2 deletions lib/view_component/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def render_in(view_context, &block)
components.map do |component|
component.set_original_view_context(__vc_original_view_context)
component.render_in(view_context, &block)
end.join.html_safe
end.join(rendered_spacer(view_context)).html_safe
end

def components
Expand Down Expand Up @@ -48,9 +48,10 @@ def format

private

def initialize(component, object, **options)
def initialize(component, object, spacer_component, **options)
@component = component
@collection = collection_variable(object || [])
@spacer_component = spacer_component
@options = options
end

Expand All @@ -69,5 +70,14 @@ def component_options(item, iterator)

@options.merge(item_options)
end

def rendered_spacer(view_context)
if @spacer_component
@spacer_component.set_original_view_context(__vc_original_view_context)
@spacer_component.render_in(view_context)
else
""
end
end
end
end
11 changes: 11 additions & 0 deletions test/sandbox/test/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def call
end
end

class SpacerComponent < ViewComponent::Base
def call
"<hr>".html_safe
end
end

def setup
@products = [OpenStruct.new(name: "Radio clock"), OpenStruct.new(name: "Mints")]
@collection = ProductComponent.with_collection(@products, notice: "secondhand")
Expand All @@ -35,5 +41,10 @@ def test_supports_components_with_keyword_args
assert_selector("*[data-name='#{@products.first.name}']", text: @products.first.name)
assert_selector("*[data-name='#{@products.last.name}']", text: @products.last.name)
end

def test_supports_collection_with_spacer_component
render_inline(ProductComponent.with_collection(@products, spacer_component: SpacerComponent.new))
assert_selector("hr", count: 1)
end
end
end