Class: Primer::Beta::NavList::Item
- Inherits:
-
Alpha::ActionList::Item
- Object
- ViewComponent::Base
- Component
- Alpha::ActionList::Item
- Primer::Beta::NavList::Item
- Defined in:
- app/components/primer/beta/nav_list/item.rb
Overview
Items are rendered as styled links. They can optionally include leading and/or trailing visuals, such as icons, avatars, and counters. Items are selected by ID. IDs can be specified via the ‘selected_item_ids` argument, which accepts a list of valid IDs for the item. Items can also themselves contain sub items. Sub items are rendered collapsed by default.
Direct Known Subclasses
Constant Summary
Constants inherited from Alpha::ActionList::Item
Alpha::ActionList::Item::DEFAULT_DESCRIPTION_SCHEME, Alpha::ActionList::Item::DEFAULT_SCHEME, Alpha::ActionList::Item::DEFAULT_SIZE, Alpha::ActionList::Item::DEFAULT_TRUNCATION_BEHAVIOR, Alpha::ActionList::Item::DESCRIPTION_SCHEME_MAPPINGS, Alpha::ActionList::Item::DESCRIPTION_SCHEME_OPTIONS, Alpha::ActionList::Item::SCHEME_MAPPINGS, Alpha::ActionList::Item::SCHEME_OPTIONS, Alpha::ActionList::Item::SIZE_MAPPINGS, Alpha::ActionList::Item::SIZE_OPTIONS, Alpha::ActionList::Item::TRUNCATION_BEHAVIOR_MAPPINGS, Alpha::ActionList::Item::TRUNCATION_BEHAVIOR_OPTIONS
Constants inherited from Component
Component::INVALID_ARIA_LABEL_TAGS
Constants included from Status::Dsl
Constants included from ViewHelper
Constants included from TestSelectorHelper
TestSelectorHelper::TEST_SELECTOR_TAG
Constants included from FetchOrFallbackHelper
FetchOrFallbackHelper::InvalidValueError
Constants included from AttributesHelper
AttributesHelper::PLURAL_ARIA_ATTRIBUTES, AttributesHelper::PLURAL_DATA_ATTRIBUTES
Instance Attribute Summary collapse
-
#selected_by_ids ⇒ Object
readonly
Returns the value of attribute selected_by_ids.
-
#sub_item ⇒ Object
(also: #sub_item?)
readonly
Returns the value of attribute sub_item.
Attributes inherited from Alpha::ActionList::Item
#active, #disabled, #href, #id, #item_id, #list, #parent
Instance Method Summary collapse
- #active? ⇒ Boolean
- #before_render ⇒ Object
-
#expand! ⇒ Object
Cause this item to show its list of sub items when rendered.
-
#initialize(selected_item_id: nil, selected_by_ids: [], sub_item: false, expanded: false, **system_arguments) ⇒ Item
constructor
A new instance of Item.
- #kind ⇒ Object
Methods inherited from Component
Methods included from JoinStyleArgumentsHelper
Methods included from TestSelectorHelper
Methods included from FetchOrFallbackHelper
#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?
Methods included from ClassNameHelper
Methods included from AttributesHelper
#aria, #data, #extract_data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes
Methods included from ExperimentalSlotHelpers
Methods included from ExperimentalRenderHelpers
Constructor Details
#initialize(selected_item_id: nil, selected_by_ids: [], sub_item: false, expanded: false, **system_arguments) ⇒ Item
Returns a new instance of Item.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/components/primer/beta/nav_list/item.rb', line 32 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 = @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 |
Instance Attribute Details
#selected_by_ids ⇒ Object (readonly)
Returns the value of attribute selected_by_ids.
11 12 13 |
# File 'app/components/primer/beta/nav_list/item.rb', line 11 def selected_by_ids @selected_by_ids end |
#sub_item ⇒ Object (readonly) Also known as: sub_item?
Returns the value of attribute sub_item.
11 12 13 |
# File 'app/components/primer/beta/nav_list/item.rb', line 11 def sub_item @sub_item end |
Instance Method Details
#active? ⇒ Boolean
59 60 61 |
# File 'app/components/primer/beta/nav_list/item.rb', line 59 def active? item_active?(self) && items.empty? end |
#before_render ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/components/primer/beta/nav_list/item.rb', line 68 def before_render if active_sub_item? @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 |
#expand! ⇒ Object
Cause this item to show its list of sub items when rendered.
64 65 66 |
# File 'app/components/primer/beta/nav_list/item.rb', line 64 def @expanded = true end |
#kind ⇒ Object
115 116 117 |
# File 'app/components/primer/beta/nav_list/item.rb', line 115 def kind :item end |