Skip to content

Commit

Permalink
autosuggest: when an item is unselected, remove the corresponding HTM…
Browse files Browse the repository at this point in the history
…L 'option' element (#240)

The select2 component that we use to manage the autosuggest controls appends an HTML 'option' element each time an item is selected by the user, if one does not already exist.

Also, the select2 component displays user-facing selections by looping over the HTML option elements that it finds in the DOM.

So, to ensure that newly-added selections always appear at the end of the user-facing selection list, we need to maintain the same order for the option elements in the DOM.  In particular, when a user unselects/clears an item, we need to remove the corresponding HTML option element.
  • Loading branch information
jayaddison committed Oct 17, 2023
1 parent 1da4edf commit 53fa276
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/app/autosuggest.ts
Expand Up @@ -23,6 +23,11 @@ function bindEquipmentInput(element: string, label: string, placeholder: string)

// TODO: Revisit once https://github.com/select2/select2/issues/3744 is handled
$(element).next('.select2').find('input[type=search]').attr('aria-label', label);

// TODO: https://github.com/openculinary/frontend/issues/239 - report upstream?
$(element).on('select2:unselect', function(evt) {
evt.params.data.element.remove();
});
}

function bindIngredientInput(element: string, label: string, placeholder: string) : void {
Expand All @@ -45,6 +50,11 @@ function bindIngredientInput(element: string, label: string, placeholder: string

// TODO: Revisit once https://github.com/select2/select2/issues/3744 is handled
$(element).next('.select2').find('input[type=search]').attr('aria-label', label);

// TODO: https://github.com/openculinary/frontend/issues/239 - report upstream?
$(element).on('select2:unselect', function(evt) {
evt.params.data.element.remove();
});
}

$(function() {
Expand Down

0 comments on commit 53fa276

Please sign in to comment.