use default parameters (#698)

* use default parameters

* Fix argument name in README
This commit is contained in:
Konstantin Vyatkin 2019-10-25 08:21:38 -04:00 committed by Josh Johnson
parent 69582349bb
commit ef2c70fb7a
2 changed files with 24 additions and 16 deletions

View file

@ -809,7 +809,7 @@ const values = example.getValue(true); // returns ['value 1', 'value 2'];
const valueArray = example.getValue(); // returns [{ active: true, choiceId: 1, highlighted: false, id: 1, label: 'Label 1', value: 'Value 1'}, { active: true, choiceId: 2, highlighted: false, id: 2, label: 'Label 2', value: 'Value 2'}]; const valueArray = example.getValue(); // returns [{ active: true, choiceId: 1, highlighted: false, id: 1, label: 'Label 1', value: 'Value 1'}, { active: true, choiceId: 2, highlighted: false, id: 2, label: 'Label 2', value: 'Value 2'}];
``` ```
### setValue(args); ### setValue(items);
**Input types affected:** `text` **Input types affected:** `text`
**Usage:** Set value of input based on an array of objects or strings. This behaves exactly the same as passing items via the `items` option but can be called after initialising Choices. **Usage:** Set value of input based on an array of objects or strings. This behaves exactly the same as passing items via the `items` option but can be called after initialising Choices.

View file

@ -390,12 +390,15 @@ class Choices {
return this._isSelectOneElement ? values[0] : values; return this._isSelectOneElement ? values[0] : values;
} }
setValue(args) { /**
* @param {string[] | import('../../types/index').Choices.Item[]} items
*/
setValue(items) {
if (!this.initialised) { if (!this.initialised) {
return this; return this;
} }
[...args].forEach(value => this._setChoiceOrItem(value)); items.forEach(value => this._setChoiceOrItem(value));
return this; return this;
} }
@ -608,8 +611,11 @@ class Choices {
} }
} }
_createGroupsFragment(groups, choices, fragment) { _createGroupsFragment(
const groupFragment = fragment || document.createDocumentFragment(); groups,
choices,
fragment = document.createDocumentFragment(),
) {
const getGroupChoices = group => const getGroupChoices = group =>
choices.filter(choice => { choices.filter(choice => {
if (this._isSelectOneElement) { if (this._isSelectOneElement) {
@ -630,17 +636,20 @@ class Choices {
const groupChoices = getGroupChoices(group); const groupChoices = getGroupChoices(group);
if (groupChoices.length >= 1) { if (groupChoices.length >= 1) {
const dropdownGroup = this._getTemplate('choiceGroup', group); const dropdownGroup = this._getTemplate('choiceGroup', group);
groupFragment.appendChild(dropdownGroup); fragment.appendChild(dropdownGroup);
this._createChoicesFragment(groupChoices, groupFragment, true); this._createChoicesFragment(groupChoices, fragment, true);
} }
}); });
return groupFragment; return fragment;
} }
_createChoicesFragment(choices, fragment, withinGroup = false) { _createChoicesFragment(
choices,
fragment = document.createDocumentFragment(),
withinGroup = false,
) {
// Create a fragment to store our list items (so we don't have to update the DOM for each item) // Create a fragment to store our list items (so we don't have to update the DOM for each item)
const choicesFragment = fragment || document.createDocumentFragment();
const { const {
renderSelectedChoices, renderSelectedChoices,
searchResultLimit, searchResultLimit,
@ -658,7 +667,7 @@ class Choices {
choice, choice,
this.config.itemSelectText, this.config.itemSelectText,
); );
choicesFragment.appendChild(dropdownItem); fragment.appendChild(dropdownItem);
} }
}; };
@ -704,13 +713,12 @@ class Choices {
} }
} }
return choicesFragment; return fragment;
} }
_createItemsFragment(items, fragment = null) { _createItemsFragment(items, fragment = document.createDocumentFragment()) {
// Create fragment to add elements to // Create fragment to add elements to
const { shouldSortItems, sortFn, removeItemButton } = this.config; const { shouldSortItems, sortFn, removeItemButton } = this.config;
const itemListFragment = fragment || document.createDocumentFragment();
// If sorting is enabled, filter items // If sorting is enabled, filter items
if (shouldSortItems && !this._isSelectOneElement) { if (shouldSortItems && !this._isSelectOneElement) {
@ -729,13 +737,13 @@ class Choices {
// Create new list element // Create new list element
const listItem = this._getTemplate('item', item, removeItemButton); const listItem = this._getTemplate('item', item, removeItemButton);
// Append it to list // Append it to list
itemListFragment.appendChild(listItem); fragment.appendChild(listItem);
}; };
// Add each list item to list // Add each list item to list
items.forEach(item => addItemToFragment(item)); items.forEach(item => addItemToFragment(item));
return itemListFragment; return fragment;
} }
_triggerChange(value) { _triggerChange(value) {