Class: Primer::OpenProject::TreeView::Node

Inherits:
Component
  • Object
show all
Defined in:
app/components/primer/open_project/tree_view/node.rb

Overview

A generic ‘TreeView` node.

This component is part of the <%= link_to_component(Primer::OpenProject::TreeView) %> component and should not be used directly.

Constant Summary collapse

DEFAULT_SELECT_VARIANT =
:none
SELECT_VARIANT_OPTIONS =
[
  :multiple,
  DEFAULT_SELECT_VARIANT
].freeze
DEFAULT_CHECKED_STATE =
false
CHECKED_STATES =
[
  DEFAULT_CHECKED_STATE,
  true,
  "mixed"
]

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

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, #extract_data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes

Methods included from ExperimentalSlotHelpers

included

Methods included from ExperimentalRenderHelpers

included

Constructor Details

#initialize(path:, current: false, select_variant: DEFAULT_SELECT_VARIANT, checked: DEFAULT_CHECKED_STATE, **system_arguments) ⇒ Node

Returns a new instance of Node.

Parameters:

  • path (Array<String>)

    The node’s “path,” i.e. this node’s label and the labels of all its ancestors. This node should be reachable by traversing the tree following this path.

  • current (Boolean) (defaults to: false)

    Whether or not this node is the current node. The current node is styled differently than regular nodes and is the first element that receives focus when tabbing to the ‘TreeView` component.

  • select_variant (Symbol) (defaults to: DEFAULT_SELECT_VARIANT)

    Controls the type of checkbox that appears. <%= one_of(Primer::OpenProject::TreeView::Node::SELECT_VARIANT_OPTIONS) %>

  • checked (Boolean | String) (defaults to: DEFAULT_CHECKED_STATE)

    The checked state of the node’s checkbox. <%= one_of(Primer::OpenProject::TreeView::Node::CHECKED_STATES) %>

  • system_arguments (Hash)

    The arguments accepted by <%= link_to_component(Primer::Alpha::ActionList) %>.



60
61
62
63
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/components/primer/open_project/tree_view/node.rb', line 60

def initialize(
  path:,
  current: false,
  select_variant: DEFAULT_SELECT_VARIANT,
  checked: DEFAULT_CHECKED_STATE,
  **system_arguments
)
  @system_arguments = deny_tag_argument(**system_arguments)

  @path = path
  @current = current
  @select_variant = fetch_or_fallback(SELECT_VARIANT_OPTIONS, select_variant, DEFAULT_SELECT_VARIANT)
  @checked = fetch_or_fallback(CHECKED_STATES, checked, DEFAULT_CHECKED_STATE)

  @system_arguments[:tag] = :li
  @system_arguments[:role] = :treeitem
  @system_arguments[:tabindex] = current? ? 0 : -1
  @system_arguments[:classes] = class_names(
    @system_arguments.delete(:classes),
    "TreeViewItem"
  )

  @system_arguments[:aria] = merge_aria(
    @system_arguments, {
      aria: {
        level: level,
        selected: false,
        checked: checked,
        labelledby: content_id
      }
    }
  )

  @system_arguments[:data] = merge_data(
    @system_arguments,
    { data: { path: @path.to_json } }
  )

  return unless current?

  @system_arguments[:aria] = merge_aria(
    @system_arguments,
    { aria: { current: true } }
  )
end

Instance Attribute Details

#checkedString (readonly)

This node’s checked state.

Returns:

  • (String)


35
36
37
# File 'app/components/primer/open_project/tree_view/node.rb', line 35

def checked
  @checked
end

#currentBoolean (readonly) Also known as: current?

Wether or not this node is the current node.

Returns:

  • (Boolean)


29
30
31
# File 'app/components/primer/open_project/tree_view/node.rb', line 29

def current
  @current
end

#select_variantSymbol (readonly)

This node’s select variant (i.e. check box variant).

Returns:

  • (Symbol)


40
41
42
# File 'app/components/primer/open_project/tree_view/node.rb', line 40

def select_variant
  @select_variant
end

Instance Method Details

#levelInteger

The numeric depth of this node.

Returns:

  • (Integer)

    This node’s depth.



109
110
111
# File 'app/components/primer/open_project/tree_view/node.rb', line 109

def level
  @level ||= @path.size
end

#merge_system_arguments!(**other_arguments) ⇒ Object

Merges the given arguments into the current hash of system arguments provided when the component was initially constructed. This method can be used to add additional arguments just before rendering.

Parameters:

  • other_arguments (Hash)

    The other hash of system arguments to merge into the current one.



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/components/primer/open_project/tree_view/node.rb', line 117

def merge_system_arguments!(**other_arguments)
  @system_arguments[:aria] = merge_aria(
    @system_arguments,
    other_arguments
  )

  @system_arguments[:data] = merge_data(
    @system_arguments,
    other_arguments
  )

  @system_arguments.merge!(**other_arguments)
end