Class: Primer::Beta::NavList::Item

Inherits:
Alpha::ActionList::Item show all
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

Alpha::NavList::Item

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::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

Constants inherited from Component

Component::INVALID_ARIA_LABEL_TAGS

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

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

Attributes inherited from Alpha::ActionList::Item

#active, #disabled, #href, #id, #list, #parent

Instance Method Summary collapse

Methods inherited from Component

deprecated?, generate_id

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?

Methods included from ClassNameHelper

#class_names

Methods included from AttributesHelper

#aria, #data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes

Constructor Details

#initialize(selected_item_id: nil, selected_by_ids: [], sub_item: false, expanded: false, **system_arguments) ⇒ Item

Returns a new instance of Item.

Parameters:

  • selected_item_id (Symbol) (defaults to: nil)

    The ID of the currently selected list item. Used internally.

  • selected_by_ids (Array<Symbol>) (defaults to: [])

    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.

  • expanded (Boolean) (defaults to: false)

    Whether this item shows (expands) or hides (collapses) its list of sub items.

  • sub_item (Boolean) (defaults to: false)

    Whether or not this item is nested under a parent item. Used internally.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



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 = 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_idsObject (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_itemObject (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

Returns:

  • (Boolean)


59
60
61
# File 'app/components/primer/beta/nav_list/item.rb', line 59

def active?
  item_active?(self) && items.empty?
end

#before_renderObject

Raises:

  • (ArgumentError)


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?
    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

#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 expand!
  @expanded = true
end

#kindObject



115
116
117
# File 'app/components/primer/beta/nav_list/item.rb', line 115

def kind
  :item
end