Fix autocomplete chip X "ends with" conflicts

Let's try this again without the regex.

Clicking the X button in an autocomplete chip could inadvertently remove
the wrong tag (and cause a weird "merging" behavior) if another tag
ended with the same text as the one being removed. For example, if a
post had both "one.two" and "two" tags and you clicked the X button on
the "two".
This commit is contained in:
Deimos
2020-03-05 14:13:11 -07:00
parent 6e610be349
commit c8f8697767

View File

@@ -6,8 +6,17 @@ $.onmount("[data-js-autocomplete-chip-clear]", function() {
var $tagsHiddenInput = $("[data-js-autocomplete-hidden-input]");
var $autocompleteInput = $("[data-js-autocomplete-input]");
var textToReplace = new RegExp($chip.text() + ",");
$tagsHiddenInput.val($tagsHiddenInput.val().replace(textToReplace, ""));
var tags = $tagsHiddenInput.val().split(",");
var tagToRemove = $chip.text();
$tagsHiddenInput.val(
tags
.filter(function(tag) {
return tag !== tagToRemove;
})
.join(",")
);
$chip.remove();
$autocompleteInput.focus();
}