mirror of
https://github.com/Choices-js/Choices.git
synced 2024-06-08 00:42:15 +02:00
Merge 61e48f846a
into 5dbea2825a
This commit is contained in:
commit
dcd8ce7697
10
README.md
10
README.md
|
@ -122,6 +122,7 @@ Or include Choices directly:
|
|||
choices: [],
|
||||
renderChoiceLimit: -1,
|
||||
maxItemCount: -1,
|
||||
addChoices: false,
|
||||
addItems: true,
|
||||
addItemFilter: null,
|
||||
removeItems: true,
|
||||
|
@ -304,6 +305,15 @@ Pass an array of objects:
|
|||
|
||||
**Usage:** The amount of items a user can input/select ("-1" indicates no limit).
|
||||
|
||||
### addChoices
|
||||
**Type**: `Boolean` **Default:** `false`
|
||||
|
||||
**Input types affected:** `select-multiple`, `select-one`
|
||||
|
||||
**Usage:** Whether a user can add choices
|
||||
|
||||
**Note:** `addItems` must also be `true`
|
||||
|
||||
### addItems
|
||||
|
||||
**Type:** `Boolean` **Default:** `true`
|
||||
|
|
|
@ -1006,6 +1006,24 @@ describe('Choices - select multiple', () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('adding user-created choices', () => {
|
||||
it('allows the user to add choices', () => {
|
||||
const newChoice = 'New Choice';
|
||||
|
||||
cy.get('[data-test-hook=add-choices]')
|
||||
.find('.choices__input--cloned')
|
||||
.type(newChoice)
|
||||
.type('{enter}');
|
||||
|
||||
cy.get('[data-test-hook=add-choices]')
|
||||
.find('.choices__list--multiple')
|
||||
.last()
|
||||
.should($el => {
|
||||
expect($el).to.contain(newChoice);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1145,5 +1145,26 @@ describe('Choices - select one', () => {
|
|||
.should('have.length', 3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('adding user-created choices', () => {
|
||||
beforeEach(() => {
|
||||
cy.get('[data-test-hook=add-choices]').find('.choices').click();
|
||||
});
|
||||
|
||||
it('allows the user to add choices', () => {
|
||||
const newChoice = 'New Choice';
|
||||
|
||||
cy.get('[data-test-hook=add-choices]')
|
||||
.find('.choices__input--cloned')
|
||||
.type(newChoice)
|
||||
.type('{enter}');
|
||||
|
||||
cy.get('[data-test-hook=add-choices]')
|
||||
.find('.choices__list--single .choices__item')
|
||||
.should(($el) => {
|
||||
expect($el).to.contain(newChoice);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/*! choices.js v10.2.0 | © 2022 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
|
||||
/*! choices.js v10.2.0 | © 2023 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
|
||||
(function webpackUniversalModuleDefinition(root, factory) {
|
||||
if(typeof exports === 'object' && typeof module === 'object')
|
||||
module.exports = factory();
|
||||
|
@ -807,9 +807,8 @@ var Choices = /** @class */function () {
|
|||
} else if (activeChoices.length >= 1) {
|
||||
choiceListFragment = this._createChoicesFragment(activeChoices, choiceListFragment);
|
||||
}
|
||||
// If we have choices to show
|
||||
var activeItems = this._store.activeItems; // If we have choices to show
|
||||
if (choiceListFragment.childNodes && choiceListFragment.childNodes.length > 0) {
|
||||
var activeItems = this._store.activeItems;
|
||||
var canAddItem = this._canAddItem(activeItems, this.input.value);
|
||||
// ...and we can select them
|
||||
if (canAddItem.response) {
|
||||
|
@ -822,13 +821,15 @@ var Choices = /** @class */function () {
|
|||
}
|
||||
} else {
|
||||
// Otherwise show a notice
|
||||
var canAddChoice = this._canAddChoice(activeItems, this.input.value);
|
||||
var dropdownItem = void 0;
|
||||
var notice = void 0;
|
||||
if (this._isSearching) {
|
||||
notice = typeof this.config.noResultsText === 'function' ? this.config.noResultsText() : this.config.noResultsText;
|
||||
if (canAddChoice.response) {
|
||||
dropdownItem = this._getTemplate('notice', canAddChoice.notice);
|
||||
} else if (this._isSearching) {
|
||||
var notice = typeof this.config.noResultsText === 'function' ? this.config.noResultsText() : this.config.noResultsText;
|
||||
dropdownItem = this._getTemplate('notice', notice, 'no-results');
|
||||
} else {
|
||||
notice = typeof this.config.noChoicesText === 'function' ? this.config.noChoicesText() : this.config.noChoicesText;
|
||||
var notice = typeof this.config.noChoicesText === 'function' ? this.config.noChoicesText() : this.config.noChoicesText;
|
||||
dropdownItem = this._getTemplate('notice', notice, 'no-choices');
|
||||
}
|
||||
this.choiceList.append(dropdownItem);
|
||||
|
@ -1151,6 +1152,11 @@ var Choices = /** @class */function () {
|
|||
this._store.dispatch((0, choices_1.activateChoices)(true));
|
||||
}
|
||||
};
|
||||
Choices.prototype._canAddChoice = function (activeItems, value) {
|
||||
var canAddItem = this._canAddItem(activeItems, value);
|
||||
canAddItem.response = this.config.addChoices && canAddItem.response;
|
||||
return canAddItem;
|
||||
};
|
||||
Choices.prototype._canAddItem = function (activeItems, value) {
|
||||
var canAddItem = true;
|
||||
var notice = typeof this.config.addItemText === 'function' ? this.config.addItemText(value) : this.config.addItemText;
|
||||
|
@ -1354,16 +1360,19 @@ var Choices = /** @class */function () {
|
|||
var target = event.target;
|
||||
var enterKey = constants_1.KEY_CODES.ENTER_KEY;
|
||||
var targetWasButton = target && target.hasAttribute('data-button');
|
||||
if (this._isTextElement && target && target.value) {
|
||||
var addedItem = false;
|
||||
if (target && target.value) {
|
||||
var value = this.input.value;
|
||||
var canAddItem = this._canAddItem(activeItems, value);
|
||||
if (canAddItem.response) {
|
||||
var canAddChoice = this._canAddChoice(activeItems, value);
|
||||
if (this._isTextElement && canAddItem.response || !this._isTextElement && canAddChoice.response) {
|
||||
this.hideDropdown(true);
|
||||
this._addItem({
|
||||
value: value
|
||||
});
|
||||
this._triggerChange(value);
|
||||
this.clearInput();
|
||||
addedItem = true;
|
||||
}
|
||||
}
|
||||
if (targetWasButton) {
|
||||
|
@ -1373,13 +1382,17 @@ var Choices = /** @class */function () {
|
|||
if (hasActiveDropdown) {
|
||||
var highlightedChoice = this.dropdown.getChild(".".concat(this.config.classNames.highlightedState));
|
||||
if (highlightedChoice) {
|
||||
// add enter keyCode value
|
||||
if (addedItem) {
|
||||
this.unhighlightAll();
|
||||
} else {
|
||||
if (activeItems[0]) {
|
||||
// add enter keyCode value
|
||||
activeItems[0].keyCode = enterKey; // eslint-disable-line no-param-reassign
|
||||
}
|
||||
|
||||
this._handleChoiceAction(activeItems, highlightedChoice);
|
||||
}
|
||||
}
|
||||
event.preventDefault();
|
||||
} else if (this._isSelectOneElement) {
|
||||
this.showDropdown();
|
||||
|
@ -2874,6 +2887,7 @@ exports.DEFAULT_CONFIG = {
|
|||
silent: false,
|
||||
renderChoiceLimit: -1,
|
||||
maxItemCount: -1,
|
||||
addChoices: false,
|
||||
addItems: true,
|
||||
addItemFilter: null,
|
||||
removeItems: true,
|
||||
|
@ -4168,7 +4182,7 @@ function getMergeFunction(key, options) {
|
|||
function getEnumerableOwnPropertySymbols(target) {
|
||||
return Object.getOwnPropertySymbols
|
||||
? Object.getOwnPropertySymbols(target).filter(function(symbol) {
|
||||
return target.propertyIsEnumerable(symbol)
|
||||
return Object.propertyIsEnumerable.call(target, symbol)
|
||||
})
|
||||
: []
|
||||
}
|
||||
|
@ -6816,15 +6830,6 @@ function applyMiddleware() {
|
|||
};
|
||||
}
|
||||
|
||||
/*
|
||||
* This is a dummy function to check if the function name has been altered by minification.
|
||||
* If the function has been minified and NODE_ENV !== 'production', warn the user.
|
||||
*/
|
||||
|
||||
function isCrushed() {}
|
||||
|
||||
if (false) {}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
2
public/assets/scripts/choices.min.js
vendored
2
public/assets/scripts/choices.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -1 +1 @@
|
|||
/*! choices.js v10.2.0 | © 2022 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
|
||||
/*! choices.js v10.2.0 | © 2023 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
|
||||
|
|
|
@ -397,6 +397,15 @@
|
|||
multiple
|
||||
></select>
|
||||
</div>
|
||||
|
||||
<div data-test-hook="add-choices">
|
||||
<label for="choices-add-choices">Add choices</label>
|
||||
<select class="form-control" name="choices-add-choices" id="choices-add-choices" multiple>
|
||||
<option value="Choice 1">Choice 1</option>
|
||||
<option value="Choice 2">Choice 2</option>
|
||||
<option value="Choice 3">Choice 3</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
|
@ -622,6 +631,8 @@
|
|||
},
|
||||
],
|
||||
});
|
||||
|
||||
new Choices('#choices-add-choices', { addChoices: true });
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
|
|
@ -417,6 +417,16 @@
|
|||
<button class="destroy">Destroy</button>
|
||||
<button class="init">Init</button>
|
||||
</div>
|
||||
|
||||
<div data-test-hook="add-choices">
|
||||
<label for="choices-add-choices">Add choices</label>
|
||||
<select class="form-control" name="choices-add-choices" id="choices-add-choices">
|
||||
<option value="Choice 1">Choice 1</option>
|
||||
<option value="Choice 2">Choice 2</option>
|
||||
<option value="Choice 3">Choice 3</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
|
@ -676,6 +686,8 @@
|
|||
document.querySelector('button.init').addEventListener('click', () => {
|
||||
newDestroyInitChoices.init();
|
||||
});
|
||||
|
||||
new Choices('#choices-add-choices', { addChoices: true });
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
|
1
public/types/src/scripts/choices.d.ts
vendored
1
public/types/src/scripts/choices.d.ts
vendored
|
@ -150,6 +150,7 @@ declare class Choices implements Choices {
|
|||
_stopLoading(): void;
|
||||
_handleLoadingState(setLoading?: boolean): void;
|
||||
_handleSearch(value: string): void;
|
||||
_canAddChoice(activeItems: Item[], value: string): Notice;
|
||||
_canAddItem(activeItems: Item[], value: string): Notice;
|
||||
_searchChoices(value: string): number;
|
||||
_addEventListeners(): void;
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1 +1 @@
|
|||
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../../../../src/scripts/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAG/C,eAAO,MAAM,kBAAkB,EAAE,UA2BhC,CAAC;AAEF,eAAO,MAAM,cAAc,EAAE,OA+C5B,CAAC"}
|
||||
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../../../../src/scripts/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAG/C,eAAO,MAAM,kBAAkB,EAAE,UA2BhC,CAAC;AAEF,eAAO,MAAM,cAAc,EAAE,OAgD5B,CAAC"}
|
|
@ -96,6 +96,14 @@ export interface Options {
|
|||
* @default -1
|
||||
*/
|
||||
maxItemCount: number;
|
||||
/**
|
||||
* Whether a user can add choices dynamically.
|
||||
*
|
||||
* **Input types affected:** select-one, select-multiple
|
||||
*
|
||||
* @default false
|
||||
*/
|
||||
addChoices: boolean;
|
||||
/**
|
||||
* Whether a user can add items.
|
||||
*
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../../../../../src/scripts/interfaces/options.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,SAAS,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC;;;;;;;;GAQG;AACH,MAAM,WAAW,OAAO;IACtB;;;;;;OAMG;IACH,MAAM,EAAE,OAAO,CAAC;IAEhB;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACH,KAAK,EAAE,MAAM,E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AAQ,CAAC;IAEzC;;;;;;OAMG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;;;;;OAMG;IACH,aAAa,EAAE,MAAM,GAAG,KAAK,CAAC,cAAc,CAAC;IAE7C;;;;;;OAMG;IACH,aAAa,EAAE,MAAM,GAAG,KAAK,CAAC,cAAc,CAAC;IAE7C;;;;;;OAMG;IACH,cAAc,EAAE,MAAM,CAAC;IAEvB;;;;;;;;;OASG;IACH,WAAW,EAAE,MAAM,GAAG,KAAK,CAAC,mBAAmB,CAAC;IAEhD;;;;OAIG;IACH,cAAc,EAAE,MAAM,GAAG,KAAK,CAAC,oBAAoB,CAAC;IAEpD;;;;;;OAMG;IACH,iBAAiB,EAAE,MAAM,GAAG,KAAK,CAAC,oBAAoB,CAAC;IAEvD;;;;;;;;;OASG;IACH,aAAa,EAAE,KAAK,CAAC,oBAAoB,CAAC;IAE1C;;;;OAIG;IACH,UAAU,EAAE,UAAU,CAAC;IAEvB;;OAEG;IACH,WAAW,EAAE,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;IAExC;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;;;;;;OAQG;IACH,cAAc,EAAE,CAAC,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;IAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,yBAAyB,EAAE,CAAC,CAAC,QAAQ,EAAE,KAAK,CAAC,OAAO,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;CACvE"}
|
|
@ -825,12 +825,12 @@ class Choices implements Choices {
|
|||
);
|
||||
}
|
||||
|
||||
// If we have choices to show
|
||||
const { activeItems } = this._store; // If we have choices to show
|
||||
|
||||
if (
|
||||
choiceListFragment.childNodes &&
|
||||
choiceListFragment.childNodes.length > 0
|
||||
) {
|
||||
const { activeItems } = this._store;
|
||||
const canAddItem = this._canAddItem(activeItems, this.input.value);
|
||||
|
||||
// ...and we can select them
|
||||
|
@ -844,18 +844,21 @@ class Choices implements Choices {
|
|||
}
|
||||
} else {
|
||||
// Otherwise show a notice
|
||||
let dropdownItem;
|
||||
let notice;
|
||||
const canAddChoice = this._canAddChoice(activeItems, this.input.value);
|
||||
|
||||
if (this._isSearching) {
|
||||
notice =
|
||||
let dropdownItem;
|
||||
|
||||
if (canAddChoice.response) {
|
||||
dropdownItem = this._getTemplate('notice', canAddChoice.notice);
|
||||
} else if (this._isSearching) {
|
||||
const notice =
|
||||
typeof this.config.noResultsText === 'function'
|
||||
? this.config.noResultsText()
|
||||
: this.config.noResultsText;
|
||||
|
||||
dropdownItem = this._getTemplate('notice', notice, 'no-results');
|
||||
} else {
|
||||
notice =
|
||||
const notice =
|
||||
typeof this.config.noChoicesText === 'function'
|
||||
? this.config.noChoicesText()
|
||||
: this.config.noChoicesText;
|
||||
|
@ -1260,6 +1263,14 @@ class Choices implements Choices {
|
|||
}
|
||||
}
|
||||
|
||||
_canAddChoice(activeItems: Item[], value: string): Notice {
|
||||
const canAddItem = this._canAddItem(activeItems, value);
|
||||
|
||||
canAddItem.response = this.config.addChoices && canAddItem.response;
|
||||
|
||||
return canAddItem;
|
||||
}
|
||||
|
||||
_canAddItem(activeItems: Item[], value: string): Notice {
|
||||
let canAddItem = true;
|
||||
let notice =
|
||||
|
@ -1555,16 +1566,22 @@ class Choices implements Choices {
|
|||
const { ENTER_KEY: enterKey } = KEY_CODES;
|
||||
const targetWasButton =
|
||||
target && (target as HTMLElement).hasAttribute('data-button');
|
||||
let addedItem = false;
|
||||
|
||||
if (this._isTextElement && target && (target as HTMLInputElement).value) {
|
||||
if (target && (target as HTMLInputElement).value) {
|
||||
const { value } = this.input;
|
||||
const canAddItem = this._canAddItem(activeItems, value);
|
||||
const canAddChoice = this._canAddChoice(activeItems, value);
|
||||
|
||||
if (canAddItem.response) {
|
||||
if (
|
||||
(this._isTextElement && canAddItem.response) ||
|
||||
(!this._isTextElement && canAddChoice.response)
|
||||
) {
|
||||
this.hideDropdown(true);
|
||||
this._addItem({ value });
|
||||
this._triggerChange(value);
|
||||
this.clearInput();
|
||||
addedItem = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1579,12 +1596,16 @@ class Choices implements Choices {
|
|||
);
|
||||
|
||||
if (highlightedChoice) {
|
||||
// add enter keyCode value
|
||||
if (addedItem) {
|
||||
this.unhighlightAll();
|
||||
} else {
|
||||
if (activeItems[0]) {
|
||||
// add enter keyCode value
|
||||
activeItems[0].keyCode = enterKey; // eslint-disable-line no-param-reassign
|
||||
}
|
||||
this._handleChoiceAction(activeItems, highlightedChoice);
|
||||
}
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
} else if (this._isSelectOneElement) {
|
||||
|
|
|
@ -37,6 +37,7 @@ export const DEFAULT_CONFIG: Options = {
|
|||
silent: false,
|
||||
renderChoiceLimit: -1,
|
||||
maxItemCount: -1,
|
||||
addChoices: false,
|
||||
addItems: true,
|
||||
addItemFilter: null,
|
||||
removeItems: true,
|
||||
|
|
|
@ -102,6 +102,15 @@ export interface Options {
|
|||
*/
|
||||
maxItemCount: number;
|
||||
|
||||
/**
|
||||
* Whether a user can add choices dynamically.
|
||||
*
|
||||
* **Input types affected:** select-one, select-multiple
|
||||
*
|
||||
* @default false
|
||||
*/
|
||||
addChoices: boolean;
|
||||
|
||||
/**
|
||||
* Whether a user can add items.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue