This commit is contained in:
Xon 2024-09-07 01:14:18 +00:00
commit ad2d261352
17 changed files with 516 additions and 413 deletions

View file

@ -1,4 +1,4 @@
/*! choices.js v11.0.1 | © 2024 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
/*! choices.js v11.0.2 | © 2024 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
@ -135,7 +135,6 @@
highlighted: highlighted,
}); };
/* eslint-disable @typescript-eslint/no-explicit-any */
var getRandomNumber = function (min, max) { return Math.floor(Math.random() * (max - min) + min); };
var generateChars = function (length) {
return Array.from({ length: length }, function () { return getRandomNumber(0, 36).toString(36); }).join('');
@ -268,6 +267,7 @@
/**
* Returns an array of keys present on the first but missing on the second object
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var diff = function (a, b) {
var aKeys = Object.keys(a).sort();
var bKeys = Object.keys(b).sort();
@ -773,7 +773,7 @@
var choice = groupOrChoice;
var result = {
id: 0, // actual ID will be assigned during _addChoice
groupId: 0, // actual ID will be assigned during _addGroup but before _addChoice
group: null, // actual group will be assigned during _addGroup but before _addChoice
score: 0, // used in search
rank: 0, // used in search, stable sort order
value: choice.value,
@ -850,7 +850,7 @@
}
return {
id: 0,
groupId: 0,
group: null,
score: 0,
rank: 0,
value: option.value,
@ -1011,8 +1011,8 @@
break;
}
case ActionType.REMOVE_CHOICE: {
state = state.filter(function (item) { return item.id !== action.choice.id; });
removeItem(action.choice);
state = state.filter(function (item) { return item.id !== action.choice.id; });
break;
}
case ActionType.HIGHLIGHT_ITEM: {
@ -1060,16 +1060,14 @@
var update = true;
switch (action.type) {
case ActionType.ADD_CHOICE: {
/*
A disabled choice appears in the choice dropdown but cannot be selected
A selected choice has been added to the passed input's value (added as an item)
An active choice appears within the choice dropdown
*/
state.push(action.choice);
break;
}
case ActionType.REMOVE_CHOICE: {
action.choice.choiceEl = undefined;
if (action.choice.group) {
action.choice.group.choices = action.choice.group.choices.filter(function (obj) { return obj.id !== action.choice.id; });
}
state = state.filter(function (obj) { return obj.id !== action.choice.id; });
break;
}
@ -1167,6 +1165,7 @@
};
Store.prototype.subscribe = function (onChange) {
this._listeners.push(onChange);
return this;
};
Store.prototype.dispatch = function (action) {
var _this = this;
@ -3172,7 +3171,6 @@
label = escapeForTemplate(allowHTML, label);
label += " (".concat(groupName, ")");
label = { trusted: label };
div.dataset.groupId = "".concat(choice.groupId);
}
var describedBy = div;
if (choice.labelClass) {
@ -3200,13 +3198,16 @@
if (choice.placeholder) {
addClassesToElement(div, placeholder);
}
div.setAttribute('role', choice.groupId ? 'treeitem' : 'option');
div.setAttribute('role', choice.group ? 'treeitem' : 'option');
div.dataset.choice = '';
div.dataset.id = choice.id;
div.dataset.value = rawValue;
if (selectText) {
div.dataset.selectText = selectText;
}
if (choice.group) {
div.dataset.groupId = "".concat(choice.group.id);
}
assignCustomProperties(div, choice, false);
if (choice.disabled) {
addClassesToElement(div, itemDisabled);
@ -3493,7 +3494,7 @@
this.passedElement.reveal();
this.containerOuter.unwrap(this.passedElement.element);
this._store._listeners = []; // prevents select/input value being wiped
this.clearStore();
this.clearStore(false);
this._stopSearch();
this._templates = Choices.defaults.templates;
this.initialised = false;
@ -3547,7 +3548,7 @@
}
this._store.dispatch(highlightItem(choice, false));
if (runEvent) {
this.passedElement.triggerEvent(EventType.highlightItem, this._getChoiceForOutput(choice));
this.passedElement.triggerEvent(EventType.unhighlightItem, this._getChoiceForOutput(choice));
}
return this;
};
@ -3641,12 +3642,9 @@
};
Choices.prototype.getValue = function (valueOnly) {
var _this = this;
if (valueOnly === void 0) { valueOnly = false; }
var values = this._store.items.reduce(function (selectedItems, item) {
var itemValue = valueOnly ? item.value : _this._getChoiceForOutput(item);
selectedItems.push(itemValue);
return selectedItems;
}, []);
var values = this._store.items.map(function (item) {
return (valueOnly ? item.value : _this._getChoiceForOutput(item));
});
return this._isSelectOneElement || this.config.singleModeForMultiSelect ? values[0] : values;
};
Choices.prototype.setValue = function (items) {
@ -3749,12 +3747,13 @@
* }], 'value', 'label', false);
* ```
*/
Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices) {
Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices, clearSearchFlag) {
var _this = this;
if (choicesArrayOrFetcher === void 0) { choicesArrayOrFetcher = []; }
if (value === void 0) { value = 'value'; }
if (label === void 0) { label = 'label'; }
if (replaceChoices === void 0) { replaceChoices = false; }
if (clearSearchFlag === void 0) { clearSearchFlag = true; }
if (!this.initialisedOK) {
this._warnChoicesInitFailed('setChoices');
return this;
@ -3799,6 +3798,9 @@
}
this.containerOuter.removeLoadingState();
this._store.withTxn(function () {
if (clearSearchFlag) {
_this._isSearching = false;
}
var isDefaultValue = value === 'value';
var isDefaultLabel = label === 'label';
choicesArrayOrFetcher.forEach(function (groupOrChoice) {
@ -3845,18 +3847,21 @@
}
});
}
_this.clearStore();
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
return;
}
var choice = groupOrChoice;
_this.clearStore(false);
var updateChoice = function (choice) {
if (deselectAll) {
_this._store.dispatch(removeItem$1(choice));
}
else if (existingItems[choice.value]) {
choice.selected = true;
}
};
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
groupOrChoice.choices.forEach(updateChoice);
return;
}
updateChoice(groupOrChoice);
});
/* @todo only generate add events for the added options instead of all
if (withEvents) {
@ -3884,6 +3889,7 @@
if (!choice) {
return this;
}
this._clearNotice();
this._store.dispatch(removeChoice(choice));
// @todo integrate with Store
this._searcher.reset();
@ -3893,13 +3899,26 @@
return this;
};
Choices.prototype.clearChoices = function () {
this.passedElement.element.replaceChildren('');
return this.clearStore();
var _this = this;
this._store.withTxn(function () {
_this._store.choices.forEach(function (choice) {
if (!choice.selected) {
_this._store.dispatch(removeChoice(choice));
}
});
});
// @todo integrate with Store
this._searcher.reset();
return this;
};
Choices.prototype.clearStore = function () {
Choices.prototype.clearStore = function (clearOptions) {
if (clearOptions === void 0) { clearOptions = true; }
this._stopSearch();
if (clearOptions) {
this.passedElement.element.replaceChildren('');
}
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._clearNotice();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
@ -3910,10 +3929,7 @@
Choices.prototype.clearInput = function () {
var shouldSetInputWidth = !this._isSelectOneElement;
this.input.clear(shouldSetInputWidth);
this._clearNotice();
if (this._isSearching) {
this._stopSearch();
}
this._stopSearch();
return this;
};
Choices.prototype._validateConfig = function () {
@ -3971,7 +3987,7 @@
return !choice.placeholder && (isSearching ? !!choice.rank : config.renderSelectedChoices || !choice.selected);
});
};
var selectableChoices = this._isSelectOneElement;
var selectableChoices = false;
var renderChoices = function (choices, withinGroup, groupLabel) {
if (isSearching) {
// sortByRank is used to ensure stable sorting, as scores are non-unique
@ -3989,7 +4005,7 @@
var dropdownItem = choice.choiceEl || _this._templates.choice(config, choice, config.itemSelectText, groupLabel);
choice.choiceEl = dropdownItem;
fragment.appendChild(dropdownItem);
if (isSearching || !choice.selected) {
if (!choice.disabled && (isSearching || !choice.selected)) {
selectableChoices = true;
}
return index < choiceLimit;
@ -4001,13 +4017,16 @@
}
if (!this._hasNonChoicePlaceholder && !isSearching && this._isSelectOneElement) {
// If we have a placeholder choice along with groups
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.groupId; }), false, undefined);
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.group; }), false, undefined);
}
// If we have grouped options
if (activeGroups.length && !isSearching) {
if (config.shouldSort) {
activeGroups.sort(config.sorter);
}
// render Choices without group first, regardless of sort, otherwise they won't be distinguishable
// from the last group
renderChoices(activeChoices.filter(function (choice) { return !choice.placeholder && !choice.group; }), false, undefined);
activeGroups.forEach(function (group) {
var groupChoices = renderableChoices(group.choices);
if (groupChoices.length) {
@ -4025,19 +4044,15 @@
renderChoices(renderableChoices(activeChoices), false, undefined);
}
}
var notice = this._notice;
if (!selectableChoices) {
if (!notice) {
if (!this._notice) {
this._notice = {
text: resolveStringFunction(config.noChoicesText),
type: NoticeTypes.noChoices,
text: resolveStringFunction(isSearching ? config.noResultsText : config.noChoicesText),
type: isSearching ? NoticeTypes.noResults : NoticeTypes.noChoices,
};
}
fragment.replaceChildren('');
}
else if (notice && notice.type === NoticeTypes.noChoices) {
this._notice = undefined;
}
this._renderNotice(fragment);
this.choiceList.element.replaceChildren(fragment);
if (selectableChoices) {
@ -4152,11 +4167,8 @@
}
}
};
// eslint-disable-next-line class-methods-use-this
Choices.prototype._getChoiceForOutput = function (choice, keyCode) {
if (!choice) {
return undefined;
}
var group = choice.groupId ? this._store.getGroupById(choice.groupId) : null;
return {
id: choice.id,
highlighted: choice.highlighted,
@ -4168,7 +4180,7 @@
label: choice.label,
placeholder: choice.placeholder,
value: choice.value,
groupValue: group && group.label ? group.label : undefined,
groupValue: choice.group ? choice.group.label : undefined,
element: choice.element,
keyCode: keyCode,
};
@ -4187,7 +4199,7 @@
if (!items.length || !this.config.removeItems || !this.config.removeItemButton) {
return;
}
var id = element && parseDataSetId(element.parentNode);
var id = element && parseDataSetId(element.parentElement);
var itemToRemove = id && items.find(function (item) { return item.id === id; });
if (!itemToRemove) {
return;
@ -4420,7 +4432,7 @@
if (!results.length) {
this._displayNotice(resolveStringFunction(this.config.noResultsText), NoticeTypes.noResults);
}
else if (noticeType === NoticeTypes.noResults) {
else {
this._clearNotice();
}
}
@ -4428,10 +4440,10 @@
return results.length;
};
Choices.prototype._stopSearch = function () {
var wasSearching = this._isSearching;
this._currentValue = '';
this._isSearching = false;
if (wasSearching) {
if (this._isSearching) {
this._currentValue = '';
this._isSearching = false;
this._clearNotice();
this._store.dispatch(activateChoices(true));
this.passedElement.triggerEvent(EventType.search, {
value: '',
@ -4583,7 +4595,6 @@
else {
this._stopSearch();
}
this._clearNotice();
return;
}
if (!this._canAddItems()) {
@ -4955,11 +4966,16 @@
if (choice.id) {
throw new TypeError('Can not re-add a choice which has already been added');
}
var config = this.config;
if ((this._isSelectElement || !config.duplicateItemsAllowed) &&
this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
return;
}
// Generate unique id, in-place update is required so chaining _addItem works as expected
this._lastAddedChoiceId++;
choice.id = this._lastAddedChoiceId;
choice.elementId = "".concat(this._baseId, "-").concat(this._idNames.itemChoice, "-").concat(choice.id);
var _a = this.config, prependValue = _a.prependValue, appendValue = _a.appendValue;
var prependValue = config.prependValue, appendValue = config.appendValue;
if (prependValue) {
choice.value = prependValue + choice.value;
}
@ -4969,6 +4985,7 @@
if ((prependValue || appendValue) && choice.element) {
choice.element.value = choice.value;
}
this._clearNotice();
this._store.dispatch(addChoice(choice));
if (choice.selected) {
this._addItem(choice, withEvents, userTriggered);
@ -4988,7 +5005,7 @@
this._lastAddedGroupId++;
group.id = this._lastAddedGroupId;
group.choices.forEach(function (item) {
item.groupId = group.id;
item.group = group;
if (group.disabled) {
item.disabled = true;
}
@ -5000,7 +5017,7 @@
var callbackOnCreateTemplates = this.config.callbackOnCreateTemplates;
var userTemplates = {};
if (typeof callbackOnCreateTemplates === 'function') {
userTemplates = callbackOnCreateTemplates.call(this, strToEl, escapeForTemplate);
userTemplates = callbackOnCreateTemplates.call(this, strToEl, escapeForTemplate, getClassNames);
}
var templating = {};
Object.keys(this._templates).forEach(function (name) {
@ -5081,12 +5098,11 @@
};
Choices.prototype._initStore = function () {
var _this = this;
this._store.subscribe(this._render);
this._store.withTxn(function () {
this._store.subscribe(this._render).withTxn(function () {
_this._addPredefinedChoices(_this._presetChoices, _this._isSelectOneElement && !_this._hasNonChoicePlaceholder, false);
});
if (this._isSelectOneElement && this._hasNonChoicePlaceholder) {
this._render({ choices: false, groups: false, items: true });
if (!this._store.choices.length || (this._isSelectOneElement && this._hasNonChoicePlaceholder)) {
this._render();
}
};
Choices.prototype._addPredefinedChoices = function (choices, selectFirstOption, withEvents) {
@ -5158,7 +5174,7 @@
throw new TypeError("".concat(caller, " called for an element which has multiple instances of Choices initialised on it"));
}
};
Choices.version = '11.0.1';
Choices.version = '11.0.2';
return Choices;
}());

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,4 @@
/*! choices.js v11.0.1 | © 2024 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
/*! choices.js v11.0.2 | © 2024 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
/******************************************************************************
Copyright (c) Microsoft Corporation.
@ -129,7 +129,6 @@ var highlightItem = function (item, highlighted) { return ({
highlighted: highlighted,
}); };
/* eslint-disable @typescript-eslint/no-explicit-any */
var getRandomNumber = function (min, max) { return Math.floor(Math.random() * (max - min) + min); };
var generateChars = function (length) {
return Array.from({ length: length }, function () { return getRandomNumber(0, 36).toString(36); }).join('');
@ -262,6 +261,7 @@ var dispatchEvent = function (element, type, customArgs) {
/**
* Returns an array of keys present on the first but missing on the second object
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var diff = function (a, b) {
var aKeys = Object.keys(a).sort();
var bKeys = Object.keys(b).sort();
@ -767,7 +767,7 @@ var mapInputToChoice = function (value, allowGroup) {
var choice = groupOrChoice;
var result = {
id: 0, // actual ID will be assigned during _addChoice
groupId: 0, // actual ID will be assigned during _addGroup but before _addChoice
group: null, // actual group will be assigned during _addGroup but before _addChoice
score: 0, // used in search
rank: 0, // used in search, stable sort order
value: choice.value,
@ -844,7 +844,7 @@ var WrappedSelect = /** @class */ (function (_super) {
}
return {
id: 0,
groupId: 0,
group: null,
score: 0,
rank: 0,
value: option.value,
@ -1005,8 +1005,8 @@ function items(s, action, context) {
break;
}
case ActionType.REMOVE_CHOICE: {
state = state.filter(function (item) { return item.id !== action.choice.id; });
removeItem(action.choice);
state = state.filter(function (item) { return item.id !== action.choice.id; });
break;
}
case ActionType.HIGHLIGHT_ITEM: {
@ -1054,16 +1054,14 @@ function choices(s, action, context) {
var update = true;
switch (action.type) {
case ActionType.ADD_CHOICE: {
/*
A disabled choice appears in the choice dropdown but cannot be selected
A selected choice has been added to the passed input's value (added as an item)
An active choice appears within the choice dropdown
*/
state.push(action.choice);
break;
}
case ActionType.REMOVE_CHOICE: {
action.choice.choiceEl = undefined;
if (action.choice.group) {
action.choice.group.choices = action.choice.group.choices.filter(function (obj) { return obj.id !== action.choice.id; });
}
state = state.filter(function (obj) { return obj.id !== action.choice.id; });
break;
}
@ -1161,6 +1159,7 @@ var Store = /** @class */ (function () {
};
Store.prototype.subscribe = function (onChange) {
this._listeners.push(onChange);
return this;
};
Store.prototype.dispatch = function (action) {
var _this = this;
@ -3166,7 +3165,6 @@ var templates = {
label = escapeForTemplate(allowHTML, label);
label += " (".concat(groupName, ")");
label = { trusted: label };
div.dataset.groupId = "".concat(choice.groupId);
}
var describedBy = div;
if (choice.labelClass) {
@ -3194,13 +3192,16 @@ var templates = {
if (choice.placeholder) {
addClassesToElement(div, placeholder);
}
div.setAttribute('role', choice.groupId ? 'treeitem' : 'option');
div.setAttribute('role', choice.group ? 'treeitem' : 'option');
div.dataset.choice = '';
div.dataset.id = choice.id;
div.dataset.value = rawValue;
if (selectText) {
div.dataset.selectText = selectText;
}
if (choice.group) {
div.dataset.groupId = "".concat(choice.group.id);
}
assignCustomProperties(div, choice, false);
if (choice.disabled) {
addClassesToElement(div, itemDisabled);
@ -3487,7 +3488,7 @@ var Choices = /** @class */ (function () {
this.passedElement.reveal();
this.containerOuter.unwrap(this.passedElement.element);
this._store._listeners = []; // prevents select/input value being wiped
this.clearStore();
this.clearStore(false);
this._stopSearch();
this._templates = Choices.defaults.templates;
this.initialised = false;
@ -3541,7 +3542,7 @@ var Choices = /** @class */ (function () {
}
this._store.dispatch(highlightItem(choice, false));
if (runEvent) {
this.passedElement.triggerEvent(EventType.highlightItem, this._getChoiceForOutput(choice));
this.passedElement.triggerEvent(EventType.unhighlightItem, this._getChoiceForOutput(choice));
}
return this;
};
@ -3635,12 +3636,9 @@ var Choices = /** @class */ (function () {
};
Choices.prototype.getValue = function (valueOnly) {
var _this = this;
if (valueOnly === void 0) { valueOnly = false; }
var values = this._store.items.reduce(function (selectedItems, item) {
var itemValue = valueOnly ? item.value : _this._getChoiceForOutput(item);
selectedItems.push(itemValue);
return selectedItems;
}, []);
var values = this._store.items.map(function (item) {
return (valueOnly ? item.value : _this._getChoiceForOutput(item));
});
return this._isSelectOneElement || this.config.singleModeForMultiSelect ? values[0] : values;
};
Choices.prototype.setValue = function (items) {
@ -3743,12 +3741,13 @@ var Choices = /** @class */ (function () {
* }], 'value', 'label', false);
* ```
*/
Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices) {
Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices, clearSearchFlag) {
var _this = this;
if (choicesArrayOrFetcher === void 0) { choicesArrayOrFetcher = []; }
if (value === void 0) { value = 'value'; }
if (label === void 0) { label = 'label'; }
if (replaceChoices === void 0) { replaceChoices = false; }
if (clearSearchFlag === void 0) { clearSearchFlag = true; }
if (!this.initialisedOK) {
this._warnChoicesInitFailed('setChoices');
return this;
@ -3793,6 +3792,9 @@ var Choices = /** @class */ (function () {
}
this.containerOuter.removeLoadingState();
this._store.withTxn(function () {
if (clearSearchFlag) {
_this._isSearching = false;
}
var isDefaultValue = value === 'value';
var isDefaultLabel = label === 'label';
choicesArrayOrFetcher.forEach(function (groupOrChoice) {
@ -3839,18 +3841,21 @@ var Choices = /** @class */ (function () {
}
});
}
_this.clearStore();
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
return;
}
var choice = groupOrChoice;
_this.clearStore(false);
var updateChoice = function (choice) {
if (deselectAll) {
_this._store.dispatch(removeItem$1(choice));
}
else if (existingItems[choice.value]) {
choice.selected = true;
}
};
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
groupOrChoice.choices.forEach(updateChoice);
return;
}
updateChoice(groupOrChoice);
});
/* @todo only generate add events for the added options instead of all
if (withEvents) {
@ -3878,6 +3883,7 @@ var Choices = /** @class */ (function () {
if (!choice) {
return this;
}
this._clearNotice();
this._store.dispatch(removeChoice(choice));
// @todo integrate with Store
this._searcher.reset();
@ -3887,13 +3893,26 @@ var Choices = /** @class */ (function () {
return this;
};
Choices.prototype.clearChoices = function () {
this.passedElement.element.replaceChildren('');
return this.clearStore();
var _this = this;
this._store.withTxn(function () {
_this._store.choices.forEach(function (choice) {
if (!choice.selected) {
_this._store.dispatch(removeChoice(choice));
}
});
});
// @todo integrate with Store
this._searcher.reset();
return this;
};
Choices.prototype.clearStore = function () {
Choices.prototype.clearStore = function (clearOptions) {
if (clearOptions === void 0) { clearOptions = true; }
this._stopSearch();
if (clearOptions) {
this.passedElement.element.replaceChildren('');
}
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._clearNotice();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
@ -3904,10 +3923,7 @@ var Choices = /** @class */ (function () {
Choices.prototype.clearInput = function () {
var shouldSetInputWidth = !this._isSelectOneElement;
this.input.clear(shouldSetInputWidth);
this._clearNotice();
if (this._isSearching) {
this._stopSearch();
}
this._stopSearch();
return this;
};
Choices.prototype._validateConfig = function () {
@ -3965,7 +3981,7 @@ var Choices = /** @class */ (function () {
return !choice.placeholder && (isSearching ? !!choice.rank : config.renderSelectedChoices || !choice.selected);
});
};
var selectableChoices = this._isSelectOneElement;
var selectableChoices = false;
var renderChoices = function (choices, withinGroup, groupLabel) {
if (isSearching) {
// sortByRank is used to ensure stable sorting, as scores are non-unique
@ -3983,7 +3999,7 @@ var Choices = /** @class */ (function () {
var dropdownItem = choice.choiceEl || _this._templates.choice(config, choice, config.itemSelectText, groupLabel);
choice.choiceEl = dropdownItem;
fragment.appendChild(dropdownItem);
if (isSearching || !choice.selected) {
if (!choice.disabled && (isSearching || !choice.selected)) {
selectableChoices = true;
}
return index < choiceLimit;
@ -3995,13 +4011,16 @@ var Choices = /** @class */ (function () {
}
if (!this._hasNonChoicePlaceholder && !isSearching && this._isSelectOneElement) {
// If we have a placeholder choice along with groups
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.groupId; }), false, undefined);
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.group; }), false, undefined);
}
// If we have grouped options
if (activeGroups.length && !isSearching) {
if (config.shouldSort) {
activeGroups.sort(config.sorter);
}
// render Choices without group first, regardless of sort, otherwise they won't be distinguishable
// from the last group
renderChoices(activeChoices.filter(function (choice) { return !choice.placeholder && !choice.group; }), false, undefined);
activeGroups.forEach(function (group) {
var groupChoices = renderableChoices(group.choices);
if (groupChoices.length) {
@ -4019,19 +4038,15 @@ var Choices = /** @class */ (function () {
renderChoices(renderableChoices(activeChoices), false, undefined);
}
}
var notice = this._notice;
if (!selectableChoices) {
if (!notice) {
if (!this._notice) {
this._notice = {
text: resolveStringFunction(config.noChoicesText),
type: NoticeTypes.noChoices,
text: resolveStringFunction(isSearching ? config.noResultsText : config.noChoicesText),
type: isSearching ? NoticeTypes.noResults : NoticeTypes.noChoices,
};
}
fragment.replaceChildren('');
}
else if (notice && notice.type === NoticeTypes.noChoices) {
this._notice = undefined;
}
this._renderNotice(fragment);
this.choiceList.element.replaceChildren(fragment);
if (selectableChoices) {
@ -4146,11 +4161,8 @@ var Choices = /** @class */ (function () {
}
}
};
// eslint-disable-next-line class-methods-use-this
Choices.prototype._getChoiceForOutput = function (choice, keyCode) {
if (!choice) {
return undefined;
}
var group = choice.groupId ? this._store.getGroupById(choice.groupId) : null;
return {
id: choice.id,
highlighted: choice.highlighted,
@ -4162,7 +4174,7 @@ var Choices = /** @class */ (function () {
label: choice.label,
placeholder: choice.placeholder,
value: choice.value,
groupValue: group && group.label ? group.label : undefined,
groupValue: choice.group ? choice.group.label : undefined,
element: choice.element,
keyCode: keyCode,
};
@ -4181,7 +4193,7 @@ var Choices = /** @class */ (function () {
if (!items.length || !this.config.removeItems || !this.config.removeItemButton) {
return;
}
var id = element && parseDataSetId(element.parentNode);
var id = element && parseDataSetId(element.parentElement);
var itemToRemove = id && items.find(function (item) { return item.id === id; });
if (!itemToRemove) {
return;
@ -4414,7 +4426,7 @@ var Choices = /** @class */ (function () {
if (!results.length) {
this._displayNotice(resolveStringFunction(this.config.noResultsText), NoticeTypes.noResults);
}
else if (noticeType === NoticeTypes.noResults) {
else {
this._clearNotice();
}
}
@ -4422,10 +4434,10 @@ var Choices = /** @class */ (function () {
return results.length;
};
Choices.prototype._stopSearch = function () {
var wasSearching = this._isSearching;
this._currentValue = '';
this._isSearching = false;
if (wasSearching) {
if (this._isSearching) {
this._currentValue = '';
this._isSearching = false;
this._clearNotice();
this._store.dispatch(activateChoices(true));
this.passedElement.triggerEvent(EventType.search, {
value: '',
@ -4577,7 +4589,6 @@ var Choices = /** @class */ (function () {
else {
this._stopSearch();
}
this._clearNotice();
return;
}
if (!this._canAddItems()) {
@ -4949,11 +4960,16 @@ var Choices = /** @class */ (function () {
if (choice.id) {
throw new TypeError('Can not re-add a choice which has already been added');
}
var config = this.config;
if ((this._isSelectElement || !config.duplicateItemsAllowed) &&
this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
return;
}
// Generate unique id, in-place update is required so chaining _addItem works as expected
this._lastAddedChoiceId++;
choice.id = this._lastAddedChoiceId;
choice.elementId = "".concat(this._baseId, "-").concat(this._idNames.itemChoice, "-").concat(choice.id);
var _a = this.config, prependValue = _a.prependValue, appendValue = _a.appendValue;
var prependValue = config.prependValue, appendValue = config.appendValue;
if (prependValue) {
choice.value = prependValue + choice.value;
}
@ -4963,6 +4979,7 @@ var Choices = /** @class */ (function () {
if ((prependValue || appendValue) && choice.element) {
choice.element.value = choice.value;
}
this._clearNotice();
this._store.dispatch(addChoice(choice));
if (choice.selected) {
this._addItem(choice, withEvents, userTriggered);
@ -4982,7 +4999,7 @@ var Choices = /** @class */ (function () {
this._lastAddedGroupId++;
group.id = this._lastAddedGroupId;
group.choices.forEach(function (item) {
item.groupId = group.id;
item.group = group;
if (group.disabled) {
item.disabled = true;
}
@ -4994,7 +5011,7 @@ var Choices = /** @class */ (function () {
var callbackOnCreateTemplates = this.config.callbackOnCreateTemplates;
var userTemplates = {};
if (typeof callbackOnCreateTemplates === 'function') {
userTemplates = callbackOnCreateTemplates.call(this, strToEl, escapeForTemplate);
userTemplates = callbackOnCreateTemplates.call(this, strToEl, escapeForTemplate, getClassNames);
}
var templating = {};
Object.keys(this._templates).forEach(function (name) {
@ -5075,12 +5092,11 @@ var Choices = /** @class */ (function () {
};
Choices.prototype._initStore = function () {
var _this = this;
this._store.subscribe(this._render);
this._store.withTxn(function () {
this._store.subscribe(this._render).withTxn(function () {
_this._addPredefinedChoices(_this._presetChoices, _this._isSelectOneElement && !_this._hasNonChoicePlaceholder, false);
});
if (this._isSelectOneElement && this._hasNonChoicePlaceholder) {
this._render({ choices: false, groups: false, items: true });
if (!this._store.choices.length || (this._isSelectOneElement && this._hasNonChoicePlaceholder)) {
this._render();
}
};
Choices.prototype._addPredefinedChoices = function (choices, selectFirstOption, withEvents) {
@ -5152,7 +5168,7 @@ var Choices = /** @class */ (function () {
throw new TypeError("".concat(caller, " called for an element which has multiple instances of Choices initialised on it"));
}
};
Choices.version = '11.0.1';
Choices.version = '11.0.2';
return Choices;
}());

View file

@ -1,4 +1,4 @@
/*! choices.js v11.0.1 | © 2024 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
/*! choices.js v11.0.2 | © 2024 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
@ -135,7 +135,6 @@
highlighted: highlighted,
}); };
/* eslint-disable @typescript-eslint/no-explicit-any */
var getRandomNumber = function (min, max) { return Math.floor(Math.random() * (max - min) + min); };
var generateChars = function (length) {
return Array.from({ length: length }, function () { return getRandomNumber(0, 36).toString(36); }).join('');
@ -268,6 +267,7 @@
/**
* Returns an array of keys present on the first but missing on the second object
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var diff = function (a, b) {
var aKeys = Object.keys(a).sort();
var bKeys = Object.keys(b).sort();
@ -773,7 +773,7 @@
var choice = groupOrChoice;
var result = {
id: 0, // actual ID will be assigned during _addChoice
groupId: 0, // actual ID will be assigned during _addGroup but before _addChoice
group: null, // actual group will be assigned during _addGroup but before _addChoice
score: 0, // used in search
rank: 0, // used in search, stable sort order
value: choice.value,
@ -850,7 +850,7 @@
}
return {
id: 0,
groupId: 0,
group: null,
score: 0,
rank: 0,
value: option.value,
@ -1011,8 +1011,8 @@
break;
}
case ActionType.REMOVE_CHOICE: {
state = state.filter(function (item) { return item.id !== action.choice.id; });
removeItem(action.choice);
state = state.filter(function (item) { return item.id !== action.choice.id; });
break;
}
case ActionType.HIGHLIGHT_ITEM: {
@ -1060,16 +1060,14 @@
var update = true;
switch (action.type) {
case ActionType.ADD_CHOICE: {
/*
A disabled choice appears in the choice dropdown but cannot be selected
A selected choice has been added to the passed input's value (added as an item)
An active choice appears within the choice dropdown
*/
state.push(action.choice);
break;
}
case ActionType.REMOVE_CHOICE: {
action.choice.choiceEl = undefined;
if (action.choice.group) {
action.choice.group.choices = action.choice.group.choices.filter(function (obj) { return obj.id !== action.choice.id; });
}
state = state.filter(function (obj) { return obj.id !== action.choice.id; });
break;
}
@ -1167,6 +1165,7 @@
};
Store.prototype.subscribe = function (onChange) {
this._listeners.push(onChange);
return this;
};
Store.prototype.dispatch = function (action) {
var _this = this;
@ -2690,7 +2689,6 @@
label = escapeForTemplate(allowHTML, label);
label += " (".concat(groupName, ")");
label = { trusted: label };
div.dataset.groupId = "".concat(choice.groupId);
}
var describedBy = div;
if (choice.labelClass) {
@ -2718,13 +2716,16 @@
if (choice.placeholder) {
addClassesToElement(div, placeholder);
}
div.setAttribute('role', choice.groupId ? 'treeitem' : 'option');
div.setAttribute('role', choice.group ? 'treeitem' : 'option');
div.dataset.choice = '';
div.dataset.id = choice.id;
div.dataset.value = rawValue;
if (selectText) {
div.dataset.selectText = selectText;
}
if (choice.group) {
div.dataset.groupId = "".concat(choice.group.id);
}
assignCustomProperties(div, choice, false);
if (choice.disabled) {
addClassesToElement(div, itemDisabled);
@ -3011,7 +3012,7 @@
this.passedElement.reveal();
this.containerOuter.unwrap(this.passedElement.element);
this._store._listeners = []; // prevents select/input value being wiped
this.clearStore();
this.clearStore(false);
this._stopSearch();
this._templates = Choices.defaults.templates;
this.initialised = false;
@ -3065,7 +3066,7 @@
}
this._store.dispatch(highlightItem(choice, false));
if (runEvent) {
this.passedElement.triggerEvent(EventType.highlightItem, this._getChoiceForOutput(choice));
this.passedElement.triggerEvent(EventType.unhighlightItem, this._getChoiceForOutput(choice));
}
return this;
};
@ -3159,12 +3160,9 @@
};
Choices.prototype.getValue = function (valueOnly) {
var _this = this;
if (valueOnly === void 0) { valueOnly = false; }
var values = this._store.items.reduce(function (selectedItems, item) {
var itemValue = valueOnly ? item.value : _this._getChoiceForOutput(item);
selectedItems.push(itemValue);
return selectedItems;
}, []);
var values = this._store.items.map(function (item) {
return (valueOnly ? item.value : _this._getChoiceForOutput(item));
});
return this._isSelectOneElement || this.config.singleModeForMultiSelect ? values[0] : values;
};
Choices.prototype.setValue = function (items) {
@ -3267,12 +3265,13 @@
* }], 'value', 'label', false);
* ```
*/
Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices) {
Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices, clearSearchFlag) {
var _this = this;
if (choicesArrayOrFetcher === void 0) { choicesArrayOrFetcher = []; }
if (value === void 0) { value = 'value'; }
if (label === void 0) { label = 'label'; }
if (replaceChoices === void 0) { replaceChoices = false; }
if (clearSearchFlag === void 0) { clearSearchFlag = true; }
if (!this.initialisedOK) {
this._warnChoicesInitFailed('setChoices');
return this;
@ -3317,6 +3316,9 @@
}
this.containerOuter.removeLoadingState();
this._store.withTxn(function () {
if (clearSearchFlag) {
_this._isSearching = false;
}
var isDefaultValue = value === 'value';
var isDefaultLabel = label === 'label';
choicesArrayOrFetcher.forEach(function (groupOrChoice) {
@ -3363,18 +3365,21 @@
}
});
}
_this.clearStore();
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
return;
}
var choice = groupOrChoice;
_this.clearStore(false);
var updateChoice = function (choice) {
if (deselectAll) {
_this._store.dispatch(removeItem$1(choice));
}
else if (existingItems[choice.value]) {
choice.selected = true;
}
};
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
groupOrChoice.choices.forEach(updateChoice);
return;
}
updateChoice(groupOrChoice);
});
/* @todo only generate add events for the added options instead of all
if (withEvents) {
@ -3402,6 +3407,7 @@
if (!choice) {
return this;
}
this._clearNotice();
this._store.dispatch(removeChoice(choice));
// @todo integrate with Store
this._searcher.reset();
@ -3411,13 +3417,26 @@
return this;
};
Choices.prototype.clearChoices = function () {
this.passedElement.element.replaceChildren('');
return this.clearStore();
var _this = this;
this._store.withTxn(function () {
_this._store.choices.forEach(function (choice) {
if (!choice.selected) {
_this._store.dispatch(removeChoice(choice));
}
});
});
// @todo integrate with Store
this._searcher.reset();
return this;
};
Choices.prototype.clearStore = function () {
Choices.prototype.clearStore = function (clearOptions) {
if (clearOptions === void 0) { clearOptions = true; }
this._stopSearch();
if (clearOptions) {
this.passedElement.element.replaceChildren('');
}
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._clearNotice();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
@ -3428,10 +3447,7 @@
Choices.prototype.clearInput = function () {
var shouldSetInputWidth = !this._isSelectOneElement;
this.input.clear(shouldSetInputWidth);
this._clearNotice();
if (this._isSearching) {
this._stopSearch();
}
this._stopSearch();
return this;
};
Choices.prototype._validateConfig = function () {
@ -3489,7 +3505,7 @@
return !choice.placeholder && (isSearching ? !!choice.rank : config.renderSelectedChoices || !choice.selected);
});
};
var selectableChoices = this._isSelectOneElement;
var selectableChoices = false;
var renderChoices = function (choices, withinGroup, groupLabel) {
if (isSearching) {
// sortByRank is used to ensure stable sorting, as scores are non-unique
@ -3507,7 +3523,7 @@
var dropdownItem = choice.choiceEl || _this._templates.choice(config, choice, config.itemSelectText, groupLabel);
choice.choiceEl = dropdownItem;
fragment.appendChild(dropdownItem);
if (isSearching || !choice.selected) {
if (!choice.disabled && (isSearching || !choice.selected)) {
selectableChoices = true;
}
return index < choiceLimit;
@ -3519,13 +3535,16 @@
}
if (!this._hasNonChoicePlaceholder && !isSearching && this._isSelectOneElement) {
// If we have a placeholder choice along with groups
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.groupId; }), false, undefined);
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.group; }), false, undefined);
}
// If we have grouped options
if (activeGroups.length && !isSearching) {
if (config.shouldSort) {
activeGroups.sort(config.sorter);
}
// render Choices without group first, regardless of sort, otherwise they won't be distinguishable
// from the last group
renderChoices(activeChoices.filter(function (choice) { return !choice.placeholder && !choice.group; }), false, undefined);
activeGroups.forEach(function (group) {
var groupChoices = renderableChoices(group.choices);
if (groupChoices.length) {
@ -3543,19 +3562,15 @@
renderChoices(renderableChoices(activeChoices), false, undefined);
}
}
var notice = this._notice;
if (!selectableChoices) {
if (!notice) {
if (!this._notice) {
this._notice = {
text: resolveStringFunction(config.noChoicesText),
type: NoticeTypes.noChoices,
text: resolveStringFunction(isSearching ? config.noResultsText : config.noChoicesText),
type: isSearching ? NoticeTypes.noResults : NoticeTypes.noChoices,
};
}
fragment.replaceChildren('');
}
else if (notice && notice.type === NoticeTypes.noChoices) {
this._notice = undefined;
}
this._renderNotice(fragment);
this.choiceList.element.replaceChildren(fragment);
if (selectableChoices) {
@ -3670,11 +3685,8 @@
}
}
};
// eslint-disable-next-line class-methods-use-this
Choices.prototype._getChoiceForOutput = function (choice, keyCode) {
if (!choice) {
return undefined;
}
var group = choice.groupId ? this._store.getGroupById(choice.groupId) : null;
return {
id: choice.id,
highlighted: choice.highlighted,
@ -3686,7 +3698,7 @@
label: choice.label,
placeholder: choice.placeholder,
value: choice.value,
groupValue: group && group.label ? group.label : undefined,
groupValue: choice.group ? choice.group.label : undefined,
element: choice.element,
keyCode: keyCode,
};
@ -3705,7 +3717,7 @@
if (!items.length || !this.config.removeItems || !this.config.removeItemButton) {
return;
}
var id = element && parseDataSetId(element.parentNode);
var id = element && parseDataSetId(element.parentElement);
var itemToRemove = id && items.find(function (item) { return item.id === id; });
if (!itemToRemove) {
return;
@ -3938,7 +3950,7 @@
if (!results.length) {
this._displayNotice(resolveStringFunction(this.config.noResultsText), NoticeTypes.noResults);
}
else if (noticeType === NoticeTypes.noResults) {
else {
this._clearNotice();
}
}
@ -3946,10 +3958,10 @@
return results.length;
};
Choices.prototype._stopSearch = function () {
var wasSearching = this._isSearching;
this._currentValue = '';
this._isSearching = false;
if (wasSearching) {
if (this._isSearching) {
this._currentValue = '';
this._isSearching = false;
this._clearNotice();
this._store.dispatch(activateChoices(true));
this.passedElement.triggerEvent(EventType.search, {
value: '',
@ -4101,7 +4113,6 @@
else {
this._stopSearch();
}
this._clearNotice();
return;
}
if (!this._canAddItems()) {
@ -4473,11 +4484,16 @@
if (choice.id) {
throw new TypeError('Can not re-add a choice which has already been added');
}
var config = this.config;
if ((this._isSelectElement || !config.duplicateItemsAllowed) &&
this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
return;
}
// Generate unique id, in-place update is required so chaining _addItem works as expected
this._lastAddedChoiceId++;
choice.id = this._lastAddedChoiceId;
choice.elementId = "".concat(this._baseId, "-").concat(this._idNames.itemChoice, "-").concat(choice.id);
var _a = this.config, prependValue = _a.prependValue, appendValue = _a.appendValue;
var prependValue = config.prependValue, appendValue = config.appendValue;
if (prependValue) {
choice.value = prependValue + choice.value;
}
@ -4487,6 +4503,7 @@
if ((prependValue || appendValue) && choice.element) {
choice.element.value = choice.value;
}
this._clearNotice();
this._store.dispatch(addChoice(choice));
if (choice.selected) {
this._addItem(choice, withEvents, userTriggered);
@ -4506,7 +4523,7 @@
this._lastAddedGroupId++;
group.id = this._lastAddedGroupId;
group.choices.forEach(function (item) {
item.groupId = group.id;
item.group = group;
if (group.disabled) {
item.disabled = true;
}
@ -4518,7 +4535,7 @@
var callbackOnCreateTemplates = this.config.callbackOnCreateTemplates;
var userTemplates = {};
if (typeof callbackOnCreateTemplates === 'function') {
userTemplates = callbackOnCreateTemplates.call(this, strToEl, escapeForTemplate);
userTemplates = callbackOnCreateTemplates.call(this, strToEl, escapeForTemplate, getClassNames);
}
var templating = {};
Object.keys(this._templates).forEach(function (name) {
@ -4599,12 +4616,11 @@
};
Choices.prototype._initStore = function () {
var _this = this;
this._store.subscribe(this._render);
this._store.withTxn(function () {
this._store.subscribe(this._render).withTxn(function () {
_this._addPredefinedChoices(_this._presetChoices, _this._isSelectOneElement && !_this._hasNonChoicePlaceholder, false);
});
if (this._isSelectOneElement && this._hasNonChoicePlaceholder) {
this._render({ choices: false, groups: false, items: true });
if (!this._store.choices.length || (this._isSelectOneElement && this._hasNonChoicePlaceholder)) {
this._render();
}
};
Choices.prototype._addPredefinedChoices = function (choices, selectFirstOption, withEvents) {
@ -4676,7 +4692,7 @@
throw new TypeError("".concat(caller, " called for an element which has multiple instances of Choices initialised on it"));
}
};
Choices.version = '11.0.1';
Choices.version = '11.0.2';
return Choices;
}());

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,4 @@
/*! choices.js v11.0.1 | © 2024 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
/*! choices.js v11.0.2 | © 2024 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
/******************************************************************************
Copyright (c) Microsoft Corporation.
@ -129,7 +129,6 @@ var highlightItem = function (item, highlighted) { return ({
highlighted: highlighted,
}); };
/* eslint-disable @typescript-eslint/no-explicit-any */
var getRandomNumber = function (min, max) { return Math.floor(Math.random() * (max - min) + min); };
var generateChars = function (length) {
return Array.from({ length: length }, function () { return getRandomNumber(0, 36).toString(36); }).join('');
@ -262,6 +261,7 @@ var dispatchEvent = function (element, type, customArgs) {
/**
* Returns an array of keys present on the first but missing on the second object
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var diff = function (a, b) {
var aKeys = Object.keys(a).sort();
var bKeys = Object.keys(b).sort();
@ -767,7 +767,7 @@ var mapInputToChoice = function (value, allowGroup) {
var choice = groupOrChoice;
var result = {
id: 0, // actual ID will be assigned during _addChoice
groupId: 0, // actual ID will be assigned during _addGroup but before _addChoice
group: null, // actual group will be assigned during _addGroup but before _addChoice
score: 0, // used in search
rank: 0, // used in search, stable sort order
value: choice.value,
@ -844,7 +844,7 @@ var WrappedSelect = /** @class */ (function (_super) {
}
return {
id: 0,
groupId: 0,
group: null,
score: 0,
rank: 0,
value: option.value,
@ -1005,8 +1005,8 @@ function items(s, action, context) {
break;
}
case ActionType.REMOVE_CHOICE: {
state = state.filter(function (item) { return item.id !== action.choice.id; });
removeItem(action.choice);
state = state.filter(function (item) { return item.id !== action.choice.id; });
break;
}
case ActionType.HIGHLIGHT_ITEM: {
@ -1054,16 +1054,14 @@ function choices(s, action, context) {
var update = true;
switch (action.type) {
case ActionType.ADD_CHOICE: {
/*
A disabled choice appears in the choice dropdown but cannot be selected
A selected choice has been added to the passed input's value (added as an item)
An active choice appears within the choice dropdown
*/
state.push(action.choice);
break;
}
case ActionType.REMOVE_CHOICE: {
action.choice.choiceEl = undefined;
if (action.choice.group) {
action.choice.group.choices = action.choice.group.choices.filter(function (obj) { return obj.id !== action.choice.id; });
}
state = state.filter(function (obj) { return obj.id !== action.choice.id; });
break;
}
@ -1161,6 +1159,7 @@ var Store = /** @class */ (function () {
};
Store.prototype.subscribe = function (onChange) {
this._listeners.push(onChange);
return this;
};
Store.prototype.dispatch = function (action) {
var _this = this;
@ -2684,7 +2683,6 @@ var templates = {
label = escapeForTemplate(allowHTML, label);
label += " (".concat(groupName, ")");
label = { trusted: label };
div.dataset.groupId = "".concat(choice.groupId);
}
var describedBy = div;
if (choice.labelClass) {
@ -2712,13 +2710,16 @@ var templates = {
if (choice.placeholder) {
addClassesToElement(div, placeholder);
}
div.setAttribute('role', choice.groupId ? 'treeitem' : 'option');
div.setAttribute('role', choice.group ? 'treeitem' : 'option');
div.dataset.choice = '';
div.dataset.id = choice.id;
div.dataset.value = rawValue;
if (selectText) {
div.dataset.selectText = selectText;
}
if (choice.group) {
div.dataset.groupId = "".concat(choice.group.id);
}
assignCustomProperties(div, choice, false);
if (choice.disabled) {
addClassesToElement(div, itemDisabled);
@ -3005,7 +3006,7 @@ var Choices = /** @class */ (function () {
this.passedElement.reveal();
this.containerOuter.unwrap(this.passedElement.element);
this._store._listeners = []; // prevents select/input value being wiped
this.clearStore();
this.clearStore(false);
this._stopSearch();
this._templates = Choices.defaults.templates;
this.initialised = false;
@ -3059,7 +3060,7 @@ var Choices = /** @class */ (function () {
}
this._store.dispatch(highlightItem(choice, false));
if (runEvent) {
this.passedElement.triggerEvent(EventType.highlightItem, this._getChoiceForOutput(choice));
this.passedElement.triggerEvent(EventType.unhighlightItem, this._getChoiceForOutput(choice));
}
return this;
};
@ -3153,12 +3154,9 @@ var Choices = /** @class */ (function () {
};
Choices.prototype.getValue = function (valueOnly) {
var _this = this;
if (valueOnly === void 0) { valueOnly = false; }
var values = this._store.items.reduce(function (selectedItems, item) {
var itemValue = valueOnly ? item.value : _this._getChoiceForOutput(item);
selectedItems.push(itemValue);
return selectedItems;
}, []);
var values = this._store.items.map(function (item) {
return (valueOnly ? item.value : _this._getChoiceForOutput(item));
});
return this._isSelectOneElement || this.config.singleModeForMultiSelect ? values[0] : values;
};
Choices.prototype.setValue = function (items) {
@ -3261,12 +3259,13 @@ var Choices = /** @class */ (function () {
* }], 'value', 'label', false);
* ```
*/
Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices) {
Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices, clearSearchFlag) {
var _this = this;
if (choicesArrayOrFetcher === void 0) { choicesArrayOrFetcher = []; }
if (value === void 0) { value = 'value'; }
if (label === void 0) { label = 'label'; }
if (replaceChoices === void 0) { replaceChoices = false; }
if (clearSearchFlag === void 0) { clearSearchFlag = true; }
if (!this.initialisedOK) {
this._warnChoicesInitFailed('setChoices');
return this;
@ -3311,6 +3310,9 @@ var Choices = /** @class */ (function () {
}
this.containerOuter.removeLoadingState();
this._store.withTxn(function () {
if (clearSearchFlag) {
_this._isSearching = false;
}
var isDefaultValue = value === 'value';
var isDefaultLabel = label === 'label';
choicesArrayOrFetcher.forEach(function (groupOrChoice) {
@ -3357,18 +3359,21 @@ var Choices = /** @class */ (function () {
}
});
}
_this.clearStore();
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
return;
}
var choice = groupOrChoice;
_this.clearStore(false);
var updateChoice = function (choice) {
if (deselectAll) {
_this._store.dispatch(removeItem$1(choice));
}
else if (existingItems[choice.value]) {
choice.selected = true;
}
};
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
groupOrChoice.choices.forEach(updateChoice);
return;
}
updateChoice(groupOrChoice);
});
/* @todo only generate add events for the added options instead of all
if (withEvents) {
@ -3396,6 +3401,7 @@ var Choices = /** @class */ (function () {
if (!choice) {
return this;
}
this._clearNotice();
this._store.dispatch(removeChoice(choice));
// @todo integrate with Store
this._searcher.reset();
@ -3405,13 +3411,26 @@ var Choices = /** @class */ (function () {
return this;
};
Choices.prototype.clearChoices = function () {
this.passedElement.element.replaceChildren('');
return this.clearStore();
var _this = this;
this._store.withTxn(function () {
_this._store.choices.forEach(function (choice) {
if (!choice.selected) {
_this._store.dispatch(removeChoice(choice));
}
});
});
// @todo integrate with Store
this._searcher.reset();
return this;
};
Choices.prototype.clearStore = function () {
Choices.prototype.clearStore = function (clearOptions) {
if (clearOptions === void 0) { clearOptions = true; }
this._stopSearch();
if (clearOptions) {
this.passedElement.element.replaceChildren('');
}
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._clearNotice();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
@ -3422,10 +3441,7 @@ var Choices = /** @class */ (function () {
Choices.prototype.clearInput = function () {
var shouldSetInputWidth = !this._isSelectOneElement;
this.input.clear(shouldSetInputWidth);
this._clearNotice();
if (this._isSearching) {
this._stopSearch();
}
this._stopSearch();
return this;
};
Choices.prototype._validateConfig = function () {
@ -3483,7 +3499,7 @@ var Choices = /** @class */ (function () {
return !choice.placeholder && (isSearching ? !!choice.rank : config.renderSelectedChoices || !choice.selected);
});
};
var selectableChoices = this._isSelectOneElement;
var selectableChoices = false;
var renderChoices = function (choices, withinGroup, groupLabel) {
if (isSearching) {
// sortByRank is used to ensure stable sorting, as scores are non-unique
@ -3501,7 +3517,7 @@ var Choices = /** @class */ (function () {
var dropdownItem = choice.choiceEl || _this._templates.choice(config, choice, config.itemSelectText, groupLabel);
choice.choiceEl = dropdownItem;
fragment.appendChild(dropdownItem);
if (isSearching || !choice.selected) {
if (!choice.disabled && (isSearching || !choice.selected)) {
selectableChoices = true;
}
return index < choiceLimit;
@ -3513,13 +3529,16 @@ var Choices = /** @class */ (function () {
}
if (!this._hasNonChoicePlaceholder && !isSearching && this._isSelectOneElement) {
// If we have a placeholder choice along with groups
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.groupId; }), false, undefined);
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.group; }), false, undefined);
}
// If we have grouped options
if (activeGroups.length && !isSearching) {
if (config.shouldSort) {
activeGroups.sort(config.sorter);
}
// render Choices without group first, regardless of sort, otherwise they won't be distinguishable
// from the last group
renderChoices(activeChoices.filter(function (choice) { return !choice.placeholder && !choice.group; }), false, undefined);
activeGroups.forEach(function (group) {
var groupChoices = renderableChoices(group.choices);
if (groupChoices.length) {
@ -3537,19 +3556,15 @@ var Choices = /** @class */ (function () {
renderChoices(renderableChoices(activeChoices), false, undefined);
}
}
var notice = this._notice;
if (!selectableChoices) {
if (!notice) {
if (!this._notice) {
this._notice = {
text: resolveStringFunction(config.noChoicesText),
type: NoticeTypes.noChoices,
text: resolveStringFunction(isSearching ? config.noResultsText : config.noChoicesText),
type: isSearching ? NoticeTypes.noResults : NoticeTypes.noChoices,
};
}
fragment.replaceChildren('');
}
else if (notice && notice.type === NoticeTypes.noChoices) {
this._notice = undefined;
}
this._renderNotice(fragment);
this.choiceList.element.replaceChildren(fragment);
if (selectableChoices) {
@ -3664,11 +3679,8 @@ var Choices = /** @class */ (function () {
}
}
};
// eslint-disable-next-line class-methods-use-this
Choices.prototype._getChoiceForOutput = function (choice, keyCode) {
if (!choice) {
return undefined;
}
var group = choice.groupId ? this._store.getGroupById(choice.groupId) : null;
return {
id: choice.id,
highlighted: choice.highlighted,
@ -3680,7 +3692,7 @@ var Choices = /** @class */ (function () {
label: choice.label,
placeholder: choice.placeholder,
value: choice.value,
groupValue: group && group.label ? group.label : undefined,
groupValue: choice.group ? choice.group.label : undefined,
element: choice.element,
keyCode: keyCode,
};
@ -3699,7 +3711,7 @@ var Choices = /** @class */ (function () {
if (!items.length || !this.config.removeItems || !this.config.removeItemButton) {
return;
}
var id = element && parseDataSetId(element.parentNode);
var id = element && parseDataSetId(element.parentElement);
var itemToRemove = id && items.find(function (item) { return item.id === id; });
if (!itemToRemove) {
return;
@ -3932,7 +3944,7 @@ var Choices = /** @class */ (function () {
if (!results.length) {
this._displayNotice(resolveStringFunction(this.config.noResultsText), NoticeTypes.noResults);
}
else if (noticeType === NoticeTypes.noResults) {
else {
this._clearNotice();
}
}
@ -3940,10 +3952,10 @@ var Choices = /** @class */ (function () {
return results.length;
};
Choices.prototype._stopSearch = function () {
var wasSearching = this._isSearching;
this._currentValue = '';
this._isSearching = false;
if (wasSearching) {
if (this._isSearching) {
this._currentValue = '';
this._isSearching = false;
this._clearNotice();
this._store.dispatch(activateChoices(true));
this.passedElement.triggerEvent(EventType.search, {
value: '',
@ -4095,7 +4107,6 @@ var Choices = /** @class */ (function () {
else {
this._stopSearch();
}
this._clearNotice();
return;
}
if (!this._canAddItems()) {
@ -4467,11 +4478,16 @@ var Choices = /** @class */ (function () {
if (choice.id) {
throw new TypeError('Can not re-add a choice which has already been added');
}
var config = this.config;
if ((this._isSelectElement || !config.duplicateItemsAllowed) &&
this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
return;
}
// Generate unique id, in-place update is required so chaining _addItem works as expected
this._lastAddedChoiceId++;
choice.id = this._lastAddedChoiceId;
choice.elementId = "".concat(this._baseId, "-").concat(this._idNames.itemChoice, "-").concat(choice.id);
var _a = this.config, prependValue = _a.prependValue, appendValue = _a.appendValue;
var prependValue = config.prependValue, appendValue = config.appendValue;
if (prependValue) {
choice.value = prependValue + choice.value;
}
@ -4481,6 +4497,7 @@ var Choices = /** @class */ (function () {
if ((prependValue || appendValue) && choice.element) {
choice.element.value = choice.value;
}
this._clearNotice();
this._store.dispatch(addChoice(choice));
if (choice.selected) {
this._addItem(choice, withEvents, userTriggered);
@ -4500,7 +4517,7 @@ var Choices = /** @class */ (function () {
this._lastAddedGroupId++;
group.id = this._lastAddedGroupId;
group.choices.forEach(function (item) {
item.groupId = group.id;
item.group = group;
if (group.disabled) {
item.disabled = true;
}
@ -4512,7 +4529,7 @@ var Choices = /** @class */ (function () {
var callbackOnCreateTemplates = this.config.callbackOnCreateTemplates;
var userTemplates = {};
if (typeof callbackOnCreateTemplates === 'function') {
userTemplates = callbackOnCreateTemplates.call(this, strToEl, escapeForTemplate);
userTemplates = callbackOnCreateTemplates.call(this, strToEl, escapeForTemplate, getClassNames);
}
var templating = {};
Object.keys(this._templates).forEach(function (name) {
@ -4593,12 +4610,11 @@ var Choices = /** @class */ (function () {
};
Choices.prototype._initStore = function () {
var _this = this;
this._store.subscribe(this._render);
this._store.withTxn(function () {
this._store.subscribe(this._render).withTxn(function () {
_this._addPredefinedChoices(_this._presetChoices, _this._isSelectOneElement && !_this._hasNonChoicePlaceholder, false);
});
if (this._isSelectOneElement && this._hasNonChoicePlaceholder) {
this._render({ choices: false, groups: false, items: true });
if (!this._store.choices.length || (this._isSelectOneElement && this._hasNonChoicePlaceholder)) {
this._render();
}
};
Choices.prototype._addPredefinedChoices = function (choices, selectFirstOption, withEvents) {
@ -4670,7 +4686,7 @@ var Choices = /** @class */ (function () {
throw new TypeError("".concat(caller, " called for an element which has multiple instances of Choices initialised on it"));
}
};
Choices.version = '11.0.1';
Choices.version = '11.0.2';
return Choices;
}());

View file

@ -1,4 +1,4 @@
/*! choices.js v11.0.1 | © 2024 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
/*! choices.js v11.0.2 | © 2024 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
@ -126,7 +126,6 @@
highlighted: highlighted,
}); };
/* eslint-disable @typescript-eslint/no-explicit-any */
var getRandomNumber = function (min, max) { return Math.floor(Math.random() * (max - min) + min); };
var generateChars = function (length) {
return Array.from({ length: length }, function () { return getRandomNumber(0, 36).toString(36); }).join('');
@ -259,6 +258,7 @@
/**
* Returns an array of keys present on the first but missing on the second object
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var diff = function (a, b) {
var aKeys = Object.keys(a).sort();
var bKeys = Object.keys(b).sort();
@ -764,7 +764,7 @@
var choice = groupOrChoice;
var result = {
id: 0, // actual ID will be assigned during _addChoice
groupId: 0, // actual ID will be assigned during _addGroup but before _addChoice
group: null, // actual group will be assigned during _addGroup but before _addChoice
score: 0, // used in search
rank: 0, // used in search, stable sort order
value: choice.value,
@ -841,7 +841,7 @@
}
return {
id: 0,
groupId: 0,
group: null,
score: 0,
rank: 0,
value: option.value,
@ -1002,8 +1002,8 @@
break;
}
case ActionType.REMOVE_CHOICE: {
state = state.filter(function (item) { return item.id !== action.choice.id; });
removeItem(action.choice);
state = state.filter(function (item) { return item.id !== action.choice.id; });
break;
}
case ActionType.HIGHLIGHT_ITEM: {
@ -1051,16 +1051,14 @@
var update = true;
switch (action.type) {
case ActionType.ADD_CHOICE: {
/*
A disabled choice appears in the choice dropdown but cannot be selected
A selected choice has been added to the passed input's value (added as an item)
An active choice appears within the choice dropdown
*/
state.push(action.choice);
break;
}
case ActionType.REMOVE_CHOICE: {
action.choice.choiceEl = undefined;
if (action.choice.group) {
action.choice.group.choices = action.choice.group.choices.filter(function (obj) { return obj.id !== action.choice.id; });
}
state = state.filter(function (obj) { return obj.id !== action.choice.id; });
break;
}
@ -1158,6 +1156,7 @@
};
Store.prototype.subscribe = function (onChange) {
this._listeners.push(onChange);
return this;
};
Store.prototype.dispatch = function (action) {
var _this = this;
@ -1532,7 +1531,6 @@
label = escapeForTemplate(allowHTML, label);
label += " (".concat(groupName, ")");
label = { trusted: label };
div.dataset.groupId = "".concat(choice.groupId);
}
var describedBy = div;
if (choice.labelClass) {
@ -1560,13 +1558,16 @@
if (choice.placeholder) {
addClassesToElement(div, placeholder);
}
div.setAttribute('role', choice.groupId ? 'treeitem' : 'option');
div.setAttribute('role', choice.group ? 'treeitem' : 'option');
div.dataset.choice = '';
div.dataset.id = choice.id;
div.dataset.value = rawValue;
if (selectText) {
div.dataset.selectText = selectText;
}
if (choice.group) {
div.dataset.groupId = "".concat(choice.group.id);
}
assignCustomProperties(div, choice, false);
if (choice.disabled) {
addClassesToElement(div, itemDisabled);
@ -1853,7 +1854,7 @@
this.passedElement.reveal();
this.containerOuter.unwrap(this.passedElement.element);
this._store._listeners = []; // prevents select/input value being wiped
this.clearStore();
this.clearStore(false);
this._stopSearch();
this._templates = Choices.defaults.templates;
this.initialised = false;
@ -1907,7 +1908,7 @@
}
this._store.dispatch(highlightItem(choice, false));
if (runEvent) {
this.passedElement.triggerEvent(EventType.highlightItem, this._getChoiceForOutput(choice));
this.passedElement.triggerEvent(EventType.unhighlightItem, this._getChoiceForOutput(choice));
}
return this;
};
@ -2001,12 +2002,9 @@
};
Choices.prototype.getValue = function (valueOnly) {
var _this = this;
if (valueOnly === void 0) { valueOnly = false; }
var values = this._store.items.reduce(function (selectedItems, item) {
var itemValue = valueOnly ? item.value : _this._getChoiceForOutput(item);
selectedItems.push(itemValue);
return selectedItems;
}, []);
var values = this._store.items.map(function (item) {
return (valueOnly ? item.value : _this._getChoiceForOutput(item));
});
return this._isSelectOneElement || this.config.singleModeForMultiSelect ? values[0] : values;
};
Choices.prototype.setValue = function (items) {
@ -2109,12 +2107,13 @@
* }], 'value', 'label', false);
* ```
*/
Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices) {
Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices, clearSearchFlag) {
var _this = this;
if (choicesArrayOrFetcher === void 0) { choicesArrayOrFetcher = []; }
if (value === void 0) { value = 'value'; }
if (label === void 0) { label = 'label'; }
if (replaceChoices === void 0) { replaceChoices = false; }
if (clearSearchFlag === void 0) { clearSearchFlag = true; }
if (!this.initialisedOK) {
this._warnChoicesInitFailed('setChoices');
return this;
@ -2159,6 +2158,9 @@
}
this.containerOuter.removeLoadingState();
this._store.withTxn(function () {
if (clearSearchFlag) {
_this._isSearching = false;
}
var isDefaultValue = value === 'value';
var isDefaultLabel = label === 'label';
choicesArrayOrFetcher.forEach(function (groupOrChoice) {
@ -2205,18 +2207,21 @@
}
});
}
_this.clearStore();
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
return;
}
var choice = groupOrChoice;
_this.clearStore(false);
var updateChoice = function (choice) {
if (deselectAll) {
_this._store.dispatch(removeItem$1(choice));
}
else if (existingItems[choice.value]) {
choice.selected = true;
}
};
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
groupOrChoice.choices.forEach(updateChoice);
return;
}
updateChoice(groupOrChoice);
});
/* @todo only generate add events for the added options instead of all
if (withEvents) {
@ -2244,6 +2249,7 @@
if (!choice) {
return this;
}
this._clearNotice();
this._store.dispatch(removeChoice(choice));
// @todo integrate with Store
this._searcher.reset();
@ -2253,13 +2259,26 @@
return this;
};
Choices.prototype.clearChoices = function () {
this.passedElement.element.replaceChildren('');
return this.clearStore();
var _this = this;
this._store.withTxn(function () {
_this._store.choices.forEach(function (choice) {
if (!choice.selected) {
_this._store.dispatch(removeChoice(choice));
}
});
});
// @todo integrate with Store
this._searcher.reset();
return this;
};
Choices.prototype.clearStore = function () {
Choices.prototype.clearStore = function (clearOptions) {
if (clearOptions === void 0) { clearOptions = true; }
this._stopSearch();
if (clearOptions) {
this.passedElement.element.replaceChildren('');
}
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._clearNotice();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
@ -2270,10 +2289,7 @@
Choices.prototype.clearInput = function () {
var shouldSetInputWidth = !this._isSelectOneElement;
this.input.clear(shouldSetInputWidth);
this._clearNotice();
if (this._isSearching) {
this._stopSearch();
}
this._stopSearch();
return this;
};
Choices.prototype._validateConfig = function () {
@ -2331,7 +2347,7 @@
return !choice.placeholder && (isSearching ? !!choice.rank : config.renderSelectedChoices || !choice.selected);
});
};
var selectableChoices = this._isSelectOneElement;
var selectableChoices = false;
var renderChoices = function (choices, withinGroup, groupLabel) {
if (isSearching) {
// sortByRank is used to ensure stable sorting, as scores are non-unique
@ -2349,7 +2365,7 @@
var dropdownItem = choice.choiceEl || _this._templates.choice(config, choice, config.itemSelectText, groupLabel);
choice.choiceEl = dropdownItem;
fragment.appendChild(dropdownItem);
if (isSearching || !choice.selected) {
if (!choice.disabled && (isSearching || !choice.selected)) {
selectableChoices = true;
}
return index < choiceLimit;
@ -2361,13 +2377,16 @@
}
if (!this._hasNonChoicePlaceholder && !isSearching && this._isSelectOneElement) {
// If we have a placeholder choice along with groups
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.groupId; }), false, undefined);
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.group; }), false, undefined);
}
// If we have grouped options
if (activeGroups.length && !isSearching) {
if (config.shouldSort) {
activeGroups.sort(config.sorter);
}
// render Choices without group first, regardless of sort, otherwise they won't be distinguishable
// from the last group
renderChoices(activeChoices.filter(function (choice) { return !choice.placeholder && !choice.group; }), false, undefined);
activeGroups.forEach(function (group) {
var groupChoices = renderableChoices(group.choices);
if (groupChoices.length) {
@ -2385,19 +2404,15 @@
renderChoices(renderableChoices(activeChoices), false, undefined);
}
}
var notice = this._notice;
if (!selectableChoices) {
if (!notice) {
if (!this._notice) {
this._notice = {
text: resolveStringFunction(config.noChoicesText),
type: NoticeTypes.noChoices,
text: resolveStringFunction(isSearching ? config.noResultsText : config.noChoicesText),
type: isSearching ? NoticeTypes.noResults : NoticeTypes.noChoices,
};
}
fragment.replaceChildren('');
}
else if (notice && notice.type === NoticeTypes.noChoices) {
this._notice = undefined;
}
this._renderNotice(fragment);
this.choiceList.element.replaceChildren(fragment);
if (selectableChoices) {
@ -2512,11 +2527,8 @@
}
}
};
// eslint-disable-next-line class-methods-use-this
Choices.prototype._getChoiceForOutput = function (choice, keyCode) {
if (!choice) {
return undefined;
}
var group = choice.groupId ? this._store.getGroupById(choice.groupId) : null;
return {
id: choice.id,
highlighted: choice.highlighted,
@ -2528,7 +2540,7 @@
label: choice.label,
placeholder: choice.placeholder,
value: choice.value,
groupValue: group && group.label ? group.label : undefined,
groupValue: choice.group ? choice.group.label : undefined,
element: choice.element,
keyCode: keyCode,
};
@ -2547,7 +2559,7 @@
if (!items.length || !this.config.removeItems || !this.config.removeItemButton) {
return;
}
var id = element && parseDataSetId(element.parentNode);
var id = element && parseDataSetId(element.parentElement);
var itemToRemove = id && items.find(function (item) { return item.id === id; });
if (!itemToRemove) {
return;
@ -2780,7 +2792,7 @@
if (!results.length) {
this._displayNotice(resolveStringFunction(this.config.noResultsText), NoticeTypes.noResults);
}
else if (noticeType === NoticeTypes.noResults) {
else {
this._clearNotice();
}
}
@ -2788,10 +2800,10 @@
return results.length;
};
Choices.prototype._stopSearch = function () {
var wasSearching = this._isSearching;
this._currentValue = '';
this._isSearching = false;
if (wasSearching) {
if (this._isSearching) {
this._currentValue = '';
this._isSearching = false;
this._clearNotice();
this._store.dispatch(activateChoices(true));
this.passedElement.triggerEvent(EventType.search, {
value: '',
@ -2943,7 +2955,6 @@
else {
this._stopSearch();
}
this._clearNotice();
return;
}
if (!this._canAddItems()) {
@ -3315,11 +3326,16 @@
if (choice.id) {
throw new TypeError('Can not re-add a choice which has already been added');
}
var config = this.config;
if ((this._isSelectElement || !config.duplicateItemsAllowed) &&
this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
return;
}
// Generate unique id, in-place update is required so chaining _addItem works as expected
this._lastAddedChoiceId++;
choice.id = this._lastAddedChoiceId;
choice.elementId = "".concat(this._baseId, "-").concat(this._idNames.itemChoice, "-").concat(choice.id);
var _a = this.config, prependValue = _a.prependValue, appendValue = _a.appendValue;
var prependValue = config.prependValue, appendValue = config.appendValue;
if (prependValue) {
choice.value = prependValue + choice.value;
}
@ -3329,6 +3345,7 @@
if ((prependValue || appendValue) && choice.element) {
choice.element.value = choice.value;
}
this._clearNotice();
this._store.dispatch(addChoice(choice));
if (choice.selected) {
this._addItem(choice, withEvents, userTriggered);
@ -3348,7 +3365,7 @@
this._lastAddedGroupId++;
group.id = this._lastAddedGroupId;
group.choices.forEach(function (item) {
item.groupId = group.id;
item.group = group;
if (group.disabled) {
item.disabled = true;
}
@ -3360,7 +3377,7 @@
var callbackOnCreateTemplates = this.config.callbackOnCreateTemplates;
var userTemplates = {};
if (typeof callbackOnCreateTemplates === 'function') {
userTemplates = callbackOnCreateTemplates.call(this, strToEl, escapeForTemplate);
userTemplates = callbackOnCreateTemplates.call(this, strToEl, escapeForTemplate, getClassNames);
}
var templating = {};
Object.keys(this._templates).forEach(function (name) {
@ -3441,12 +3458,11 @@
};
Choices.prototype._initStore = function () {
var _this = this;
this._store.subscribe(this._render);
this._store.withTxn(function () {
this._store.subscribe(this._render).withTxn(function () {
_this._addPredefinedChoices(_this._presetChoices, _this._isSelectOneElement && !_this._hasNonChoicePlaceholder, false);
});
if (this._isSelectOneElement && this._hasNonChoicePlaceholder) {
this._render({ choices: false, groups: false, items: true });
if (!this._store.choices.length || (this._isSelectOneElement && this._hasNonChoicePlaceholder)) {
this._render();
}
};
Choices.prototype._addPredefinedChoices = function (choices, selectFirstOption, withEvents) {
@ -3518,7 +3534,7 @@
throw new TypeError("".concat(caller, " called for an element which has multiple instances of Choices initialised on it"));
}
};
Choices.version = '11.0.1';
Choices.version = '11.0.2';
return Choices;
}());

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,4 @@
/*! choices.js v11.0.1 | © 2024 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
/*! choices.js v11.0.2 | © 2024 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
/******************************************************************************
Copyright (c) Microsoft Corporation.
@ -120,7 +120,6 @@ var highlightItem = function (item, highlighted) { return ({
highlighted: highlighted,
}); };
/* eslint-disable @typescript-eslint/no-explicit-any */
var getRandomNumber = function (min, max) { return Math.floor(Math.random() * (max - min) + min); };
var generateChars = function (length) {
return Array.from({ length: length }, function () { return getRandomNumber(0, 36).toString(36); }).join('');
@ -253,6 +252,7 @@ var dispatchEvent = function (element, type, customArgs) {
/**
* Returns an array of keys present on the first but missing on the second object
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var diff = function (a, b) {
var aKeys = Object.keys(a).sort();
var bKeys = Object.keys(b).sort();
@ -758,7 +758,7 @@ var mapInputToChoice = function (value, allowGroup) {
var choice = groupOrChoice;
var result = {
id: 0, // actual ID will be assigned during _addChoice
groupId: 0, // actual ID will be assigned during _addGroup but before _addChoice
group: null, // actual group will be assigned during _addGroup but before _addChoice
score: 0, // used in search
rank: 0, // used in search, stable sort order
value: choice.value,
@ -835,7 +835,7 @@ var WrappedSelect = /** @class */ (function (_super) {
}
return {
id: 0,
groupId: 0,
group: null,
score: 0,
rank: 0,
value: option.value,
@ -996,8 +996,8 @@ function items(s, action, context) {
break;
}
case ActionType.REMOVE_CHOICE: {
state = state.filter(function (item) { return item.id !== action.choice.id; });
removeItem(action.choice);
state = state.filter(function (item) { return item.id !== action.choice.id; });
break;
}
case ActionType.HIGHLIGHT_ITEM: {
@ -1045,16 +1045,14 @@ function choices(s, action, context) {
var update = true;
switch (action.type) {
case ActionType.ADD_CHOICE: {
/*
A disabled choice appears in the choice dropdown but cannot be selected
A selected choice has been added to the passed input's value (added as an item)
An active choice appears within the choice dropdown
*/
state.push(action.choice);
break;
}
case ActionType.REMOVE_CHOICE: {
action.choice.choiceEl = undefined;
if (action.choice.group) {
action.choice.group.choices = action.choice.group.choices.filter(function (obj) { return obj.id !== action.choice.id; });
}
state = state.filter(function (obj) { return obj.id !== action.choice.id; });
break;
}
@ -1152,6 +1150,7 @@ var Store = /** @class */ (function () {
};
Store.prototype.subscribe = function (onChange) {
this._listeners.push(onChange);
return this;
};
Store.prototype.dispatch = function (action) {
var _this = this;
@ -1526,7 +1525,6 @@ var templates = {
label = escapeForTemplate(allowHTML, label);
label += " (".concat(groupName, ")");
label = { trusted: label };
div.dataset.groupId = "".concat(choice.groupId);
}
var describedBy = div;
if (choice.labelClass) {
@ -1554,13 +1552,16 @@ var templates = {
if (choice.placeholder) {
addClassesToElement(div, placeholder);
}
div.setAttribute('role', choice.groupId ? 'treeitem' : 'option');
div.setAttribute('role', choice.group ? 'treeitem' : 'option');
div.dataset.choice = '';
div.dataset.id = choice.id;
div.dataset.value = rawValue;
if (selectText) {
div.dataset.selectText = selectText;
}
if (choice.group) {
div.dataset.groupId = "".concat(choice.group.id);
}
assignCustomProperties(div, choice, false);
if (choice.disabled) {
addClassesToElement(div, itemDisabled);
@ -1847,7 +1848,7 @@ var Choices = /** @class */ (function () {
this.passedElement.reveal();
this.containerOuter.unwrap(this.passedElement.element);
this._store._listeners = []; // prevents select/input value being wiped
this.clearStore();
this.clearStore(false);
this._stopSearch();
this._templates = Choices.defaults.templates;
this.initialised = false;
@ -1901,7 +1902,7 @@ var Choices = /** @class */ (function () {
}
this._store.dispatch(highlightItem(choice, false));
if (runEvent) {
this.passedElement.triggerEvent(EventType.highlightItem, this._getChoiceForOutput(choice));
this.passedElement.triggerEvent(EventType.unhighlightItem, this._getChoiceForOutput(choice));
}
return this;
};
@ -1995,12 +1996,9 @@ var Choices = /** @class */ (function () {
};
Choices.prototype.getValue = function (valueOnly) {
var _this = this;
if (valueOnly === void 0) { valueOnly = false; }
var values = this._store.items.reduce(function (selectedItems, item) {
var itemValue = valueOnly ? item.value : _this._getChoiceForOutput(item);
selectedItems.push(itemValue);
return selectedItems;
}, []);
var values = this._store.items.map(function (item) {
return (valueOnly ? item.value : _this._getChoiceForOutput(item));
});
return this._isSelectOneElement || this.config.singleModeForMultiSelect ? values[0] : values;
};
Choices.prototype.setValue = function (items) {
@ -2103,12 +2101,13 @@ var Choices = /** @class */ (function () {
* }], 'value', 'label', false);
* ```
*/
Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices) {
Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices, clearSearchFlag) {
var _this = this;
if (choicesArrayOrFetcher === void 0) { choicesArrayOrFetcher = []; }
if (value === void 0) { value = 'value'; }
if (label === void 0) { label = 'label'; }
if (replaceChoices === void 0) { replaceChoices = false; }
if (clearSearchFlag === void 0) { clearSearchFlag = true; }
if (!this.initialisedOK) {
this._warnChoicesInitFailed('setChoices');
return this;
@ -2153,6 +2152,9 @@ var Choices = /** @class */ (function () {
}
this.containerOuter.removeLoadingState();
this._store.withTxn(function () {
if (clearSearchFlag) {
_this._isSearching = false;
}
var isDefaultValue = value === 'value';
var isDefaultLabel = label === 'label';
choicesArrayOrFetcher.forEach(function (groupOrChoice) {
@ -2199,18 +2201,21 @@ var Choices = /** @class */ (function () {
}
});
}
_this.clearStore();
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
return;
}
var choice = groupOrChoice;
_this.clearStore(false);
var updateChoice = function (choice) {
if (deselectAll) {
_this._store.dispatch(removeItem$1(choice));
}
else if (existingItems[choice.value]) {
choice.selected = true;
}
};
choicesFromOptions.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
groupOrChoice.choices.forEach(updateChoice);
return;
}
updateChoice(groupOrChoice);
});
/* @todo only generate add events for the added options instead of all
if (withEvents) {
@ -2238,6 +2243,7 @@ var Choices = /** @class */ (function () {
if (!choice) {
return this;
}
this._clearNotice();
this._store.dispatch(removeChoice(choice));
// @todo integrate with Store
this._searcher.reset();
@ -2247,13 +2253,26 @@ var Choices = /** @class */ (function () {
return this;
};
Choices.prototype.clearChoices = function () {
this.passedElement.element.replaceChildren('');
return this.clearStore();
var _this = this;
this._store.withTxn(function () {
_this._store.choices.forEach(function (choice) {
if (!choice.selected) {
_this._store.dispatch(removeChoice(choice));
}
});
});
// @todo integrate with Store
this._searcher.reset();
return this;
};
Choices.prototype.clearStore = function () {
Choices.prototype.clearStore = function (clearOptions) {
if (clearOptions === void 0) { clearOptions = true; }
this._stopSearch();
if (clearOptions) {
this.passedElement.element.replaceChildren('');
}
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._clearNotice();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
@ -2264,10 +2283,7 @@ var Choices = /** @class */ (function () {
Choices.prototype.clearInput = function () {
var shouldSetInputWidth = !this._isSelectOneElement;
this.input.clear(shouldSetInputWidth);
this._clearNotice();
if (this._isSearching) {
this._stopSearch();
}
this._stopSearch();
return this;
};
Choices.prototype._validateConfig = function () {
@ -2325,7 +2341,7 @@ var Choices = /** @class */ (function () {
return !choice.placeholder && (isSearching ? !!choice.rank : config.renderSelectedChoices || !choice.selected);
});
};
var selectableChoices = this._isSelectOneElement;
var selectableChoices = false;
var renderChoices = function (choices, withinGroup, groupLabel) {
if (isSearching) {
// sortByRank is used to ensure stable sorting, as scores are non-unique
@ -2343,7 +2359,7 @@ var Choices = /** @class */ (function () {
var dropdownItem = choice.choiceEl || _this._templates.choice(config, choice, config.itemSelectText, groupLabel);
choice.choiceEl = dropdownItem;
fragment.appendChild(dropdownItem);
if (isSearching || !choice.selected) {
if (!choice.disabled && (isSearching || !choice.selected)) {
selectableChoices = true;
}
return index < choiceLimit;
@ -2355,13 +2371,16 @@ var Choices = /** @class */ (function () {
}
if (!this._hasNonChoicePlaceholder && !isSearching && this._isSelectOneElement) {
// If we have a placeholder choice along with groups
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.groupId; }), false, undefined);
renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.group; }), false, undefined);
}
// If we have grouped options
if (activeGroups.length && !isSearching) {
if (config.shouldSort) {
activeGroups.sort(config.sorter);
}
// render Choices without group first, regardless of sort, otherwise they won't be distinguishable
// from the last group
renderChoices(activeChoices.filter(function (choice) { return !choice.placeholder && !choice.group; }), false, undefined);
activeGroups.forEach(function (group) {
var groupChoices = renderableChoices(group.choices);
if (groupChoices.length) {
@ -2379,19 +2398,15 @@ var Choices = /** @class */ (function () {
renderChoices(renderableChoices(activeChoices), false, undefined);
}
}
var notice = this._notice;
if (!selectableChoices) {
if (!notice) {
if (!this._notice) {
this._notice = {
text: resolveStringFunction(config.noChoicesText),
type: NoticeTypes.noChoices,
text: resolveStringFunction(isSearching ? config.noResultsText : config.noChoicesText),
type: isSearching ? NoticeTypes.noResults : NoticeTypes.noChoices,
};
}
fragment.replaceChildren('');
}
else if (notice && notice.type === NoticeTypes.noChoices) {
this._notice = undefined;
}
this._renderNotice(fragment);
this.choiceList.element.replaceChildren(fragment);
if (selectableChoices) {
@ -2506,11 +2521,8 @@ var Choices = /** @class */ (function () {
}
}
};
// eslint-disable-next-line class-methods-use-this
Choices.prototype._getChoiceForOutput = function (choice, keyCode) {
if (!choice) {
return undefined;
}
var group = choice.groupId ? this._store.getGroupById(choice.groupId) : null;
return {
id: choice.id,
highlighted: choice.highlighted,
@ -2522,7 +2534,7 @@ var Choices = /** @class */ (function () {
label: choice.label,
placeholder: choice.placeholder,
value: choice.value,
groupValue: group && group.label ? group.label : undefined,
groupValue: choice.group ? choice.group.label : undefined,
element: choice.element,
keyCode: keyCode,
};
@ -2541,7 +2553,7 @@ var Choices = /** @class */ (function () {
if (!items.length || !this.config.removeItems || !this.config.removeItemButton) {
return;
}
var id = element && parseDataSetId(element.parentNode);
var id = element && parseDataSetId(element.parentElement);
var itemToRemove = id && items.find(function (item) { return item.id === id; });
if (!itemToRemove) {
return;
@ -2774,7 +2786,7 @@ var Choices = /** @class */ (function () {
if (!results.length) {
this._displayNotice(resolveStringFunction(this.config.noResultsText), NoticeTypes.noResults);
}
else if (noticeType === NoticeTypes.noResults) {
else {
this._clearNotice();
}
}
@ -2782,10 +2794,10 @@ var Choices = /** @class */ (function () {
return results.length;
};
Choices.prototype._stopSearch = function () {
var wasSearching = this._isSearching;
this._currentValue = '';
this._isSearching = false;
if (wasSearching) {
if (this._isSearching) {
this._currentValue = '';
this._isSearching = false;
this._clearNotice();
this._store.dispatch(activateChoices(true));
this.passedElement.triggerEvent(EventType.search, {
value: '',
@ -2937,7 +2949,6 @@ var Choices = /** @class */ (function () {
else {
this._stopSearch();
}
this._clearNotice();
return;
}
if (!this._canAddItems()) {
@ -3309,11 +3320,16 @@ var Choices = /** @class */ (function () {
if (choice.id) {
throw new TypeError('Can not re-add a choice which has already been added');
}
var config = this.config;
if ((this._isSelectElement || !config.duplicateItemsAllowed) &&
this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
return;
}
// Generate unique id, in-place update is required so chaining _addItem works as expected
this._lastAddedChoiceId++;
choice.id = this._lastAddedChoiceId;
choice.elementId = "".concat(this._baseId, "-").concat(this._idNames.itemChoice, "-").concat(choice.id);
var _a = this.config, prependValue = _a.prependValue, appendValue = _a.appendValue;
var prependValue = config.prependValue, appendValue = config.appendValue;
if (prependValue) {
choice.value = prependValue + choice.value;
}
@ -3323,6 +3339,7 @@ var Choices = /** @class */ (function () {
if ((prependValue || appendValue) && choice.element) {
choice.element.value = choice.value;
}
this._clearNotice();
this._store.dispatch(addChoice(choice));
if (choice.selected) {
this._addItem(choice, withEvents, userTriggered);
@ -3342,7 +3359,7 @@ var Choices = /** @class */ (function () {
this._lastAddedGroupId++;
group.id = this._lastAddedGroupId;
group.choices.forEach(function (item) {
item.groupId = group.id;
item.group = group;
if (group.disabled) {
item.disabled = true;
}
@ -3354,7 +3371,7 @@ var Choices = /** @class */ (function () {
var callbackOnCreateTemplates = this.config.callbackOnCreateTemplates;
var userTemplates = {};
if (typeof callbackOnCreateTemplates === 'function') {
userTemplates = callbackOnCreateTemplates.call(this, strToEl, escapeForTemplate);
userTemplates = callbackOnCreateTemplates.call(this, strToEl, escapeForTemplate, getClassNames);
}
var templating = {};
Object.keys(this._templates).forEach(function (name) {
@ -3435,12 +3452,11 @@ var Choices = /** @class */ (function () {
};
Choices.prototype._initStore = function () {
var _this = this;
this._store.subscribe(this._render);
this._store.withTxn(function () {
this._store.subscribe(this._render).withTxn(function () {
_this._addPredefinedChoices(_this._presetChoices, _this._isSelectOneElement && !_this._hasNonChoicePlaceholder, false);
});
if (this._isSelectOneElement && this._hasNonChoicePlaceholder) {
this._render({ choices: false, groups: false, items: true });
if (!this._store.choices.length || (this._isSelectOneElement && this._hasNonChoicePlaceholder)) {
this._render();
}
};
Choices.prototype._addPredefinedChoices = function (choices, selectFirstOption, withEvents) {
@ -3512,7 +3528,7 @@ var Choices = /** @class */ (function () {
throw new TypeError("".concat(caller, " called for an element which has multiple instances of Choices initialised on it"));
}
};
Choices.version = '11.0.1';
Choices.version = '11.0.2';
return Choices;
}());

View file

@ -6,7 +6,7 @@ import { StateChangeSet } from './interfaces/state';
import Store from './store/store';
import { ChoiceFull } from './interfaces/choice-full';
import { GroupFull } from './interfaces/group-full';
import { PassedElementType } from './interfaces';
import { EventChoiceValueType, PassedElementType } from './interfaces';
import { EventChoice } from './interfaces/event-choice';
import { NoticeType, Templates } from './interfaces/templates';
import { Searcher } from './interfaces/search';
@ -76,7 +76,7 @@ declare class Choices {
removeHighlightedItems(runEvent?: boolean): this;
showDropdown(preventInputFocus?: boolean): this;
hideDropdown(preventInputBlur?: boolean): this;
getValue(valueOnly?: boolean): string[] | EventChoice[] | EventChoice | string;
getValue<B extends boolean = false>(valueOnly?: B): EventChoiceValueType<B> | EventChoiceValueType<B>[];
setValue(items: string[] | InputChoice[]): this;
setChoiceByValue(value: string | string[]): this;
/**
@ -142,11 +142,11 @@ declare class Choices {
* }], 'value', 'label', false);
* ```
*/
setChoices(choicesArrayOrFetcher?: (InputChoice | InputGroup)[] | ((instance: Choices) => (InputChoice | InputGroup)[] | Promise<(InputChoice | InputGroup)[]>), value?: string | null, label?: string, replaceChoices?: boolean): this | Promise<this>;
setChoices(choicesArrayOrFetcher?: (InputChoice | InputGroup)[] | ((instance: Choices) => (InputChoice | InputGroup)[] | Promise<(InputChoice | InputGroup)[]>), value?: string | null, label?: string, replaceChoices?: boolean, clearSearchFlag?: boolean): this | Promise<this>;
refresh(withEvents?: boolean, selectFirstOption?: boolean, deselectAll?: boolean): this;
removeChoice(value: string): this;
clearChoices(): this;
clearStore(): this;
clearStore(clearOptions?: boolean): this;
clearInput(): this;
_validateConfig(): void;
_render(changes?: StateChangeSet): void;
@ -155,11 +155,11 @@ declare class Choices {
_displayNotice(text: string, type: NoticeType, openDropdown?: boolean): void;
_clearNotice(): void;
_renderNotice(fragment?: DocumentFragment): void;
_getChoiceForOutput(choice?: ChoiceFull, keyCode?: number): EventChoice | undefined;
_getChoiceForOutput(choice: ChoiceFull, keyCode?: number): EventChoice;
_triggerChange(value: any): void;
_handleButtonAction(element?: HTMLElement): void;
_handleItemAction(element?: HTMLElement, hasShiftKey?: boolean): void;
_handleChoiceAction(element?: HTMLElement): boolean;
_handleButtonAction(element: HTMLElement): void;
_handleItemAction(element: HTMLElement, hasShiftKey?: boolean): void;
_handleChoiceAction(element: HTMLElement): boolean;
_handleBackspace(items: ChoiceFull[]): void;
_loadChoices(): void;
_handleLoadingState(setLoading?: boolean): void;

View file

@ -1,5 +1,6 @@
import { StringUntrusted } from './string-untrusted';
export type CustomProperties = Record<string, any> | string;
import { Types } from './types';
import { GroupFull } from './group-full';
export interface ChoiceFull {
id: number;
highlighted: boolean;
@ -8,11 +9,11 @@ export interface ChoiceFull {
choiceEl?: HTMLElement;
labelClass?: Array<string>;
labelDescription?: string;
customProperties?: CustomProperties;
customProperties?: Types.CustomProperties;
disabled: boolean;
active: boolean;
elementId?: string;
groupId: number;
group: GroupFull | null;
label: StringUntrusted | string;
placeholder: boolean;
selected: boolean;

View file

@ -1,4 +1,5 @@
import { InputChoice } from './input-choice';
export type EventChoiceValueType<B extends boolean> = B extends true ? string : EventChoice;
export interface EventChoice extends InputChoice {
element?: HTMLOptionElement | HTMLOptGroupElement;
groupValue?: string;

View file

@ -1,10 +1,11 @@
import { StringUntrusted } from './string-untrusted';
import { Types } from './types';
export interface InputChoice {
id?: number;
highlighted?: boolean;
labelClass?: string | Array<string>;
labelDescription?: string;
customProperties?: Record<string, any> | string;
customProperties?: Types.CustomProperties;
disabled?: boolean;
active?: boolean;
label: StringUntrusted | string;

View file

@ -3,6 +3,7 @@ import { InputChoice } from './input-choice';
import { ClassNames } from './class-names';
import { PositionOptionsType } from './position-options-type';
import { Types } from './types';
import { CallbackOnCreateTemplatesFn } from './templates';
export declare const ObjectsInConfig: string[];
/**
* Choices options interface
@ -447,9 +448,9 @@ export interface Options {
*/
noResultsText: string | Types.StringFunction;
/**
* The text that is shown when a user has selected all possible choices. Optionally pass a function returning a string.
* The text that is shown when a user has selected all possible choices, or no choices exist. Optionally pass a function returning a string.
*
* **Input types affected:** select-multiple
* **Input types affected:** select-multiple, select-one
*
* @default 'No choices to choose from'
*/
@ -536,7 +537,7 @@ export interface Options {
* @example
* ```
* const example = new Choices(element, {
* callbackOnCreateTemplates: function (template, originalTemplates) {
* callbackOnCreateTemplates: function (template, originalTemplates, getClassNames) {
* var classNames = this.config.classNames;
* return {
* item: (data) => {
@ -560,6 +561,6 @@ export interface Options {
*
* @default null
*/
callbackOnCreateTemplates: ((template: Types.StrToEl, escapeForTemplate: Types.EscapeForTemplateFn) => void) | null;
callbackOnCreateTemplates: CallbackOnCreateTemplatesFn | null;
appendGroupInSearch: false;
}

View file

@ -12,6 +12,7 @@ export declare const NoticeTypes: {
readonly generic: "";
};
export type NoticeType = Types.ValueOf<typeof NoticeTypes>;
export type CallbackOnCreateTemplatesFn = (template: Types.StrToEl, escapeForTemplate: Types.EscapeForTemplateFn, getClassNames: Types.GetClassNamesFn) => Partial<Templates>;
export interface Templates {
containerOuter(options: TemplateOptions, dir: HTMLElement['dir'], isSelectElement: boolean, isSelectOneElement: boolean, searchEnabled: boolean, passedElementType: PassedElementType, labelId: string): HTMLDivElement;
containerInner({ classNames: { containerInner } }: TemplateOptions): HTMLDivElement;

View file

@ -3,6 +3,7 @@ import { StringPreEscaped } from './string-pre-escaped';
export declare namespace Types {
type StrToEl = (str: string) => HTMLElement | HTMLInputElement | HTMLOptionElement;
type EscapeForTemplateFn = (allowHTML: boolean, s: StringUntrusted | StringPreEscaped | string) => string;
type GetClassNamesFn = (s: string | Array<string>) => string;
type StringFunction = () => string;
type NoticeStringFunction = (value: string, valueRaw: string) => string;
type NoticeLimitFunction = (maxItemCount: number) => string;
@ -13,4 +14,5 @@ export declare namespace Types {
label?: StringUntrusted | string;
}
type ValueOf<T extends object> = T[keyof T];
type CustomProperties = Record<string, any> | string;
}

View file

@ -12,7 +12,7 @@ export default class Store<T> implements IStore {
get defaultState(): State;
changeSet(init: boolean): StateChangeSet;
reset(): void;
subscribe(onChange: StoreListener): void;
subscribe(onChange: StoreListener): this;
dispatch(action: AnyAction): void;
withTxn(func: () => void): void;
/**