Class: Pulse::NavList::Item

Inherits:
ActionList::Item
  • Object
show all
Defined in:
app/components/pulse/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.

Instance Attribute Summary collapse

Instance Method Summary collapse

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



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
58
59
60
61
62
# File 'app/components/pulse/nav_list/item.rb', line 33

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] = merge_classes(
    system_arguments[:classes],
    'ActionListItem--subItem' => @sub_item
  )

  @sub_list_arguments = {
    classes: merge_classes(
      'ActionList',
      'ActionList--subGroup'
    )
  }

  @list = system_arguments[:list]

  if @list
    @sub_list_arguments['data-action'] =
      "keydown:#{@list.custom_element_name}#handleItemWithSubItemKeydown"
  end

  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.



10
11
12
# File 'app/components/pulse/nav_list/item.rb', line 10

def selected_by_ids
  @selected_by_ids
end

#sub_itemObject (readonly) Also known as: sub_item?

Returns the value of attribute sub_item.



10
11
12
# File 'app/components/pulse/nav_list/item.rb', line 10

def sub_item
  @sub_item
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/components/pulse/nav_list/item.rb', line 64

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

#before_renderObject



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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/components/pulse/nav_list/item.rb', line 73

def before_render
  if active_sub_item?
    expand!

    @content_arguments[:classes] = merge_classes(
      @content_arguments[:classes],
      'ActionListContent--hasActiveSubItem'
    )
  else
    @system_arguments[:classes] = merge_classes(
      @system_arguments[:classes],
      'pulse-outline-none pulse-bg-gray-50' => active?
    )
  end

  @content_arguments[:'aria-current'] = 'page' if active?

  super

  if items.present? && trailing_action.present?
    raise 'Cannot render a trailing action for an item with subitems'
  end

  if items.present? && @selected_by_ids.present?
    raise 'Cannot pass `selected_by_ids:` for an item with subitems, ' \
          'since parent items cannot be selected'
  end

  return if items.blank?

  @sub_list_arguments[:aria] = merge_aria(
    @sub_list_arguments,
    { aria: { labelledby: id } }
  )

  if href.present?
    raise ArgumentError,
          'Items with sub-items cannot have hrefs'
  end

  @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] = merge_classes(
    @system_arguments[:classes],
    'ActionListItem--hasSubItem'
  )
end

#expand!Object

Cause this item to show its list of sub items when rendered.



69
70
71
# File 'app/components/pulse/nav_list/item.rb', line 69

def expand!
  @expanded = true
end

#kindObject



131
132
133
# File 'app/components/pulse/nav_list/item.rb', line 131

def kind
  :item
end