Run onChange callback when removing highligted items

This commit is contained in:
Josh Johnson 2016-09-20 12:57:44 +01:00
parent ef1e7d23ef
commit 727d304f49
4 changed files with 24 additions and 14 deletions

View file

@ -1,4 +1,4 @@
/*! choices.js v2.0.2 | (c) 2016 Josh Johnson | https://github.com/jshjohnson/Choices#readme */ /*! choices.js v2.0.3 | (c) 2016 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
/******/ (function(modules) { // webpackBootstrap /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache /******/ // The module cache
/******/ var installedModules = {}; /******/ var installedModules = {};
@ -225,9 +225,10 @@
if (!cuttingTheMustard) console.error('Choices: Your browser doesn\'t support Choices'); if (!cuttingTheMustard) console.error('Choices: Your browser doesn\'t support Choices');
// Input type check // Input type check
var canInit = this.passedElement && (0, _utils.isElement)(this.passedElement) && ['select-one', 'select-multiple', 'text'].some(function (type) { var isValidType = ['select-one', 'select-multiple', 'text'].some(function (type) {
return type === _this.passedElement.type; return type === _this.passedElement.type;
}); });
var canInit = (0, _utils.isElement)(this.passedElement) && isValidType;
if (canInit) { if (canInit) {
// If element has already been initalised with Choices // If element has already been initalised with Choices
@ -465,11 +466,18 @@
value: function removeHighlightedItems() { value: function removeHighlightedItems() {
var _this6 = this; var _this6 = this;
var runCallback = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
var items = this.store.getItemsFilteredByActive(); var items = this.store.getItemsFilteredByActive();
items.forEach(function (item) { items.forEach(function (item) {
if (item.highlighted && item.active) { if (item.highlighted && item.active) {
_this6._removeItem(item); _this6._removeItem(item);
// If this action was performed by the user
// run the callback
if (runCallback) {
_this6._triggerChange(item.value);
}
} }
}); });
@ -997,7 +1005,7 @@
if (!hasHighlightedItems) { if (!hasHighlightedItems) {
this.highlightItem(lastItem); this.highlightItem(lastItem);
} }
this.removeHighlightedItems(); this.removeHighlightedItems(true);
} }
} }
} }
@ -4650,7 +4658,6 @@
*/ */
var extend = exports.extend = function extend() { var extend = exports.extend = function extend() {
var extended = {}; var extended = {};
var deep = false;
var length = arguments.length; var length = arguments.length;
/** /**
@ -4661,7 +4668,7 @@
for (var prop in obj) { for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) { if (Object.prototype.hasOwnProperty.call(obj, prop)) {
// If deep merge and property is an object, merge properties // If deep merge and property is an object, merge properties
if (deep && isType('Object', obj[prop])) { if (isType('Object', obj[prop])) {
extended[prop] = extend(true, extended[prop], obj[prop]); extended[prop] = extend(true, extended[prop], obj[prop]);
} else { } else {
extended[prop] = obj[prop]; extended[prop] = obj[prop];
@ -4675,11 +4682,9 @@
// store argument at position i // store argument at position i
var obj = arguments[i]; var obj = arguments[i];
// If we are in fact dealing with an object, merge it. Otherwise throw error // If we are in fact dealing with an object, merge it.
if (isType('Object', obj)) { if (isType('Object', obj)) {
merge(obj); merge(obj);
} else {
console.error('Custom options must be an object');
} }
} }

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -361,12 +361,17 @@ export default class Choices {
* @return {Object} Class instance * @return {Object} Class instance
* @public * @public
*/ */
removeHighlightedItems() { removeHighlightedItems(runCallback = true) {
const items = this.store.getItemsFilteredByActive(); const items = this.store.getItemsFilteredByActive();
items.forEach((item) => { items.forEach((item) => {
if (item.highlighted && item.active) { if (item.highlighted && item.active) {
this._removeItem(item); this._removeItem(item);
// If this action was performed by the user
// run the callback
if (runCallback) {
this._triggerChange(item.value);
}
} }
}); });
@ -814,7 +819,7 @@ export default class Choices {
if (!hasHighlightedItems) { if (!hasHighlightedItems) {
this.highlightItem(lastItem); this.highlightItem(lastItem);
} }
this.removeHighlightedItems(); this.removeHighlightedItems(true);
} }
} }
} }