Removing selected option sometimes needs multiple clicks

Created on 23 March 2022, over 2 years ago
Updated 21 May 2024, 7 months ago

Problem/Motivation

In Select2 Entity Reference field widgets, with autocomplete enabled, for multivalued fields, removing a selected option doesn't always work on the first click. When the issue occurs, it needs 3 clicks. See screen recording @ https://www.drupal.org/files/issues/2022-03-23/Screen%20Recording%202022... โ†’ .

Steps to reproduce

  1. Install standard Drupal
  2. Install select2 module
  3. Configure the Select2 Entity Reference field widget for the Tags field on Article content type
  4. Make sure autocomplete is enabled for the Select2 Entity Reference field widget
  5. Create 10 tags or so
  6. Create an Article with those 10 tags
  7. Navigate to the edit form of the Article you just created
  8. Start clicking on the X-icons in the tags field 1 by 1. Eventually you'll come across a tag that needs 3 clicks before it get removed.

Proposed resolution

Unsure what the solution is, but I did track the root cause of the issue: the drag & drop functionality that is initiated here:

        // Copied from https://github.com/woocommerce/woocommerce/blob/master/assets/js/admin/wc-enhanced-select.js#L118
        if (Object.prototype.hasOwnProperty.call(config, 'ajax') && config.multiple) {
          var $select = $(this);
          var $list = $select.next('.select2-container').find('ul.select2-selection__rendered');
          Sortable.create($list[0], {
            draggable: 'li:not(.select2-search)',
            forceFallback: true,
            onEnd: function () {
              $($list.find('.select2-selection__choice').get().reverse()).each(function () {
                $select.prepend($select.find('option[value="' + $(this).data('optionValue') + '"]').first());
              });
            }
          });
        }

The usage of SortableJs was introduced in #3104877: Replace jquery.ui.sortable with SortableJs โ†’ .

๐Ÿ› Bug report
Status

Fixed

Version

1.0

Component

Field widgets

Created by

๐Ÿ‡ง๐Ÿ‡ชBelgium rp7

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