Browse Source

Include "leftover" text in tags input on submit

Previously, the autocompleting tag input would only actually submit the
tags that had been converted to "chips". This meant that if the user had
a "leftover" tag that they had typed in but not actually converted to a
chip (by typing a comma or using the autocomplete dropdown), it would be
lost when they submitted the form.

This appends it to the tags before submitting, so that it's not lost.
merge-requests/68/head
Deimos 6 years ago
parent
commit
5c2230dddc
  1. 19
      tildes/static/js/behaviors/autocomplete-input.js

19
tildes/static/js/behaviors/autocomplete-input.js

@ -42,6 +42,25 @@ $.onmount('[data-js-autocomplete-input]', function() {
// move the "tags" name to the hidden input (so the form works without JS) // move the "tags" name to the hidden input (so the form works without JS)
$(this).removeAttr("name"); $(this).removeAttr("name");
$("[data-js-autocomplete-hidden-input]").attr("name", "tags"); $("[data-js-autocomplete-hidden-input]").attr("name", "tags");
// attach an event handler to the form that will add the input's value to
// the end of the tags list before submitting (to include any tag that's
// still in the input and wasn't converted to a chip)
$(this).closest("form").on("beforeSend.ic", function(evt, elt, data, settings, xhr, requestId) {
$autocompleteInput = $(elt).find("[data-js-autocomplete-input]").first();
if (!$autocompleteInput.val()) {
return;
}
var dataPieces = settings.data.split("&");
for (i = 0; i < dataPieces.length; i++) {
if (dataPieces[i].indexOf("tags=") === 0) {
dataPieces[i] += $autocompleteInput.val();
break;
}
}
settings.data = dataPieces.join("&");
});
} }
if ($(this).val() !== '') { if ($(this).val() !== '') {

Loading…
Cancel
Save