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

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

Constructor Details

#initialize(list:, label: nil, label_classes: nil, label_arguments: {}, content_arguments: {}, form_arguments: {}, parent: nil, truncate_label: false, 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

Returns a new instance of Item.

Parameters:

  • list (Primer::Alpha::ActionList)

    The list that contains this item. Used internally.

  • parent (Primer::Alpha::ActionList::Item) (defaults to: nil)

    This item’s parent item. ‘nil` if this item is at the root. Used internally.

  • label (String) (defaults to: nil)

    Item label. If no label is provided, content is used.

  • label_classes (String) (defaults to: nil)

    CSS classes that will be added to the label.

  • label_arguments (Hash) (defaults to: {})

    <%= link_to_system_arguments_docs %> used to construct the label.

  • content_arguments (Hash) (defaults to: {})

    <%= link_to_system_arguments_docs %> used to construct the item’s anchor or button tag.

  • form_arguments (Hash) (defaults to: {})

    Allows the item to submit a form on click. The URL passed in the ‘href:` option will be used as the form action. Pass the `method:` option to this hash to control what kind of request is made, <%= one_of(Primer::Alpha::ActionList::FormWrapper::HTTP_METHOD_OPTIONS) %> The `name:` option is required and specifies the desired name of the field that will be included in the params sent to the server on form submission. Specify the `value:` option to send a custom value to the server; otherwise the value of `name:` is sent.

  • truncate_label (Boolean) (defaults to: false)

    Truncate label with ellipsis.

  • href (String) (defaults to: nil)

    Link URL.

  • role (String) (defaults to: nil)

    ARIA role describing the function of the item.

  • size (Symbol) (defaults to: DEFAULT_SIZE)

    Controls block sizing of the item.

  • scheme (Symbol) (defaults to: DEFAULT_SCHEME)

    Controls color/style based on behavior.

  • disabled (Boolean) (defaults to: false)

    Disabled items are not clickable and visually dim.

  • description_scheme (Symbol) (defaults to: DEFAULT_DESCRIPTION_SCHEME)

    Display description inline with label, or block on the next line. <%= one_of(Primer::Alpha::ActionList::Item::DESCRIPTION_SCHEME_OPTIONS) %>

  • active (Boolean) (defaults to: false)

    If the parent list’s ‘select_variant` is set to `:single` or `:multiple`, causes this item to render checked.

  • on_click (String) (defaults to: nil)

    JavaScript to execute when the item is clicked.

  • id (String) (defaults to: self.class.generate_id)

    Used internally.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
# File 'app/components/primer/alpha/action_list/item.rb', line 161

def initialize(
  list:,
  label: nil,
  label_classes: nil,
  label_arguments: {},
  content_arguments: {},
  form_arguments: {},
  parent: nil,
  truncate_label: false,
  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
  @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] ||= {}
  @system_arguments[:data][:targets] = "#{list_class.custom_element_name}.items"

  @label_arguments = {
    **label_arguments,
    classes: class_names(
      label_classes,
      label_arguments[:classes],
      "ActionListItem-label",
      "ActionListItem-label--truncate" => @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.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? ? :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.



131
132
133
# File 'app/components/primer/alpha/action_list/item.rb', line 131

def active
  @active
end

#disabledObject (readonly) Also known as: disabled?

Returns the value of attribute disabled.



131
132
133
# File 'app/components/primer/alpha/action_list/item.rb', line 131

def disabled
  @disabled
end

#hrefObject (readonly)

Returns the value of attribute href.



131
132
133
# File 'app/components/primer/alpha/action_list/item.rb', line 131

def href
  @href
end

#idObject (readonly)

Returns the value of attribute id.



131
132
133
# File 'app/components/primer/alpha/action_list/item.rb', line 131

def id
  @id
end

#listObject (readonly)

Returns the value of attribute list.



131
132
133
# File 'app/components/primer/alpha/action_list/item.rb', line 131

def list
  @list
end

#parentObject (readonly)

Returns the value of attribute parent.



131
132
133
# File 'app/components/primer/alpha/action_list/item.rb', line 131

def parent
  @parent
end