diff --git a/admin/app/components/solidus_admin/sidebar/component.html.erb b/admin/app/components/solidus_admin/sidebar/component.html.erb index 00c34bca45c..fd7cc4603ab 100644 --- a/admin/app/components/solidus_admin/sidebar/component.html.erb +++ b/admin/app/components/solidus_admin/sidebar/component.html.erb @@ -27,7 +27,7 @@ <% end %> diff --git a/admin/app/components/solidus_admin/sidebar/item/component.rb b/admin/app/components/solidus_admin/sidebar/item/component.rb index 045c4d501a5..5d4ce1c9865 100644 --- a/admin/app/components/solidus_admin/sidebar/item/component.rb +++ b/admin/app/components/solidus_admin/sidebar/item/component.rb @@ -4,12 +4,17 @@ class SolidusAdmin::Sidebar::Item::Component < SolidusAdmin::BaseComponent with_collection_parameter :item + # @param item [SolidusAdmin::MainNavItem + # @param fullpath [String] the current path + # @param url_helpers [#solidus_admin, #spree] context for generating paths def initialize( item:, + fullpath: "#", url_helpers: Struct.new(:spree, :solidus_admin).new(spree, solidus_admin) ) @item = item @url_helpers = url_helpers + @fullpath = fullpath end def name @@ -59,11 +64,11 @@ def nested_items tag.nav( class: nested_nav_active_classes ) do - render self.class.with_collection(@item.children, url_helpers: @url_helpers) + render self.class.with_collection(@item.children, url_helpers: @url_helpers, fullpath: @fullpath) end end def active? - @item.active?(@url_helpers, request.fullpath) + @item.active?(@url_helpers, @fullpath) end end diff --git a/admin/spec/components/previews/solidus_admin/sidebar/component_preview.rb b/admin/spec/components/previews/solidus_admin/sidebar/component_preview.rb index 4949fdefcf6..5c766c60781 100644 --- a/admin/spec/components/previews/solidus_admin/sidebar/component_preview.rb +++ b/admin/spec/components/previews/solidus_admin/sidebar/component_preview.rb @@ -16,6 +16,20 @@ def active? end end + # The item component is used to render main navigation items, which are + # rendered within the sidebar. + # + # It needs to be passed a {SolidusAdmin::MainNavItem} instance, which + # represents the data for a main navigation item. + # + # ```ruby + # item = SolidusAdmin::MainNavItem.new( + # key: :overview, + # position: 80 + # ) + # render component("sidebar/item", item: item) + # ``` + # # @param store_name text # @param store_url url # @param logo_path text { description: "Asset path to the store logo" } diff --git a/admin/spec/components/previews/solidus_admin/sidebar/item/component_preview.rb b/admin/spec/components/previews/solidus_admin/sidebar/item/component_preview.rb new file mode 100644 index 00000000000..cb90bdb0ea2 --- /dev/null +++ b/admin/spec/components/previews/solidus_admin/sidebar/item/component_preview.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require "solidus_admin/main_nav_item" + +# @component "sidebar/item" +class SolidusAdmin::Sidebar::Item::ComponentPreview < ViewComponent::Preview + include SolidusAdmin::Preview + + DUMMY_ROUTE = :foo_path + + DUMMY_PATH = "#" + + # @param active toggle { description: "Whether the item is active" } + # @param key text { description: "ID also used for i18n" } + # @param icon text { description: "RemixIcon name (https://remixicon.com/)" } + def overview(active: false, key: "orders", icon: "inbox-line") + item = SolidusAdmin::MainNavItem.new( + key: key, + icon: icon, + position: 1, + route: DUMMY_ROUTE + ) + + render_with_template( + locals: { + item: item, + url_helpers: url_helpers, + fullpath: fullpath(active) + } + ) + end + + private + + # solidus_admin.foo_path => "#" + def url_helpers + Struct.new(:solidus_admin).new( + Struct.new(DUMMY_ROUTE).new(DUMMY_PATH) + ) + end + + def fullpath(active) + active ? DUMMY_PATH : "" + end +end diff --git a/admin/spec/components/previews/solidus_admin/sidebar/item/component_preview/overview.html.erb b/admin/spec/components/previews/solidus_admin/sidebar/item/component_preview/overview.html.erb new file mode 100644 index 00000000000..d8df49071d0 --- /dev/null +++ b/admin/spec/components/previews/solidus_admin/sidebar/item/component_preview/overview.html.erb @@ -0,0 +1,7 @@ +