Eslint fixes

This commit is contained in:
Josh Johnson 2017-08-15 09:29:42 +01:00
parent 7967874597
commit 6b3437627d
6 changed files with 156 additions and 184 deletions

View file

@ -1,80 +1,62 @@
export const addItem = (value, label, id, choiceId, groupId, customProperties, placeholder, keyCode) => {
return {
type: 'ADD_ITEM',
value,
label,
id,
choiceId,
groupId,
customProperties,
placeholder,
keyCode,
};
};
export const addItem = (value, label, id, choiceId, groupId, customProperties, placeholder, keyCode) => ({
type: 'ADD_ITEM',
value,
label,
id,
choiceId,
groupId,
customProperties,
placeholder,
keyCode,
});
export const removeItem = (id, choiceId) => {
return {
type: 'REMOVE_ITEM',
id,
choiceId,
};
};
export const removeItem = (id, choiceId) => ({
type: 'REMOVE_ITEM',
id,
choiceId,
});
export const highlightItem = (id, highlighted) => {
return {
type: 'HIGHLIGHT_ITEM',
id,
highlighted,
};
};
export const highlightItem = (id, highlighted) => ({
type: 'HIGHLIGHT_ITEM',
id,
highlighted,
});
export const addChoice = (value, label, id, groupId, disabled, elementId, customProperties, placeholder, keyCode) => {
return {
type: 'ADD_CHOICE',
value,
label,
id,
groupId,
disabled,
elementId,
customProperties,
placeholder,
keyCode
};
};
export const addChoice = (value, label, id, groupId, disabled, elementId, customProperties, placeholder, keyCode) => ({
type: 'ADD_CHOICE',
value,
label,
id,
groupId,
disabled,
elementId,
customProperties,
placeholder,
keyCode,
});
export const filterChoices = (results) => {
return {
type: 'FILTER_CHOICES',
results,
};
};
export const filterChoices = results => ({
type: 'FILTER_CHOICES',
results,
});
export const activateChoices = (active = true) => {
return {
type: 'ACTIVATE_CHOICES',
active,
};
};
export const activateChoices = (active = true) => ({
type: 'ACTIVATE_CHOICES',
active,
});
export const clearChoices = () => {
return {
type: 'CLEAR_CHOICES',
};
};
export const clearChoices = () => ({
type: 'CLEAR_CHOICES',
});
export const addGroup = (value, id, active, disabled) => {
return {
type: 'ADD_GROUP',
value,
id,
active,
disabled,
};
};
export const addGroup = (value, id, active, disabled) => ({
type: 'ADD_GROUP',
value,
id,
active,
disabled,
});
export const clearAll = () => {
return {
type: 'CLEAR_ALL',
};
};
export const clearAll = () => ({
type: 'CLEAR_ALL',
});

View file

