Class: Primer::Alpha::TreeView::Node

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

Overview

A generic TreeView node.

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

Constant Summary collapse

DEFAULT_NODE_VARIANT =
Primer::Alpha::TreeView::DEFAULT_NODE_VARIANT
NODE_VARIANT_TAG_MAP =
{ DEFAULT_NODE_VARIANT => :div, :button => :button, :anchor => :a }.freeze
NODE_VARIANT_TAG_OPTIONS =
NODE_VARIANT_TAG_MAP.keys.freeze
DEFAULT_SELECT_VARIANT =
:none
SELECT_VARIANT_OPTIONS =
[
  :single,
  :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 Primer::AttributesHelper

Primer::AttributesHelper::PLURAL_ARIA_ATTRIBUTES, Primer::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 Primer::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:, node_variant:, href: nil, current: false, select_variant: DEFAULT_SELECT_VARIANT, checked: DEFAULT_CHECKED_STATE, disabled: false, value: nil, **content_arguments) ⇒ Node



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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/components/primer/alpha/tree_view/node.rb', line 80

def initialize(
  path:,
  node_variant:,
  href: nil,
  current: false,
  select_variant: DEFAULT_SELECT_VARIANT,
  checked: DEFAULT_CHECKED_STATE,
  disabled: false,
  value: nil,
  **content_arguments
)
  @system_arguments = {
    tag: :li,
    role: :none,
    classes: "TreeViewItem"
  }

  @content_arguments = content_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)
  @disabled = disabled
  @node_variant = fetch_or_fallback(NODE_VARIANT_TAG_OPTIONS, node_variant, DEFAULT_NODE_VARIANT)

  @content_arguments[:tag] = NODE_VARIANT_TAG_MAP[@node_variant]
  @content_arguments[:href] = href if href
  @content_arguments[:id] = content_id
  @content_arguments[:role] = :treeitem
  @content_arguments[:tabindex] = current? ? 0 : -1
  @content_arguments[:classes] = class_names(
    @content_arguments.delete(:classes),
    "TreeViewItemContent"
  )

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

  @content_arguments[:data] = merge_data(
    @content_arguments, {
    data: {
      value: value,
      path: @path.to_json,
      select_variant: @select_variant
    }
  }
  )

  return unless current?

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

Instance Attribute Details

#checkedString (readonly)

This node’s checked state.



39
40
41
# File 'app/components/primer/alpha/tree_view/node.rb', line 39

def checked
  @checked
end

#currentBoolean (readonly) Also known as: current?

Wether or not this node is the current node.



33
34
35
# File 'app/components/primer/alpha/tree_view/node.rb', line 33

def current
  @current
end

#disabledBoolean (readonly) Also known as: disabled?

Whether or not this node is disabled, i.e. cannot be activated.



54
55
56
# File 'app/components/primer/alpha/tree_view/node.rb', line 54

def disabled
  @disabled
end

#node_variantSymbol (readonly)

This node’s variant, eg. :button, :div, etc.



49
50
51
# File 'app/components/primer/alpha/tree_view/node.rb', line 49

def node_variant
  @node_variant
end

#select_variantSymbol (readonly)

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



44
45
46
# File 'app/components/primer/alpha/tree_view/node.rb', line 44

def select_variant
  @select_variant
end

Instance Method Details

#levelInteger

The numeric depth of this node.



149
150
151
# File 'app/components/primer/alpha/tree_view/node.rb', line 149

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.



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/components/primer/alpha/tree_view/node.rb', line 157

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

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

  @content_arguments.merge!(**other_arguments)
end