Skip to content

Commit

Permalink
Silent config option
Browse files Browse the repository at this point in the history
  • Loading branch information
jshjohnson committed May 18, 2017
1 parent 4ad0406 commit bc48904
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Or include Choices directly:

// Passing options (with default options)
const choices = new Choices(elements, {
silent: false,
items: [],
choices: [],
maxItemCount: -1,
Expand All @@ -66,7 +67,7 @@ Or include Choices directly:
duplicateItems: true,
delimiter: ',',
paste: true,
search: true,
searchEnabled: true,
searchChoices: true,
searchFloor: 1,
searchFields: ['label', 'value'],
Expand Down Expand Up @@ -133,6 +134,14 @@ Or include Choices directly:


## Configuration options
### silent
**Type:** `Boolean` **Default:** `false`

**Input types affected:** `text`, `select-single`, `select-multiple`

**Usage:** Optionally supress console errors and warnings.


### items
**Type:** `Array` **Default:** `[]`

Expand Down Expand Up @@ -239,19 +248,19 @@ Pass an array of objects:

**Usage:** Whether a user can paste into the input.

### search
### searchEnabled
**Type:** `Boolean` **Default:** `true`

**Input types affected:** `select-one`

**Usage:** Whether a user should be allowed to search avaiable choices. Note that multiple select boxes will always show search inputs.
**Usage:** Whether a search area should be shown. **Note:** Multiple select boxes will *always* show search areas.

### searchChoices
**Type:** `Boolean` **Default:** `true`

**Input types affected:** `select-one`

**Usage:** Whether the plugin should filter the choices by input or not. If `false`, the search event will still emit.
**Usage:** Whether choices should be filtered by input or not. If `false`, the search event will still emit, but choices will not be filtered.


### searchFields
Expand Down
37 changes: 23 additions & 14 deletions assets/scripts/src/choices.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Choices {
}

const defaultConfig = {
silent: false,
items: [],
choices: [],
maxItemCount: -1,
Expand All @@ -59,7 +60,7 @@ class Choices {
duplicateItems: true,
delimiter: ',',
paste: true,
search: true,
searchEnabled: true,
searchChoices: true,
searchFloor: 1,
searchFields: ['label', 'value'],
Expand Down Expand Up @@ -131,16 +132,19 @@ class Choices {
// Retrieve triggering element (i.e. element with 'data-choice' trigger)
this.element = element;
this.passedElement = isType('String', element) ? document.querySelector(element) : element;
this.isSelectElement = this.passedElement.type === 'select-one' || this.passedElement.type === 'select-multiple';
this.isTextElement = this.passedElement.type === 'text';
this.isSelectElement = this.passedElement.type === 'select-one' ||
this.passedElement.type === 'select-multiple';

if (!this.passedElement) {
console.error('Passed element not found');
if (!this.config.silent) {
console.error('Passed element not found');
}
return;
}

this.highlightPosition = 0;
this.canSearch = this.config.search;
this.canSearch = this.config.searchEnabled;

// Assing preset choices from passed object
this.presetChoices = this.config.choices;
Expand Down Expand Up @@ -179,7 +183,9 @@ class Choices {

// Cutting the mustard
const cuttingTheMustard = 'classList' in document.documentElement;
if (!cuttingTheMustard) console.error('Choices: Your browser doesn\'t support Choices');
if (!cuttingTheMustard && !this.config.silent) {
console.error('Choices: Your browser doesn\'t support Choices');
}

// Input type check
const isValidType = ['select-one', 'select-multiple', 'text'].some(type => type === this.passedElement.type);
Expand All @@ -193,7 +199,7 @@ class Choices {

// Let's go
this.init();
} else {
} else if (!this.config.silent) {
console.error('Incompatible input passed');
}
}
Expand Down Expand Up @@ -585,7 +591,9 @@ class Choices {
*/
removeItemsByValue(value) {
if (!value || !isType('String', value)) {
console.error('removeItemsByValue: No value was passed to be removed');
if (!this.config.silent) {
console.error('removeItemsByValue: No value was passed to be removed');
}
return;
}

Expand Down Expand Up @@ -825,10 +833,10 @@ class Choices {
if (foundChoice) {
if (!foundChoice.selected) {
this._addItem(foundChoice.value, foundChoice.label, foundChoice.id, foundChoice.groupId);
} else {
} else if (!this.config.silent) {
console.warn('Attempting to select choice already selected');
}
} else {
} else if (!this.config.silent) {
console.warn('Attempting to select choice that does not exist');
}
});
Expand Down Expand Up @@ -894,7 +902,7 @@ class Choices {
if (this.passedElement.type !== 'select-one') {
this._setInputWidth();
}
if (this.passedElement.type !== 'text' && this.config.search) {
if (this.passedElement.type !== 'text' && this.config.searchEnabled) {
this.isSearching = false;
this.store.dispatch(activateChoices(true));
}
Expand Down Expand Up @@ -1404,7 +1412,7 @@ class Choices {
this.showDropdown(true);
}

this.canSearch = this.config.search;
this.canSearch = this.config.searchEnabled;

const onAKey = () => {
// If CTRL + A or CMD + A have been pressed and there are items to select
Expand Down Expand Up @@ -2056,7 +2064,9 @@ class Choices {
*/
_removeItem(item) {
if (!item || !isType('Object', item)) {
console.error('removeItem: No item object was passed to be removed');
if (!this.config.silent) {
console.error('removeItem: No item object was passed to be removed');
}
return;
}

Expand Down Expand Up @@ -2327,8 +2337,7 @@ class Choices {
dropdown.appendChild(choiceList);
}

if ((this.passedElement.type === 'select-multiple' && this.canSearch) ||
this.passedElement.type === 'text') {
if (this.passedElement.type === 'select-multiple' || this.passedElement.type === 'text') {
containerInner.appendChild(input);
} else if (this.canSearch) {
dropdown.insertBefore(input, dropdown.firstChild);
Expand Down
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ <h2>Single select input</h2>
var multipleDefault = new Choices(document.getElementById('choices-multiple-groups'));

var multipleFetch = new Choices('#choices-multiple-remote-fetch', {
searchChoices: false,
placeholder: true,
placeholderValue: 'Pick an Strokes record',
maxItemCount: 5,
Expand Down Expand Up @@ -396,7 +397,7 @@ <h2>Single select input</h2>
});

var singleNoSearch = new Choices('#choices-single-no-search', {
search: false,
searchEnabled: false,
removeItemButton: true,
choices: [
{value: 'One', label: 'Label One'},
Expand Down
4 changes: 3 additions & 1 deletion tests/spec/choices_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('Choices', () => {
});

it('should have config options', function() {
expect(this.choices.config.silent).toEqual(jasmine.any(Boolean));
expect(this.choices.config.items).toEqual(jasmine.any(Array));
expect(this.choices.config.choices).toEqual(jasmine.any(Array));
expect(this.choices.config.maxItemCount).toEqual(jasmine.any(Number));
Expand All @@ -74,7 +75,8 @@ describe('Choices', () => {
expect(this.choices.config.duplicateItems).toEqual(jasmine.any(Boolean));
expect(this.choices.config.delimiter).toEqual(jasmine.any(String));
expect(this.choices.config.paste).toEqual(jasmine.any(Boolean));
expect(this.choices.config.search).toEqual(jasmine.any(Boolean));
expect(this.choices.config.searchEnabled).toEqual(jasmine.any(Boolean));
expect(this.choices.config.searchChoices).toEqual(jasmine.any(Boolean));
expect(this.choices.config.searchFloor).toEqual(jasmine.any(Number));
expect(this.choices.config.searchFields).toEqual(jasmine.any(Array) || jasmine.any(String));
expect(this.choices.config.position).toEqual(jasmine.any(String));
Expand Down

0 comments on commit bc48904

Please sign in to comment.