Class: Yattho::Alpha::NavList::Item

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

Constant Summary

Constants inherited from ActionList::Item

ActionList::Item::DEFAULT_DESCRIPTION_SCHEME, ActionList::Item::DEFAULT_SCHEME, ActionList::Item::DEFAULT_SIZE, ActionList::Item::DESCRIPTION_SCHEME_MAPPINGS, ActionList::Item::DESCRIPTION_SCHEME_OPTIONS, ActionList::Item::SCHEME_MAPPINGS, ActionList::Item::SCHEME_OPTIONS, ActionList::Item::SIZE_MAPPINGS, 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

Instance Attribute Summary collapse

Attributes inherited from ActionList::Item

#active, #disabled, #href, #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

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
# File 'app/components/yattho/alpha/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"
    )
  }

  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/yattho/alpha/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/yattho/alpha/nav_list/item.rb', line 11

def sub_item
  @sub_item
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/components/yattho/alpha/nav_list/item.rb', line 55

def active?
  item_active?(self)
end

#before_renderObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/components/yattho/alpha/nav_list/item.rb', line 64

def before_render
  if active_sub_item?
    expand!

    @content_arguments[:classes] = class_names(
      @content_arguments[:classes],
      "ActionListContent--hasActiveSubItem"
    )
  end

  super

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

  return if items.blank?

  @content_arguments[:tag] = :button
  @content_arguments[:'aria-expanded'] = @expanded.to_s
  @content_arguments[:'data-action'] = "click:#{@list.custom_element_name}#handleItemWithSubItemClick"

  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.



60
61
62
# File 'app/components/yattho/alpha/nav_list/item.rb', line 60

def expand!
  @expanded = true
end