@ -43,9 +43,9 @@ class Choices {
if (isType('String', element)) {
const elements = document.querySelectorAll(element);
if (elements.length > 1) {
for (let i = 1; i < elements.length; i++) {
for (let i = 1; i < elements.length; i += 1) {
const el = elements[i];
new Choices(el, userConfig);
return new Choices(el, userConfig);
}
}
}
@ -132,7 +132,7 @@ class Choices {
if (this.config.renderSelectedChoices !== 'auto' && this.config.renderSelectedChoices !== 'always') {
if (!this.config.silent) {
console.warn(
'renderSelectedChoices: Possible values are \'auto\' and \'always\'. Falling back to \'auto\'.'
'renderSelectedChoices: Possible values are \'auto\' and \'always\'. Falling back to \'auto\'.',
);
}
this.config.renderSelectedChoices = 'auto';
@ -155,7 +155,7 @@ class Choices {
if (!this.config.silent) {
console.error('Passed element not found');
}
return;
return false;
}
this.isTextElement = this.passedElement.type === 'text';
@ -179,8 +179,8 @@ class Choices {
this.placeholder = false;
if (!this.isSelectOneElement) {
this.placeholder = this.config.placeholder ?
(this.config.placeholderValue || this.passedElement.getAttribute('placeholder')) :
false;
(this.config.placeholderValue || this.passedElement.getAttribute('placeholder')) :
false;
}
// Assign preset choices from passed object
@ -229,7 +229,7 @@ class Choices {
if (canInit) {
// If element has already been initialised with Choices
if (this.passedElement.getAttribute('data-choice') === 'active') {
return;
return false;
}
// Let's go
@ -415,7 +415,7 @@ class Choices {
}
// Add each choice to dropdown within range
for (let i = 0; i < choiceLimit; i++) {
for (let i = 0; i < choiceLimit; i += 1) {
if (sortedChoices[i]) {
appendChoice(sortedChoices[i]);
}
@ -753,12 +753,12 @@ class Choices {
const body = document.body;
const html = document.documentElement;
const winHeight = Math.max(
body.scrollHeight,
body.offsetHeight,
html.clientHeight,
html.scrollHeight,
html.offsetHeight
);
body.scrollHeight,
body.offsetHeight,
html.clientHeight,
html.scrollHeight,
html.offsetHeight,
);
this.containerOuter.classList.add(this.config.classNames.openState);
this.containerOuter.setAttribute('aria-expanded', 'true');
@ -886,10 +886,9 @@ class Choices {
item.value,
item.label,
true,
false,
-1,
false, -1,
item.customProperties,
item.placeholder
item.placeholder,
);
} else {
this._addItem(
@ -898,7 +897,7 @@ class Choices {
item.id,
undefined,
item.customProperties,
item.placeholder
item.placeholder,
);
}
} else if (itemType === 'String') {
@ -907,8 +906,7 @@ class Choices {
item,
item,
true,
false,
-1,
false, -1,
null,
);
} else {
@ -1023,7 +1021,7 @@ class Choices {
*/
clearStore() {
this.store.dispatch(
clearAll()
clearAll(),
);
return this;
}
@ -1034,7 +1032,7 @@ class Choices {
* @public
*/
clearInput() {
if (this.input.value){
if (this.input.value) {
this.input.value = '';
}
if (!this.isSelectOneElement) {
@ -1118,7 +1116,7 @@ class Choices {
return this;
}
/* ===== End of Public functions ======*/
/* ===== End of Public functions ====== */
/* =============================================
= Private functions =
@ -1136,7 +1134,7 @@ class Choices {
}
triggerEvent(this.passedElement, 'change', {
value
value,
});
}
@ -1155,7 +1153,7 @@ class Choices {
// If we are clicking on a button
if (this.config.removeItems && this.config.removeItemButton) {
const itemId = element.parentNode.getAttribute('data-id');
const itemToRemove = activeItems.find((item) => item.id === parseInt(itemId, 10));
const itemToRemove = activeItems.find(item => item.id === parseInt(itemId, 10));
// Remove item associated with button
this._removeItem(itemToRemove);
@ -1180,7 +1178,7 @@ class Choices {
placeholderChoice.id,
placeholderChoice.groupId,
null,
placeholderChoice.placeholder
placeholderChoice.placeholder,
);
this._triggerChange(placeholderChoice.value);
}
@ -1238,7 +1236,7 @@ class Choices {
// If we are clicking on an option
const id = element.getAttribute('data-id');
const choice = this.store.getChoiceById(id);
const passedKeyCode = activeItems[0] && activeItems[0].keyCode ? activeItems[0].keyCode : null;
const passedKeyCode = activeItems[0] && activeItems[0].keyCode ? activeItems[0].keyCode : null;
const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState);
// Update choice keyCode
@ -1259,7 +1257,7 @@ class Choices {
choice.groupId,
choice.customProperties,
choice.placeholder,
choice.keyCode
choice.keyCode,
);
this._triggerChange(choice.value);
}
@ -1342,8 +1340,7 @@ class Choices {
return item.value === value;
});
if (
!isUnique &&
if (!isUnique &&
!this.config.duplicateItems &&
!this.isSelectOneElement &&
canAddItem
@ -1417,7 +1414,7 @@ class Choices {
result,
groupId,
value,
label
label,
);
} else {
this._addChoice(
@ -1427,7 +1424,7 @@ class Choices {
result.disabled,
undefined,
result.customProperties,
result.placeholder
result.placeholder,
);
}
});
@ -1467,7 +1464,7 @@ class Choices {
this.highlightPosition = 0;
this.isSearching = true;
this.store.dispatch(
filterChoices(results)
filterChoices(results),
);
return results.length;
@ -1503,13 +1500,13 @@ class Choices {
// Trigger search event
triggerEvent(this.passedElement, 'search', {
value,
resultCount
resultCount,
});
} else if (hasUnactiveChoices) {
// Otherwise reset choices to active
this.isSearching = false;
this.store.dispatch(
activateChoices(true)
activateChoices(true),
);
}
}
@ -1663,13 +1660,10 @@ class Choices {
}
this._handleChoiceAction(activeItems, highlighted);
}
} else if (this.isSelectOneElement) {
} else if (this.isSelectOneElement && !hasActiveDropdown) {
// Open single select dropdown if it's not active
if (!hasActiveDropdown) {
this.showDropdown(true);
e.preventDefault();
}
this.showDropdown(true);
e.preventDefault();
}
};
@ -1771,7 +1765,6 @@ class Choices {
if (this.isTextElement) {
const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState);
if (value) {
if (canAddItem.notice) {
const dropdownItem = this._getTemplate('notice', canAddItem.notice);
this.dropdown.innerHTML = dropdownItem.outerHTML;
@ -1797,7 +1790,7 @@ class Choices {
if (!this.isTextElement && this.isSearching) {
this.isSearching = false;
this.store.dispatch(
activateChoices(true)
activateChoices(true),
);
}
} else if (this.canSearch && canAddItem.response) {
@ -1849,11 +1842,9 @@ class Choices {
if (document.activeElement !== this.input) {
this.input.focus();
}
} else {
if (!hasActiveDropdown) {
} else if (!hasActiveDropdown) {
// If a select box, we want to show the dropdown
this.showDropdown(true);
}
this.showDropdown(true);
}
}
// Prevents focus event firing
@ -1920,13 +1911,11 @@ class Choices {
if (document.activeElement !== this.input) {
this.input.focus();
}
} else if (this.canSearch) {
this.showDropdown(true);
} else {
if (this.canSearch) {
this.showDropdown(true);
} else {
this.showDropdown();
this.containerOuter.focus();
}
this.showDropdown();
this.containerOuter.focus();
}
} else if (this.isSelectOneElement && target !== this.input && !this.dropdown.contains(target)) {
this.hideDropdown(true);
@ -2104,7 +2093,9 @@ class Choices {
// Scroll position of dropdown
const containerScrollPos = this.choiceList.scrollTop + dropdownHeight;
// Difference between the choice and scroll position
const endPoint = direction > 0 ? ((this.choiceList.scrollTop + choicePos) - containerScrollPos) : choice.offsetTop;
const endPoint = direction > 0 ? (
(this.choiceList.scrollTop + choicePos) - containerScrollPos) :
choice.offsetTop;
const animateScroll = () => {
const strength = 4;
@ -2199,7 +2190,7 @@ class Choices {
*/
_addItem(value, label = null, choiceId = -1, groupId = -1, customProperties = null, placeholder = false, keyCode = null) {
let passedValue = isType('String', value) ? value.trim() : value;
let passedKeyCode = keyCode;
const passedKeyCode = keyCode;
const items = this.store.getItems();
const passedLabel = label || passedValue;
const passedOptionId = parseInt(choiceId, 10) || -1;
@ -2229,8 +2220,8 @@ class Choices {
groupId,
customProperties,
placeholder,
passedKeyCode
)
passedKeyCode,
),
);
if (this.isSelectOneElement) {
@ -2244,14 +2235,14 @@ class Choices {
value: passedValue,
label: passedLabel,
groupValue: group.value,
keyCode: passedKeyCode
keyCode: passedKeyCode,
});
} else {
triggerEvent(this.passedElement, 'addItem', {
id,
value: passedValue,
label: passedLabel,
keyCode: passedKeyCode
keyCode: passedKeyCode,
});
}
@ -2277,7 +2268,7 @@ class Choices {
const group = groupId >= 0 ? this.store.getGroupById(groupId) : null;
this.store.dispatch(
removeItem(id, choiceId)
removeItem(id, choiceId),
);
if (group && group.value) {
@ -2330,8 +2321,8 @@ class Choices {
choiceElementId,
customProperties,
placeholder,
keyCode
)
keyCode,
),
);
if (isSelected) {
@ -2342,7 +2333,7 @@ class Choices {
undefined,
customProperties,
placeholder,
keyCode
keyCode,
);
}
}
@ -2354,7 +2345,7 @@ class Choices {
*/
_clearChoices() {
this.store.dispatch(
clearChoices()
clearChoices(),
);
}
@ -2369,7 +2360,7 @@ class Choices {
*/
_addGroup(group, id, valueKey = 'value', labelKey = 'label') {
const groupChoices = isType('Object', group) ? group.choices : Array.from(group.getElementsByTagName('OPTION'));
const groupId = id ? id : Math.floor(new Date().valueOf() * Math.random());
const groupId = id || Math.floor(new Date().valueOf() * Math.random());
const isDisabled = group.disabled ? group.disabled : false;
if (groupChoices) {
@ -2378,8 +2369,8 @@ class Choices {
group.label,
groupId,
true,
isDisabled
)
isDisabled,
),
);
groupChoices.forEach((option) => {
@ -2391,7 +2382,7 @@ class Choices {
isOptDisabled,
groupId,
option.customProperties,
option.placeholder
option.placeholder,
);
});
} else {
@ -2400,8 +2391,8 @@ class Choices {
group.label,
group.id,
false,
group.disabled
)
group.disabled,
),
);
}
}
@ -2453,8 +2444,7 @@ class Choices {
`),
itemList: () => {
const localClasses = classNames(
globalClasses.list,
{
globalClasses.list, {
[globalClasses.listSingle]: (this.isSelectOneElement),
[globalClasses.listItems]: (!this.isSelectOneElement),
},
@ -2471,8 +2461,7 @@ class Choices {
`),
item: (data) => {
let localClasses = classNames(
globalClasses.item,
{
globalClasses.item, {
[globalClasses.highlightedState]: data.highlighted,
[globalClasses.itemSelectable]: !data.highlighted,
[globalClasses.placeholder]: data.placeholder,
@ -2481,8 +2470,7 @@ class Choices {
if (this.config.removeItemButton) {
localClasses = classNames(
globalClasses.item,
{
globalClasses.item, {
[globalClasses.highlightedState]: data.highlighted,
[globalClasses.itemSelectable]: !data.disabled,
[globalClasses.placeholder]: data.placeholder,
@ -2551,8 +2539,7 @@ class Choices {
`),
choiceGroup: (data) => {
const localClasses = classNames(
globalClasses.group,
{
globalClasses.group, {
[globalClasses.itemDisabled]: data.disabled,
},
);
@ -2576,8 +2563,7 @@ class Choices {
choice: (data) => {
const localClasses = classNames(
globalClasses.item,
globalClasses.itemChoice,
{
globalClasses.itemChoice, {
[globalClasses.itemDisabled]: data.disabled,
[globalClasses.itemSelectable]: !data.disabled,
[globalClasses.placeholder]: data.placeholder,
@ -2640,8 +2626,7 @@ class Choices {
notice: (label, type = '') => {
const localClasses = classNames(
globalClasses.item,
globalClasses.itemChoice,
{
globalClasses.itemChoice, {
[globalClasses.noResults]: (type === 'no-results'),
[globalClasses.noChoices]: (type === 'no-choices'),
},
@ -2692,7 +2677,7 @@ class Choices {
// Hide passed input
this.passedElement.classList.add(
this.config.classNames.input,
this.config.classNames.hiddenState
this.config.classNames.hiddenState,
);
// Remove element from tab index
@ -2701,7 +2686,7 @@ class Choices {
// Backup original styles if any
const origStyle = this.passedElement.getAttribute('style');
if (Boolean(origStyle)) {
if (origStyle) {
this.passedElement.setAttribute('data-choice-orig-style', origStyle);
}
@ -2789,7 +2774,7 @@ class Choices {
(shouldPreselect) ? choice.disabled : false,
undefined,
choice.customProperties,
choice.placeholder
choice.placeholder,
);
} else {
this._addChoice(
@ -2827,7 +2812,7 @@ class Choices {
}
}
/* ===== End of Private functions ======*/
/* ===== End of Private functions ====== */
}
module.exports = Choices;

View file

@ -18,7 +18,7 @@ const choices = (state = [], action) => {
score: 9999,
customProperties: action.customProperties,
placeholder: (action.placeholder || false),
keyCode: null
keyCode: null,
}];
}
@ -27,7 +27,8 @@ const choices = (state = [], action) => {
// If all choices need to be activated
if (action.activateOptions) {
newState = state.map((choice) => {
newState = state.map((obj) => {
const choice = obj;
choice.active = action.active;
return choice;
});
@ -35,7 +36,8 @@ const choices = (state = [], action) => {
// When an item is added and it has an associated choice,
// we want to disable it so it can't be chosen again
if (action.choiceId > -1) {
newState = state.map((choice) => {
newState = state.map((obj) => {
const choice = obj;
if (choice.id === parseInt(action.choiceId, 10)) {
choice.selected = true;
}
@ -50,7 +52,8 @@ const choices = (state = [], action) => {
// When an item is removed and it has an associated choice,
// we want to re-enable it so it can be chosen again
if (action.choiceId > -1) {
return state.map((choice) => {
return state.map((obj) => {
const choice = obj;
if (choice.id === parseInt(action.choiceId, 10)) {
choice.selected = false;
}
@ -63,10 +66,10 @@ const choices = (state = [], action) => {
case 'FILTER_CHOICES': {
const filteredResults = action.results;
const filteredState = state.map((choice) => {
const filteredState = state.map((obj) => {
const choice = obj;
// Set active state based on whether choice is
// within filtered results
choice.active = filteredResults.some((result) => {
if (result.item.id === choice.id) {
choice.score = result.score;
@ -82,14 +85,15 @@ const choices = (state = [], action) => {
}
case 'ACTIVATE_CHOICES': {
return state.map((choice) => {
return state.map((obj) => {
const choice = obj;
choice.active = action.active;
return choice;
});
}
case 'CLEAR_CHOICES': {
return state.choices = [];
return [];
}
default: {

View file

@ -10,7 +10,7 @@ const groups = (state = [], action) => {
}
case 'CLEAR_CHOICES': {
return state.groups = [];
return [];
}
default: {

View file

@ -12,10 +12,11 @@ const items = (state = [], action) => {
highlighted: false,
customProperties: action.customProperties,
placeholder: (action.placeholder || false),
keyCode: null
keyCode: null,
}];
return newState.map((item) => {
return newState.map((obj) => {
const item = obj;
if (item.highlighted) {
item.highlighted = false;
}
@ -25,7 +26,8 @@ const items = (state = [], action) => {
case 'REMOVE_ITEM': {
// Set item to inactive
return state.map((item) => {
return state.map((obj) => {
const item = obj;
if (item.id === action.id) {
item.active = false;
}
@ -34,7 +36,8 @@ const items = (state = [], action) => {
}
case 'HIGHLIGHT_ITEM': {
return state.map((item) => {
return state.map((obj) => {
const item = obj;
if (item.id === action.id) {
item.highlighted = action.highlighted;
}

View file

@ -1,11 +1,13 @@
import { createStore } from 'redux';
import rootReducer from './../reducers/index.js';
import rootReducer from './../reducers/index';
export default class Store {
constructor() {
this.store = createStore(
rootReducer
, window.devToolsExtension ? window.devToolsExtension() : undefined
rootReducer,
window.devToolsExtension ?
window.devToolsExtension() :
undefined,
);
}
@ -50,9 +52,7 @@ export default class Store {
*/
getItemsFilteredByActive() {
const items = this.getItems();
const values = items.filter((item) => {
return item.active === true;
}, []);
const values = items.filter(item => item.active === true, []);
return values;
}
@ -117,7 +117,7 @@ export default class Store {
getChoiceById(id) {
if (id) {
const choices = this.getChoicesFilteredByActive();
const foundChoice = choices.find((choice) => choice.id === parseInt(id, 10));
const foundChoice = choices.find(choice => choice.id === parseInt(id, 10));
return foundChoice;
}
return false;
@ -142,9 +142,9 @@ export default class Store {
const values = groups.filter((group) => {
const isActive = group.active === true && group.disabled === false;
const hasActiveOptions = choices.some((choice) => {
return choice.active === true && choice.disabled === false;
});
const hasActiveOptions = choices.some(choice =>
choice.active === true && choice.disabled === false,
);
return isActive && hasActiveOptions;
}, []);
@ -171,9 +171,7 @@ export default class Store {
const choices = this.getChoices();
const placeholderChoice = [...choices]
.reverse()
.find((choice) => {
return choice.placeholder === true;
});
.find(choice => choice.placeholder === true);
return placeholderChoice;
}