class Primer::Beta::NavList::Item
themselves contain sub items. Sub items are rendered collapsed by default.
‘selected_item_ids` argument, which accepts a list of valid IDs for the item. Items can also
such as icons, avatars, and counters. Items are selected by ID. IDs can be specified via the
Items are rendered as styled links. They can optionally include leading and/or trailing visuals,
def active?
def active? item_active?(self) && items.empty? end
def active_sub_item?
def active_sub_item? items.any? { |subitem| item_active?(subitem) } end
def before_render
def before_render if active_sub_item? expand! @content_arguments[:classes] = class_names( @content_arguments[:classes], "ActionListContent--hasActiveSubItem" ) else @system_arguments[:classes] = class_names( @system_arguments[:classes], "ActionListItem--navActive" => active? ) end @content_arguments[:"aria-current"] = "page" if active? super raise "Cannot render a trailing action for an item with subitems" if items.present? && trailing_action.present? raise "Cannot pass `selected_by_ids:` for an item with subitems, since parent items cannot be selected" if items.present? && @selected_by_ids.present? return if items.blank? @sub_list_arguments[:aria] = merge_aria( @sub_list_arguments, { aria: { labelledby: id } } ) raise ArgumentError, "Items with sub-items cannot have hrefs" if href.present? @content_arguments[:tag] = :button @content_arguments[:"aria-expanded"] = @expanded.to_s @content_arguments[:"data-action"] = " click:#{@list.custom_element_name}#handleItemWithSubItemClick keydown:#{@list.custom_element_name}#handleItemWithSubItemKeydown " with_private_trailing_action_icon(:"chevron-down", classes: "ActionListItem-collapseIcon") @system_arguments[:classes] = class_names( @system_arguments[:classes], "ActionListItem--hasSubItem" ) end
def current_page?(url)
def current_page?(url) helpers.current_page?(url) end
def expand!
def expand! @expanded = true end
def initialize(selected_item_id: nil, selected_by_ids: [], sub_item: false, expanded: false, **system_arguments)
-
system_arguments
(Hash
) -- <%= link_to_system_arguments_docs %> -
sub_item
(Boolean
) -- Whether or not this item is nested under a parent item. Used internally. -
expanded
(Boolean
) -- Whether this item shows (expands) or hides (collapses) its list of sub items. -
selected_by_ids
(Array
) -- The list of IDs that select this item. In other words, if the `selected_item_id` attribute on the parent `NavList` is set to one of these IDs, the item will appear selected. -
selected_item_id
(Symbol
) -- The ID of the currently selected list item. Used internally.
def initialize(selected_item_id: nil, selected_by_ids: [], sub_item: false, expanded: false, **system_arguments) @selected_item_id = selected_item_id @selected_by_ids = Array(selected_by_ids) @expanded = expanded @sub_item = sub_item system_arguments[:classes] = class_names( system_arguments[:classes], "ActionListItem--subItem" => @sub_item ) @sub_list_arguments = { classes: class_names( "ActionList", "ActionList--subGroup" ) } @list = system_arguments[:list] @sub_list_arguments["data-action"] = "keydown:#{@list.custom_element_name}#handleItemWithSubItemKeydown" if @list overrides = { "data-item-id": @selected_by_ids.join(" ") } super(**system_arguments, **overrides) end
def item_active?(item)
the parent and bypass the problem entirely. Maybe not the most OO approach,
be set before `before_render` is invoked, we can call helper methods here in
ActionView::Base). Since we know the view context for the parent object must
`helpers` is simply an alias for the view context (i.e. an instance of
This means helper methods like current_page? will blow up with an error, since
item until _after_ the parent's before_render, etc methods have been called.
items.any?(&:active?), but unfortunately the view context is not set on each
Normally it would be easier to simply ask each item for its active status, eg.
def item_active?(item) if item.selected_by_ids.present? item.selected_by_ids.include?(@selected_item_id) elsif item.href current_page?(item.href) else # :nocov: false # :nocov: end end
def kind
def kind :item end
def list_class
def list_class Primer::Beta::NavList end