Skip to content

Commit

Permalink
Merge pull request #5861 from mamhoff/allow-other-variant-autocomplet…
Browse files Browse the repository at this point in the history
…e-options

Variant Autocomplete: Allow passing select2 options
  • Loading branch information
tvdeyen authored Oct 4, 2024
2 parents 9251e11 + dce4abb commit 0a27b89
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
/**
* Make the element a select2 dropdown used for finding Variants. By default, the search term will be
* passed to the defined Spree::Config.variant_search_class by the controller with its defined scope.
* @param {Object|undefined|null} options Options
* @param {Object|undefined|null} options Options to be passed to select2. If null, the default options will be used.
* @param {Function|undefined} options.searchParameters Returns a hash object for params to merge on the select2 ajax request
* Accepts an argument of the select2 search term. To use Ransack, define
* variant_search_term as a falsy value, and q as the Ransack query. Note,
* you need to ensure that the attributes are allowed to be Ransacked.
*/
$.fn.variantAutocomplete = function(options) {
$.fn.variantAutocomplete = function(options = {}) {
// Default options
options = options || {}

this.select2({
const searchParameters = options.searchParameters || null
delete options.searchParameters
const select2options = options
const defaultOptions = {
placeholder: Spree.translations.variant_placeholder,
minimumInputLength: 3,
initSelection: function(element, callback) {
Expand All @@ -39,7 +40,7 @@
}
},
data: function(term, page) {
const extraParameters = options["searchParameters"] ? options["searchParameters"](term) : {}
const extraParameters = searchParameters ? searchParameters(term) : {}

return {
variant_search_term: term,
Expand All @@ -64,7 +65,9 @@
} else {
return Select2.util.escapeMarkup(variant.name);
}
}
});
},
}

this.select2(Object.assign({}, defaultOptions, select2options));
};
})();

0 comments on commit 0a27b89

Please sign in to comment.