Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Commit

Permalink
Reset key combo when tab loses focus
Browse files Browse the repository at this point in the history
Issue #1283
  • Loading branch information
pablosichert committed Nov 20, 2017
1 parent 05ace09 commit 1cb4254
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/components/Shortcuts/ShortcutProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ export default class ShortcutProvider extends Component {
componentWillMount() {
document.addEventListener('keydown', this.handleKeyDown);
document.addEventListener('keyup', this.handleKeyUp);
window.addEventListener('blur', this.handleBlur);
}

componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeyDown);
document.removeEventListener('keyup', this.handleKeyUp);
window.removeEventListener('blur', this.handleBlur);
}

handleKeyDown = event => {
Expand Down Expand Up @@ -76,6 +78,11 @@ export default class ShortcutProvider extends Component {
this.fired = {};
};

handleBlur = () => {
this.keySequence = [];
this.fired = {};
};

subscribe = (name, handler) => {
const { hotkeys, keymap } = this.props;

Expand Down
10 changes: 7 additions & 3 deletions src/components/Shortcuts/ShortcutProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('ShortcutProvider', () => {
it('should clean up event listeners', () => {
const listeners = [];

global.document = {
const listenerAPI = {
addEventListener: (event, handler) => {
listeners.push([event, handler]);
},
Expand All @@ -52,6 +52,9 @@ describe('ShortcutProvider', () => {
}
};

global.document = listenerAPI;
global.window = listenerAPI;

try {
const shortcutProvider = new ShortcutProvider;

Expand All @@ -64,6 +67,7 @@ describe('ShortcutProvider', () => {
throw error;
} finally {
delete global.document;
delete global.window;
}
});

Expand Down Expand Up @@ -316,7 +320,7 @@ describe('ShortcutProvider', () => {
});
});

describe('handleKeyUp', () => {
describe('handleBlur', () => {
it('should reset key sequence', () => {
const shortcutProvider = new ShortcutProvider;

Expand All @@ -327,7 +331,7 @@ describe('ShortcutProvider', () => {

shortcutProvider.keySequence = [ key1, key2, key3, key4 ];

shortcutProvider.handleKeyUp();
shortcutProvider.handleBlur();

expect(shortcutProvider.keySequence).to.deep.equal([]);
expect(shortcutProvider.fired).to.deep.equal({});
Expand Down

0 comments on commit 1cb4254

Please sign in to comment.