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

Conversation

nickcoyne
Copy link
Contributor

What are you trying to accomplish?

This PR extends with_collection to allow an optional spacer_component to be specified. This component will be rendered between items in the rendered collection.

What approach did you choose and why?

In use, this is a parallel for Rails' partial rendering with a spacer template using the spacer_template option.

The implementation is similar to that used in Rails' partial rendering, where the elements of the collection are simply joined with the content of the rendered spacer component.

Anything you want to highlight for special attention from reviewers?

I'm not sure that I'm completely following the existing patterns for the updates in collection.rb. Please suggest edits if necessary. Thanks!


def rendered_spacer(view_context)
if @spacer_component
spacer = @spacer_component.new
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of newing ourselves, would it make sense to accept a pre-initialized component? Thoughts being that we can support components that require arguments as a spacer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered that (but did not need it for my current use-case), so went with the simplest option, which also matches Rails spacer template implementation that does not accept any arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BlakeWilliams do you want any other changes here? I feel like accepting components with arguments could be a future enhancement if the need arises.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nickcoyne up until now, ViewComponent has not handled component instantiation (and neither has Rails). I think I'd prefer we keep it that way for consistency's sake.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joelhawksley I've updated this to accept a component instance.

docs/guide/collections.md Outdated Show resolved Hide resolved
docs/guide/collections.md Outdated Show resolved Hide resolved
docs/guide/collections.md Outdated Show resolved Hide resolved
docs/guide/collections.md Outdated Show resolved Hide resolved
lib/view_component/base.rb Outdated Show resolved Hide resolved
@joelhawksley joelhawksley enabled auto-merge (squash) October 31, 2024 16:24
@joelhawksley joelhawksley merged commit 5b07cf1 into ViewComponent:main Oct 31, 2024
19 checks passed
@nickcoyne nickcoyne deleted the collection-spacer branch October 31, 2024 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants