class Primer::Alpha::ActionList::Item
such as icons, avatars, and counters.
An individual ‘ActionList` item. Items can optionally include leading and/or trailing visuals,
def before_render
def before_render if @list.allows_selection? @content_arguments[:aria] = merge_aria( @content_arguments, { aria: { checked: active? } } ) end @system_arguments[:classes] = class_names( @system_arguments[:classes], "ActionListItem--withActions" => trailing_action.present? ) return unless leading_visual @content_arguments[:classes] = class_names( @content_arguments[:classes], "ActionListContent--visual16" => leading_visual, "ActionListContent--blockDescription" => description && @description_scheme == :block ) end
def initialize(
-
system_arguments
(Hash
) -- <%= link_to_system_arguments_docs %> -
id
(String
) -- Used internally. -
on_click
(String
) -- JavaScript to execute when the item is clicked. -
active
(Boolean
) -- If the parent list's `select_variant` is set to `:single` or `:multiple`, causes this item to render checked. -
description_scheme
(Symbol
) -- Display description inline with label, or block on the next line. <%= one_of(Primer::Alpha::ActionList::Item::DESCRIPTION_SCHEME_OPTIONS) %> -
disabled
(Boolean
) -- Disabled items are not clickable and visually dim. -
scheme
(Symbol
) -- Controls color/style based on behavior. -
size
(Symbol
) -- Controls block sizing of the item. -
role
(String
) -- ARIA role describing the function of the item. -
href
(String
) -- Link URL. -
truncate_label
(Boolean
) -- Truncate label with ellipsis. -
form_arguments
(Hash
) -- 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. -
content_arguments
(Hash
) -- <%= link_to_system_arguments_docs %> used to construct the item's anchor or button tag. -
label_arguments
(Hash
) -- <%= link_to_system_arguments_docs %> used to construct the label. -
label_classes
(String
) -- CSS classes that will be added to the label. -
label
(String
) -- Item label. If no label is provided, content is used. -
parent
(Primer::Alpha::ActionList::Item
) -- This item's parent item. `nil` if this item is at the root. Used internally. -
list
(Primer::Alpha::ActionList
) -- The list that contains this item. Used internally.
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
def list_class
def list_class Primer::Alpha::ActionList end