Class: Pulse::ActionList::Item

Inherits:
Component
  • Object
show all
Defined in:
app/components/pulse/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

NavList::Item

Constant Summary collapse

DEFAULT_SIZE =
:medium
SIZE_MAPPINGS =
{
  DEFAULT_SIZE => nil
  # :large => "ActionListContent--sizeLarge",
  # :xlarge => "ActionListContent--sizeXLarge"
}.freeze
SIZE_OPTIONS =

:large => "ActionListContent--sizeLarge", :xlarge => "ActionListContent--sizeXLarge"

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::TW_MERGE

Constants included from Pulse::AttributesHelper

Pulse::AttributesHelper::PLURAL_ARIA_ATTRIBUTES, Pulse::AttributesHelper::PLURAL_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Component

generate_id, #merge_attributes, #merge_classes

Methods included from SvgHelper

#render_svg

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean

Methods included from Pulse::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 (Pulse::ActionList)

    The list that contains this item. Used internally.

  • parent (Pulse::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(Pulse::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(Pulse::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 %>



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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'app/components/pulse/action_list/item.rb', line 167

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] = merge_classes(
    @system_arguments[:classes],
    SCHEME_MAPPINGS[@scheme],
    'pulse-relative pulse-rounded selected:pulse-font-normal',
    'ActionListItem--disabled' => @disabled
  )

  @system_arguments[:data] ||= {}
  @system_arguments[:data][:targets] =
    "#{list_class.custom_element_name}.items"

  @label_arguments = {
    **label_arguments,
    classes: merge_classes(
      label_classes,
      label_arguments[:classes],
      'pulse-font-semibold',
      'ActionListItem-label--truncate' => @truncate_label
    )
  }

  @content_arguments[:id] = @id
  @content_arguments[:classes] = merge_classes(
    @content_arguments[:classes],
    'pulse-relative pulse-grid pulse-w-full pulse-text-start',
    'pulse-select-none pulse-bg-transparent pulse-rounded',
    'pulse-grid-rows-[min-content]',
    'pulse-grid-cols-[min-content_min-content_minmax(0,auto)_min-content_min-content]', # rubocop:disable Layout/LineLength
    'pulse-border-none hover:pulse-no-underline pulse-py-2',
    'pulse-items-start pulse-gap-2 pulse-items-center',
    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: merge_classes(
      'ActionListItem-descriptionWrap',
      DESCRIPTION_SCHEME_MAPPINGS[@description_scheme]
    )
  }
end

Instance Attribute Details

#activeObject (readonly) Also known as: active?

Returns the value of attribute active.



137
138
139
# File 'app/components/pulse/action_list/item.rb', line 137

def active
  @active
end

#disabledObject (readonly) Also known as: disabled?

Returns the value of attribute disabled.



137
138
139
# File 'app/components/pulse/action_list/item.rb', line 137

def disabled
  @disabled
end

#hrefObject (readonly)

Returns the value of attribute href.



137
138
139
# File 'app/components/pulse/action_list/item.rb', line 137

def href
  @href
end

#idObject (readonly)

Returns the value of attribute id.



137
138
139
# File 'app/components/pulse/action_list/item.rb', line 137

def id
  @id
end

#listObject (readonly)

Returns the value of attribute list.



137
138
139
# File 'app/components/pulse/action_list/item.rb', line 137

def list
  @list
end

#parentObject (readonly)

Returns the value of attribute parent.



137
138
139
# File 'app/components/pulse/action_list/item.rb', line 137

def parent
  @parent
end