Class: Primer::Alpha::ActionList::Item

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

Overview

An individual ‘ActionList` item. Items can optionally include leading and/or trailing visuals, such as icons, avatars, and counters.

Direct Known Subclasses

Beta::NavList::Item

Constant Summary collapse

DEFAULT_SIZE =
:medium
SIZE_MAPPINGS =
{
  DEFAULT_SIZE => nil,
  :large => "ActionListContent--sizeLarge",
  :xlarge => "ActionListContent--sizeXLarge"
}.freeze
SIZE_OPTIONS =
SIZE_MAPPINGS.keys.freeze
DEFAULT_DESCRIPTION_SCHEME =
:block
DESCRIPTION_SCHEME_MAPPINGS =
{
  :inline => "ActionListItem-descriptionWrap--inline",
  DEFAULT_DESCRIPTION_SCHEME => "ActionListItem-descriptionWrap"
}.freeze
DESCRIPTION_SCHEME_OPTIONS =
DESCRIPTION_SCHEME_MAPPINGS.keys.freeze
DEFAULT_SCHEME =
:default
SCHEME_MAPPINGS =
{
  DEFAULT_SCHEME => nil,
  :danger => "ActionListItem--danger"
}.freeze
SCHEME_OPTIONS =
SCHEME_MAPPINGS.keys.freeze
DEFAULT_TRUNCATION_BEHAVIOR =
:none
TRUNCATION_BEHAVIOR_MAPPINGS =
{
  DEFAULT_TRUNCATION_BEHAVIOR => nil,
  false => nil,
  :show_tooltip => "ActionListItem-label--truncate",
  :truncate => "ActionListItem-label--truncate",
  true => "ActionListItem-label--truncate"
}
TRUNCATION_BEHAVIOR_OPTIONS =
TRUNCATION_BEHAVIOR_MAPPINGS.keys.freeze

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(list:, label: nil, item_id: nil, label_classes: nil, label_arguments: {}, content_arguments: {}, form_arguments: {}, parent: nil, truncate_label: :none, href: nil, role: nil, size: DEFAULT_SIZE, scheme: DEFAULT_SCHEME, disabled: false, description_scheme: DEFAULT_DESCRIPTION_SCHEME, active: false, on_click: nil, id: self.class.generate_id, **system_arguments) ⇒ Item



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'app/components/primer/alpha/action_list/item.rb', line 181

def initialize(
  list:,
  label: nil,
  item_id: nil,
  label_classes: nil,
  label_arguments: {},
  content_arguments: {},
  form_arguments: {},
  parent: nil,
  truncate_label: :none,
  href: nil,
  role: nil,
  size: DEFAULT_SIZE,
  scheme: DEFAULT_SCHEME,
  disabled: false,
  description_scheme: DEFAULT_DESCRIPTION_SCHEME,
  active: false,
  on_click: nil,
  id: self.class.generate_id,
  **system_arguments
)
  @list = list
  @parent = parent
  @label = label
  @item_id = item_id
  @href = href || content_arguments[:href]
  @truncate_label = truncate_label
  @disabled = disabled
  @active = active
  @id = id
  @system_arguments = system_arguments
  @content_arguments = content_arguments
  @form_wrapper = FormWrapper.new(list: @list, action: @href, **form_arguments)

  @size = fetch_or_fallback(SIZE_OPTIONS, size, DEFAULT_SIZE)
  @scheme = fetch_or_fallback(SCHEME_OPTIONS, scheme, DEFAULT_SCHEME)
  @description_scheme = fetch_or_fallback(
    DESCRIPTION_SCHEME_OPTIONS, description_scheme, DEFAULT_DESCRIPTION_SCHEME
  )

  @system_arguments[:classes] = class_names(
    @system_arguments[:classes],
    SCHEME_MAPPINGS[@scheme],
    "ActionListItem",
    "ActionListItem--disabled" => @disabled
  )

  @system_arguments[:data] = merge_data(
    @system_arguments, {
      data: {
        targets: "#{list_class.custom_element_name}.items",
        **(@item_id ? { item_id: @item_id } : {})
      }
    }
  )

  @label_arguments = {
    **label_arguments,
    classes: class_names(
      label_classes,
      label_arguments[:classes],
      "ActionListItem-label",
      TRUNCATION_BEHAVIOR_MAPPINGS[@truncate_label],
    )
  }

  @content_arguments[:id] = @id
  @content_arguments[:classes] = class_names(
    @content_arguments[:classes],
    "ActionListContent",
    SIZE_MAPPINGS[@size]
  )

  unless @content_arguments[:tag]
    if @href && @form_wrapper.get? && !@disabled
      @content_arguments[:tag] = :a
      @content_arguments[:href] = @href
    else
      @content_arguments[:tag] = :button
      @content_arguments[:type] = @form_wrapper.form_required? ? :submit : :button
      @content_arguments[:onclick] = on_click if on_click
    end
  end

  if @content_arguments[:tag] != :button && @form_wrapper.form_required?
    raise ArgumentError, "items that submit forms must use a \"button\" tag instead of \"#{@content_arguments[:tag]}\""
  end

  if @content_arguments[:tag] != :button && @list.acts_as_form_input?
    raise ArgumentError, "items within lists/menus that act as form inputs must use \"button\" tags instead of \"#{@content_arguments[:tag]}\""
  end

  if @disabled
    @content_arguments[:aria] ||= merge_aria(
      @content_arguments,
      { aria: { disabled: "true" } }
    )
  end

  @content_arguments[:role] = role ||
                              if @list.acts_as_listbox?
                                ActionList::LIST_BOX_ITEM_ROLE
                              elsif @list.allows_selection?
                                ActionList::SELECT_VARIANT_ROLE_MAP[@list.select_variant]
                              elsif @list.acts_as_menu?
                                ActionList::DEFAULT_MENU_ITEM_ROLE
                              end

  @system_arguments[:role] = @list.acts_as_menu? || @list.acts_as_listbox? ? :none : nil

  @description_wrapper_arguments = {
    classes: class_names(
      "ActionListItem-descriptionWrap",
      DESCRIPTION_SCHEME_MAPPINGS[@description_scheme]
    )
  }
end

Instance Attribute Details

#activeObject (readonly) Also known as: active?

Returns the value of attribute active.



150
151
152
# File 'app/components/primer/alpha/action_list/item.rb', line 150

def active
  @active
end

#disabledObject (readonly) Also known as: disabled?

Returns the value of attribute disabled.



150
151
152
# File 'app/components/primer/alpha/action_list/item.rb', line 150

def disabled
  @disabled
end

#hrefObject (readonly)

Returns the value of attribute href.



150
151
152
# File 'app/components/primer/alpha/action_list/item.rb', line 150

def href
  @href
end

#idObject (readonly)

Returns the value of attribute id.



150
151
152
# File 'app/components/primer/alpha/action_list/item.rb', line 150

def id
  @id
end

#item_idObject (readonly)

Returns the value of attribute item_id.



150
151
152
# File 'app/components/primer/alpha/action_list/item.rb', line 150

def item_id
  @item_id
end

#listObject (readonly)

Returns the value of attribute list.



150
151
152
# File 'app/components/primer/alpha/action_list/item.rb', line 150

def list
  @list
end

#parentObject (readonly)

Returns the value of attribute parent.



150
151
152
# File 'app/components/primer/alpha/action_list/item.rb', line 150

def parent
  @parent
end