Skip to content

Commit f8bee8d

Browse files
briandipalmaLeaVerou
authored andcommitted
feat: Add originalEvent property to select events
Allows distinguishing between click and keydown selection.
1 parent fac0676 commit f8bee8d

3 files changed

Lines changed: 12 additions & 10 deletions

File tree

awesomplete.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ var _ = function (input, o) {
7777
if(me.opened) {
7878
if (c === 13 && me.selected) { // Enter
7979
evt.preventDefault();
80-
me.select();
80+
me.select(undefined, undefined, evt);
8181
}
8282
else if (c === 9 && me.selected && me.tabSelect) {
83-
me.select();
83+
me.select(undefined, undefined, evt);
8484
}
8585
else if (c === 27) { // Esc
8686
me.close({ reason: "esc" });
@@ -114,7 +114,7 @@ var _ = function (input, o) {
114114

115115
if (li && evt.button === 0) { // Only select on left click
116116
evt.preventDefault();
117-
me.select(li, evt.target);
117+
me.select(li, evt.target, evt);
118118
}
119119
}
120120
}
@@ -269,7 +269,7 @@ _.prototype = {
269269
}
270270
},
271271

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

282282
var allowed = $.fire(this.input, "awesomplete-select", {
283283
text: suggestion,
284-
origin: origin || selected
284+
origin: origin || selected,
285+
originalEvent: originalEvent
285286
});
286287

287288
if (allowed) {
288289
this.replace(suggestion);
289290
this.close({ reason: "select" });
290291
$.fire(this.input, "awesomplete-selectcomplete", {
291-
text: suggestion
292+
text: suggestion,
293+
originalEvent: originalEvent
292294
});
293295
}
294296
}

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,12 @@ <h1>Events</h1>
317317
<tbody>
318318
<tr>
319319
<td><code>awesomplete-select</code></td>
320-
<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>
320+
<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>
321321
<td>Yes. The selection will not be applied and the popup will not close.</td>
322322
</tr>
323323
<tr>
324324
<td><code>awesomplete-selectcomplete</code></td>
325-
<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>
325+
<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>
326326
<td>No</td>
327327
</tr>
328328
<tr>

test/events/clickSpec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe("click event", function () {
2828
describe("on left click", function () {
2929
it("selects item", function () {
3030
var event = $.fire(this.target, "click", { button: 0 });
31-
expect(this.subject.select).toHaveBeenCalledWith(this.li, this.target);
31+
expect(this.subject.select).toHaveBeenCalledWith(this.li, this.target, event);
3232
});
3333
});
3434

@@ -46,7 +46,7 @@ describe("click event", function () {
4646
describe("on left click", function () {
4747
it("selects item", function () {
4848
var event = $.fire(this.target, "click", { button: 0 });
49-
expect(this.subject.select).toHaveBeenCalledWith(this.li, this.target);
49+
expect(this.subject.select).toHaveBeenCalledWith(this.li, this.target, event);
5050
expect(event.defaultPrevented).toBe(true);
5151
});
5252
});

0 commit comments

Comments
 (0)