-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- can be used any place a function that received a KeyboardEvent would be used - does not participate in the wider ember-keyboard functionality (responders, priority, etc), but can be useful if you just want to leverage the key combo matching code of this addon
- Loading branch information
Showing
7 changed files
with
156 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { helper } from '@ember/component/helper'; | ||
import isKey from 'ember-keyboard/utils/is-key'; | ||
import listenerName from 'ember-keyboard/utils/listener-name'; | ||
import { assert } from '@ember/debug'; | ||
|
||
export default helper(function ifKey([keyCombo, callback]/*, hash*/) { | ||
return function(event) { | ||
assert( | ||
'ember-keyboard: You must pass a function as the second argument to the `if-key` helper', | ||
typeof callback === 'function' | ||
); | ||
assert( | ||
'ember-keyboard: The `if-key` helper expects to be invoked with a KeyboardEvent', | ||
event instanceof KeyboardEvent | ||
); | ||
|
||
if (isKey(listenerName(event.type, keyCombo), event)) { | ||
callback(event); | ||
}; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, ifKey } from 'ember-keyboard/helpers/if-key'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { module, skip, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { click, render, resetOnerror, setupOnerror, triggerEvent } from '@ember/test-helpers'; | ||
import { hbs } from 'ember-cli-htmlbars'; | ||
|
||
module('Integration | Helper | if-key', function(hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
let onTriggerCalled; | ||
hooks.beforeEach(function() { | ||
onTriggerCalled = false; | ||
this.set('onTrigger', () => { | ||
onTriggerCalled = true; | ||
}); | ||
}); | ||
|
||
|
||
module('error cases', function(hooks) { | ||
hooks.afterEach(() => resetOnerror()); | ||
|
||
// This doesn't work. I wish it did, but can't figure out why not. | ||
skip('errors if invoked without a handler', async function(assert) { | ||
assert.expect(1); | ||
setupOnerror(function(error) { | ||
assert.strictEqual( | ||
error.message, | ||
"Assertion Failed: ember-keyboard: The if-key helper must be provided a function as its second argument", | ||
'error is thrown' | ||
); | ||
}); | ||
await render(hbs`{{on-document "keydown" (if-key "alt+c" unknownEvent)}}`); | ||
await triggerEvent(document.body, 'keydown', { altKey: true, key: 'c' }); | ||
}); | ||
|
||
// This doesn't work. I wish it did, but can't figure out why not. | ||
skip('warns if called without a keyboard event', async function(assert) { | ||
assert.expect(1); | ||
setupOnerror(function(error) { | ||
assert.strictEqual( | ||
error.message, | ||
"Assertion Failed: ember-keyboard: The if-key helper expects to be invoked with a KeyboardEvent", | ||
'error is thrown' | ||
); | ||
}); | ||
await render(hbs`<button {{on 'click' (if-key "alt+c" onTrigger)}}>Press me</button>`); | ||
await click('button'); | ||
}); | ||
}); | ||
|
||
test('called with event', async function(assert) { | ||
let onTriggerCalledWith; | ||
this.set('onTrigger', (ev) => { | ||
onTriggerCalledWith = ev; | ||
}); | ||
await render(hbs`{{on-document "keydown" (if-key "alt+c" onTrigger)}}`); | ||
await triggerEvent(document.body, 'keydown', { altKey: true, key: 'c' }); | ||
assert.ok(onTriggerCalledWith instanceof KeyboardEvent); | ||
}); | ||
|
||
test('not called if key combo does not match', async function(assert) { | ||
let onTriggerCalledWith; | ||
this.set('onTrigger', (ev) => { | ||
onTriggerCalledWith = ev; | ||
}); | ||
await render(hbs`{{on-document "keydown" (if-key "alt+c" onTrigger)}}`); | ||
await triggerEvent(document.body, 'keydown', { shiftKey: true, key: 'z' }); | ||
assert.ok(!onTriggerCalledWith, 'trigger is on invoked if key does not match'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters