Skip to content

Commit

Permalink
Add support for spaceSelectsMatch. Closes #170.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Humphreys committed Jan 4, 2019
1 parent 9181229 commit 994fbf2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ Collection object shown with defaults:
// specify whether the menu should be positioned. Set to false and use in conjuction with menuContainer to create an inline menu
// (defaults to true)
positionMenu: true,

// when the spacebar is hit, select the current match
spaceSelectsMatch: false
}
```

Expand All @@ -190,7 +193,7 @@ The `lookup` column can also be passed a function to construct a string to query

```js
{
lookup: function (person) {
lookup: function (person, mentionText) {
return person.name + person.email;
}
}
Expand Down
26 changes: 18 additions & 8 deletions dist/tribute.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ <h5>Tribute on traditional form elements!</h5>

</div>

<script src="../dist/tribute.min.js?33289"></script>
<script src="../dist/tribute.js"></script>
<script>
// example of alternative callback
var tribute = new Tribute({
Expand All @@ -93,7 +93,8 @@ <h5>Tribute on traditional form elements!</h5>
}

return '@' + item.original.value;
}
},
spaceSelectsMatch: true
});

tribute.attach(document.getElementById('test'));
Expand Down
4 changes: 3 additions & 1 deletion src/Tribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Tribute {
allowSpaces = false,
replaceTextSuffix = null,
positionMenu = true,
spaceSelectsMatch = false,
}) {

this.menuSelected = 0
Expand All @@ -32,6 +33,7 @@ class Tribute {
this.replaceTextSuffix = replaceTextSuffix
this.positionMenu = positionMenu
this.hasTrailingSpace = false;
this.spaceSelectsMatch = spaceSelectsMatch;

if (values) {
this.collection = [{
Expand Down Expand Up @@ -68,7 +70,7 @@ class Tribute {
// array of objects or a function returning an array of objects
values: values,

requireLeadingSpace: requireLeadingSpace,
requireLeadingSpace: requireLeadingSpace
}]
}
else if (collection) {
Expand Down
19 changes: 13 additions & 6 deletions src/TributeEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class TributeEvents {
}, {
key: 27,
value: 'ESCAPE'
}, {
key: 32,
value: 'SPACE'
}, {
key: 38,
value: 'UP'
Expand Down Expand Up @@ -206,12 +209,16 @@ class TributeEvents {
this.callbacks().enter(e, el)
},
space: (e, el) => {
if (this.tribute.isActive && !this.tribute.allowSpaces) {
e.stopPropagation();
setTimeout(() => {
this.tribute.hideMenu();
this.tribute.isActive = false;
}, 0);
if (this.tribute.isActive) {
if (this.tribute.spaceSelectsMatch) {
this.callbacks().enter(e, el)
} else if (!this.tribute.allowSpaces) {
e.stopPropagation();
setTimeout(() => {
this.tribute.hideMenu();
this.tribute.isActive = false;
}, 0);
}
}
},
up: (e, el) => {
Expand Down

0 comments on commit 994fbf2

Please sign in to comment.