Skip to content

Commit d268d86

Browse files
committed
Merge pull request DefinitelyTyped#4657 from brad-jones/master
Added MousetrapInstance Interface.
2 parents 5156f56 + 50fffc5 commit d268d86

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

mousetrap/mousetrap-tests.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,18 @@ Mousetrap.trigger('esc', 'keyup');
4242

4343
Mousetrap.reset();
4444

45+
// Test that we can create an instance of mousetrap and attach the
46+
// event handler to the form element only, instead of the entire document.
47+
var element = document.querySelector('form');
48+
var instance = new Mousetrap(element);
49+
instance.bind('mod+s', function(){ console.log('Instance Saved'); });
50+
51+
// Test that the factory method works as well.
52+
Mousetrap(element).bind('mod+s', function(){ console.log('Factory Saved'); });
53+
4554
// Test that Mousetrap can be loaded as an external module.
4655
// Assume that if the externally-loaded module can be assigned to a variable with the type of global Mousetrap,
4756
// then everything is working correctly.
4857

4958
import importedMousetrap = require('mousetrap');
5059
var mousetrapModuleReference: typeof Mousetrap = importedMousetrap;
51-

mousetrap/mousetrap.d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Type definitions for Mousetrap 1.2.2
1+
// Type definitions for Mousetrap 1.5.x
22
// Project: http://craig.is/killing/mice
33
// Definitions by: Dániel Tar <https://github.com/qcz>
44
// Definitions: https://github.com/borisyankov/DefinitelyTyped
@@ -8,8 +8,19 @@ interface ExtendedKeyboardEvent extends KeyboardEvent {
88
}
99

1010
interface MousetrapStatic {
11+
(el: Element): MousetrapInstance;
12+
new (el: Element): MousetrapInstance;
1113
stopCallback: (e: ExtendedKeyboardEvent, element: Element, combo: string) => boolean;
14+
bind(keys: string, callback: (e: ExtendedKeyboardEvent, combo: string) => any, action?: string): void;
15+
bind(keyArray: string[], callback: (e: ExtendedKeyboardEvent, combo: string) => any, action?: string): void;
16+
unbind(keys: string, action?: string): void;
17+
unbind(keyArray: string[], action?: string): void;
18+
trigger(keys: string, action?: string): void;
19+
reset(): void;
20+
}
1221

22+
interface MousetrapInstance {
23+
stopCallback: (e: ExtendedKeyboardEvent, element: Element, combo: string) => boolean;
1324
bind(keys: string, callback: (e: ExtendedKeyboardEvent, combo: string) => any, action?: string): void;
1425
bind(keyArray: string[], callback: (e: ExtendedKeyboardEvent, combo: string) => any, action?: string): void;
1526
unbind(keys: string, action?: string): void;

0 commit comments

Comments
 (0)