The [0] hatch in misc/vertical-tabs.js causes issues if there are multiple forms with vertical tabs

Created on 21 June 2022, over 2 years ago
Updated 11 July 2024, 3 months ago

Problem/Motivation

In the refactoring issue, https://www.drupal.org/project/drupal/issues/3239134 , the [0] hatch was added to misc/vertical-tabs.js in line 98: https://git.drupalcode.org/project/drupal/-/blob/9.5.x/core/misc/vertica...

If there are multiple forms on a page that uses vertical tabs, this will produce a javascript error because of the hard-coded [0].

The following javascript error occurs

Uncaught TypeError: Cannot set properties of undefined (setting 'value')
    at Drupal.verticalTab.focus (vertical-tabs.js?v=9.4.0:98:78)
    at vertical-tabs.js?v=9.4.0:65:40
    at Array.forEach (<anonymous>)
    at Object.attach (vertical-tabs.js?v=9.4.0:25:68)
    at drupal.js?v=9.4.0:27:24
    at Array.forEach (<anonymous>)
    at Drupal.attachBehaviors (drupal.js?v=9.4.0:24:34)
    at HTMLDivElement.open (nd_visualshortcodes.js?v=1:588:16)
    at t.<computed>.<computed>._trigger (widget.js:706:13)
    at t.<computed>.<computed>.open (dialog.js:300:8)

See screenshot:

Steps to reproduce

1. Create a page that has multiple forms using vertical tabs.
2. If you click one of the tabs, you get the javascript error described above.

Proposed resolution

Remove the [0] hatch from misc/vertical-tabs.js like such.

From:

        focus() {
      this.details
        .siblings('.vertical-tabs__pane')
        .each(function () {
          const tab = $(this).data('verticalTab');
          tab.details.hide();
          tab.details.removeAttr('open');
          tab.item.removeClass('is-selected');
        })
        .end()
        .show()
        .siblings(':hidden.vertical-tabs__active-tab')[0].value =
        this.details.attr('id');

to

        focus() {
      this.details
        .siblings('.vertical-tabs__pane')
        .each(function () {
          const tab = $(this).data('verticalTab');
          tab.details.hide();
          tab.details.removeAttr('open');
          tab.item.removeClass('is-selected');
        })
        .end()
        .show()
        .siblings(':hidden.vertical-tabs__active-tab')
        .get()
        .forEach((hidden) => {
          hidden.value = this.details.attr('id');
        });

Using [0] directly assumes that there is exactly one hidden element matching `:hidden.vertical-tabs__active-tab`.But with the `.siblings(':hidden.vertical-tabs__active-tab')` selects all hidden sibling elements with the class 'vertical-tabs__active-tab'. and .get() method iterates through all the elements matching the condition.
With the latest changes

If there are multiple forms on a page that uses vertical tabs, this will produce a javascript error because of the hard-coded [0].

will be solved.

🐛 Bug report
Status

Fixed

Version

10.3

Component
Javascript 

Last updated about 3 hours ago

Created by

🇺🇸United States baldwinlouie

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024