Skip to content

Commit

Permalink
Merge pull request #211 from oWallyson/master
Browse files Browse the repository at this point in the history
Added keyCode to choice objects
  • Loading branch information
jshjohnson authored Jul 19, 2017
2 parents d3ebae6 + dba6093 commit 40242f6
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 417 deletions.
675 changes: 345 additions & 330 deletions assets/scripts/dist/choices.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/scripts/dist/choices.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions assets/scripts/dist/choices.min.js

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions assets/scripts/src/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export const addItem = (value, label, id, choiceId, groupId, customProperties) => {
export const addItem = (value, label, id, choiceId, groupId, customProperties, keyCode) => {
return {
type: 'ADD_ITEM',
value,
label,
id,
choiceId,
groupId,
customProperties
customProperties,
keyCode
};
};

Expand All @@ -26,7 +27,7 @@ export const highlightItem = (id, highlighted) => {
};
};

export const addChoice = (value, label, id, groupId, disabled, elementId, customProperties) => {
export const addChoice = (value, label, id, groupId, disabled, elementId, customProperties, keyCode) => {
return {
type: 'ADD_CHOICE',
value,
Expand All @@ -35,7 +36,8 @@ export const addChoice = (value, label, id, groupId, disabled, elementId, custom
groupId,
disabled,
elementId: elementId,
customProperties
customProperties,
keyCode
};
};

Expand Down
44 changes: 31 additions & 13 deletions assets/scripts/src/choices.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,15 +835,17 @@ class Choices {
true,
false,
-1,
item.customProperties
item.customProperties,
null
);
} else {
this._addItem(
item.value,
item.label,
item.id,
undefined,
item.customProperties
item.customProperties,
null
);
}
} else if (itemType === 'String') {
Expand All @@ -853,7 +855,8 @@ class Choices {
item,
true,
false,
-1
-1,
null
);
} else {
this._addItem(item);
Expand Down Expand Up @@ -898,7 +901,8 @@ class Choices {
foundChoice.label,
foundChoice.id,
foundChoice.groupId,
foundChoice.customProperties
foundChoice.customProperties,
foundChoice.keyCode
);
} else if (!this.config.silent) {
console.warn('Attempting to select choice already selected');
Expand Down Expand Up @@ -948,7 +952,8 @@ class Choices {
result.selected,
result.disabled,
undefined,
result['customProperties']
result['customProperties'],
null
);
}
});
Expand Down Expand Up @@ -1155,8 +1160,12 @@ 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].keyCode !== null ? activeItems[0].keyCode : null
const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState);

// Update choice keyCode
choice.keyCode = passedKeyCode

triggerEvent(this.passedElement, 'choice', {
choice,
});
Expand All @@ -1170,7 +1179,8 @@ class Choices {
choice.label,
choice.id,
choice.groupId,
choice.customProperties
choice.customProperties,
choice.keyCode
);
this._triggerChange(choice.value);
}
Expand Down Expand Up @@ -1343,7 +1353,8 @@ class Choices {
result.selected,
result.disabled,
undefined,
result['customProperties']
result['customProperties'],
null
);
}
});
Expand Down Expand Up @@ -1539,7 +1550,7 @@ class Choices {
if (this.isTextElement && target.value) {
const value = this.input.value;
const canAddItem = this._canAddItem(activeItems, value);

// All is good, add
if (canAddItem.response) {
if (hasActiveDropdown) {
Expand All @@ -1562,6 +1573,8 @@ class Choices {

// If we have a highlighted choice
if (highlighted) {
// add enter keyCode value
activeItems[0].keyCode = enterKey
this._handleChoiceAction(activeItems, highlighted);
}

Expand Down Expand Up @@ -2099,8 +2112,9 @@ class Choices {
* @return {Object} Class instance
* @public
*/
_addItem(value, label = null, choiceId = -1, groupId = -1, customProperties = null) {
_addItem(value, label = null, choiceId = -1, groupId = -1, customProperties = null, keyCode = null) {
let passedValue = isType('String', value) ? value.trim() : value;
let passedKeyCode = keyCode
const items = this.store.getItems();
const passedLabel = label || passedValue;
const passedOptionId = parseInt(choiceId, 10) || -1;
Expand All @@ -2121,7 +2135,7 @@ class Choices {
passedValue += this.config.appendValue.toString();
}

this.store.dispatch(addItem(passedValue, passedLabel, id, passedOptionId, groupId, customProperties));
this.store.dispatch(addItem(passedValue, passedLabel, id, passedOptionId, groupId, customProperties, passedKeyCode));

if (this.isSelectOneElement) {
this.removeActiveItems(id);
Expand All @@ -2134,12 +2148,14 @@ class Choices {
value: passedValue,
label: passedLabel,
groupValue: group.value,
keyCode: passedKeyCode
});
} else {
triggerEvent(this.passedElement, 'addItem', {
id,
value: passedValue,
label: passedLabel,
keyCode: passedKeyCode
});
}

Expand Down Expand Up @@ -2195,7 +2211,7 @@ class Choices {
* @return
* @private
*/
_addChoice(value, label = null, isSelected = false, isDisabled = false, groupId = -1, customProperties = null) {
_addChoice(value, label = null, isSelected = false, isDisabled = false, groupId = -1, customProperties = null, keyCode = null) {
if (typeof value === 'undefined' || value === null) {
return;
}
Expand All @@ -2213,7 +2229,8 @@ class Choices {
groupId,
isDisabled,
choiceElementId,
customProperties
customProperties,
keyCode
));

if (isSelected) {
Expand All @@ -2222,7 +2239,8 @@ class Choices {
choiceLabel,
choiceId,
undefined,
customProperties
customProperties,
keyCode
);
}
}
Expand Down
3 changes: 2 additions & 1 deletion assets/scripts/src/reducers/choices.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const choices = (state = [], action) => {
selected: false,
active: true,
score: 9999,
customProperties: action.customProperties
customProperties: action.customProperties,
keyCode: null
}];
}

Expand Down
3 changes: 2 additions & 1 deletion assets/scripts/src/reducers/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const items = (state = [], action) => {
label: action.label,
active: true,
highlighted: false,
customProperties: action.customProperties
customProperties: action.customProperties,
keyCode: null
}];

return newState.map((item) => {
Expand Down
Loading

0 comments on commit 40242f6

Please sign in to comment.