Skip to content

Commit

Permalink
feat: Add originalEvent property to select events
Browse files Browse the repository at this point in the history
Allows distinguishing between click and keydown selection.
  • Loading branch information
briandipalma authored and LeaVerou committed Mar 14, 2019
1 parent fac0676 commit f8bee8d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
14 changes: 8 additions & 6 deletions awesomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ var _ = function (input, o) {
if(me.opened) {
if (c === 13 && me.selected) { // Enter
evt.preventDefault();
me.select();
me.select(undefined, undefined, evt);
}
else if (c === 9 && me.selected && me.tabSelect) {
me.select();
me.select(undefined, undefined, evt);
}
else if (c === 27) { // Esc
me.close({ reason: "esc" });
Expand Down Expand Up @@ -114,7 +114,7 @@ var _ = function (input, o) {

if (li && evt.button === 0) { // Only select on left click
evt.preventDefault();
me.select(li, evt.target);
me.select(li, evt.target, evt);
}
}
}
Expand Down Expand Up @@ -269,7 +269,7 @@ _.prototype = {
}
},

select: function (selected, origin) {
select: function (selected, origin, originalEvent) {
if (selected) {
this.index = $.siblingIndex(selected);
} else {
Expand All @@ -281,14 +281,16 @@ _.prototype = {

var allowed = $.fire(this.input, "awesomplete-select", {
text: suggestion,
origin: origin || selected
origin: origin || selected,
originalEvent: originalEvent
});

if (allowed) {
this.replace(suggestion);
this.close({ reason: "select" });
$.fire(this.input, "awesomplete-selectcomplete", {
text: suggestion
text: suggestion,
originalEvent: originalEvent
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,12 @@ <h1>Events</h1>
<tbody>
<tr>
<td><code>awesomplete-select</code></td>
<td>The user has made a selection (either via pressing enter or clicking on an item), but it has not been applied yet. Callback will be passed an object with <code>text</code> (selected suggestion) and <code>origin</code> (DOM element) properties.</td>
<td>The user has made a selection (either via pressing enter or clicking on an item), but it has not been applied yet. Callback will be passed an object with <code>text</code> (selected suggestion), <code>origin</code> (DOM element) properties and <code>originalEvent</code> the original triggering DOM event.</td>
<td>Yes. The selection will not be applied and the popup will not close.</td>
</tr>
<tr>
<td><code>awesomplete-selectcomplete</code></td>
<td>The user has made a selection (either via pressing enter or clicking on an item), and it has been applied. Callback will be passed an object with a <code>text</code> property containing the selected suggestion.</td>
<td>The user has made a selection (either via pressing enter or clicking on an item), and it has been applied. Callback will be passed an object with a <code>text</code> property containing the selected suggestion and <code>originalEvent</code> the original triggering DOM event.</td>
<td>No</td>
</tr>
<tr>
Expand Down
4 changes: 2 additions & 2 deletions test/events/clickSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("click event", function () {
describe("on left click", function () {
it("selects item", function () {
var event = $.fire(this.target, "click", { button: 0 });
expect(this.subject.select).toHaveBeenCalledWith(this.li, this.target);
expect(this.subject.select).toHaveBeenCalledWith(this.li, this.target, event);
});
});

Expand All @@ -46,7 +46,7 @@ describe("click event", function () {
describe("on left click", function () {
it("selects item", function () {
var event = $.fire(this.target, "click", { button: 0 });
expect(this.subject.select).toHaveBeenCalledWith(this.li, this.target);
expect(this.subject.select).toHaveBeenCalledWith(this.li, this.target, event);
expect(event.defaultPrevented).toBe(true);
});
});
Expand Down

0 comments on commit f8bee8d

Please sign in to comment.