Skip to content

Commit

Permalink
Inject nested components into parent component initialization in admin
Browse files Browse the repository at this point in the history
By following this pattern, overriding the parent component becomes
easier, as only the initializer is needed.

That also allows to avoid slots and its problem with deeply nested
structures [1].

[1] - ViewComponent/view_component#867
  • Loading branch information
waiting-for-dev committed May 29, 2023
1 parent 5cd42ab commit b790e3a
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions admin/app/components/solidus_admin/main_nav_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
module SolidusAdmin
# Renders the main navigation of Solidus Admin.
class MainNavComponent < BaseComponent
def initialize(main_nav_item_component: component("main_nav_item"), items: container.within_namespace("main_nav"))
@main_nav_item_component = main_nav_item_component
@items = items
end

erb_template <<~ERB
<nav>
<%=
render component("main_nav_item").with_collection(
container.all("main_nav").sort_by(&:position)
render @main_nav_item_component.with_collection(
sorted_items
)
%>
</nav>
ERB

private

def sorted_items
@items.sort_by(&:position)
end
end
end

0 comments on commit b790e3a

Please sign in to comment.