react-keybindings
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -10,2 +10,3 @@ "use strict"; | ||
}); | ||
exports.default = mapActionsToKeys; | ||
@@ -36,3 +37,3 @@ var _react = require("react"); | ||
exports.default = function (_keymap) { | ||
function mapActionsToKeys(_keymap) { | ||
/** | ||
@@ -67,12 +68,14 @@ * Convert strings to key codes | ||
return function (WrappedComponent) { | ||
return function wrapComponent(WrappedComponent) { | ||
return (function (_Component) { | ||
_inherits(_class2, _Component); | ||
function _class2() { | ||
var _temp, _this, _ret, _Object$getPrototypeO; | ||
function _class2(props) { | ||
_classCallCheck(this, _class2); | ||
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(_class2)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.state = { pressedKeys: [] }, _this.handleKeyDown = function (e) { | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(_class2).call(this, props)); | ||
_this.state = { pressedKeys: [] }; | ||
_this.handleKeyDown = function (e) { | ||
var pressedKeys = [].concat(_toConsumableArray(_this.state.pressedKeys), [e.keyCode]); | ||
@@ -98,3 +101,5 @@ | ||
_this.setState({ pressedKeys: pressedKeys }); | ||
}, _this.handleKeyUp = function (e) { | ||
}; | ||
_this.handleKeyUp = function (e) { | ||
var _pressedKeys = _this.state.pressedKeys; | ||
@@ -127,3 +132,5 @@ var keyCode = _pressedKeys.indexOf(e.keyCode); | ||
_this.setState({ pressedKeys: pressedKeys }); | ||
}, _temp), _possibleConstructorReturn(_this, _ret); | ||
}; | ||
return _this; | ||
} | ||
@@ -175,2 +182,2 @@ | ||
}; | ||
}; | ||
} |
{ | ||
"name": "react-keybindings", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Add keybindings in React apps", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -5,3 +5,3 @@ import React, { Component } from "react" | ||
export default (_keymap) => { | ||
export default function mapActionsToKeys(_keymap) { | ||
/** | ||
@@ -40,92 +40,98 @@ * Convert strings to key codes | ||
return (WrappedComponent) => class extends Component { | ||
state = { pressedKeys: [] }; | ||
return function wrapComponent(WrappedComponent) { | ||
return class extends Component { | ||
constructor(props) { | ||
super(props) | ||
} | ||
componentDidMount() { | ||
const node = findDOMNode(this.wrapped) | ||
state = { pressedKeys: [] }; | ||
node.addEventListener("keydown", this.handleKeyDown) | ||
node.addEventListener("keyup", this.handleKeyUp) | ||
} | ||
componentDidMount() { | ||
const node = findDOMNode(this.wrapped) | ||
componentWillUnmount() { | ||
const node = findDOMNode(this.wrapped) | ||
node.addEventListener("keydown", this.handleKeyDown) | ||
node.addEventListener("keyup", this.handleKeyUp) | ||
} | ||
node.removeEventListener("keydown", this.handleKeyDown) | ||
node.removeEventListener("keyup", this.handleKeyUp) | ||
} | ||
componentWillUnmount() { | ||
const node = findDOMNode(this.wrapped) | ||
/** | ||
* Add the current pressed key to the component state | ||
*/ | ||
handleKeyDown = (e) => { | ||
const pressedKeys = [...this.state.pressedKeys, e.keyCode] | ||
// handle special keys | ||
if (e.ctrlKey && pressedKeys.indexOf(specialKeys.CTRL) < 0) { | ||
pressedKeys.push(specialKeys.CTRL) | ||
node.removeEventListener("keydown", this.handleKeyDown) | ||
node.removeEventListener("keyup", this.handleKeyUp) | ||
} | ||
if (e.metaKey && pressedKeys.indexOf(specialKeys.META) < 0) { | ||
pressedKeys.push(specialKeys.META) | ||
} | ||
/** | ||
* Add the current pressed key to the component state | ||
*/ | ||
handleKeyDown = (e) => { | ||
const pressedKeys = [...this.state.pressedKeys, e.keyCode] | ||
if (e.shiftKey && pressedKeys.indexOf(specialKeys.SHIFT) < 0) { | ||
pressedKeys.push(specialKeys.SHIFT) | ||
} | ||
// handle special keys | ||
if (e.ctrlKey && pressedKeys.indexOf(specialKeys.CTRL) < 0) { | ||
pressedKeys.push(specialKeys.CTRL) | ||
} | ||
if (e.altKey && pressedKeys.indexOf(specialKeys.ALT) < 0) { | ||
pressedKeys.push(specialKeys.ALT) | ||
} | ||
if (e.metaKey && pressedKeys.indexOf(specialKeys.META) < 0) { | ||
pressedKeys.push(specialKeys.META) | ||
} | ||
this.setState({ pressedKeys }) | ||
}; | ||
if (e.shiftKey && pressedKeys.indexOf(specialKeys.SHIFT) < 0) { | ||
pressedKeys.push(specialKeys.SHIFT) | ||
} | ||
/** | ||
* Remove the released key from the component state | ||
*/ | ||
handleKeyUp = (e) => { | ||
const _pressedKeys = this.state.pressedKeys | ||
const keyCode = _pressedKeys.indexOf(e.keyCode) | ||
const ctrl = _pressedKeys.indexOf(specialKeys.CTRL) | ||
const meta = _pressedKeys.indexOf(specialKeys.META) | ||
const shift = _pressedKeys.indexOf(specialKeys.SHIFT) | ||
const alt = _pressedKeys.indexOf(specialKeys.ALT) | ||
if (e.altKey && pressedKeys.indexOf(specialKeys.ALT) < 0) { | ||
pressedKeys.push(specialKeys.ALT) | ||
} | ||
const pressedKeys = [ | ||
..._pressedKeys.slice(0, keyCode), | ||
..._pressedKeys.slice(keyCode + 1), | ||
] | ||
this.setState({ pressedKeys }) | ||
}; | ||
// remove special keys | ||
if (ctrl > -1) { | ||
pressedKeys.splice(ctrl, 1) | ||
} | ||
/** | ||
* Remove the released key from the component state | ||
*/ | ||
handleKeyUp = (e) => { | ||
const _pressedKeys = this.state.pressedKeys | ||
const keyCode = _pressedKeys.indexOf(e.keyCode) | ||
const ctrl = _pressedKeys.indexOf(specialKeys.CTRL) | ||
const meta = _pressedKeys.indexOf(specialKeys.META) | ||
const shift = _pressedKeys.indexOf(specialKeys.SHIFT) | ||
const alt = _pressedKeys.indexOf(specialKeys.ALT) | ||
if (meta > -1) { | ||
pressedKeys.splice(meta, 1) | ||
} | ||
const pressedKeys = [ | ||
..._pressedKeys.slice(0, keyCode), | ||
..._pressedKeys.slice(keyCode + 1), | ||
] | ||
if (shift > -1) { | ||
pressedKeys.splice(shift, 1) | ||
} | ||
// remove special keys | ||
if (ctrl > -1) { | ||
pressedKeys.splice(ctrl, 1) | ||
} | ||
if (alt > -1) { | ||
pressedKeys.splice(alt, 1) | ||
} | ||
if (meta > -1) { | ||
pressedKeys.splice(meta, 1) | ||
} | ||
this.setState({ pressedKeys }) | ||
}; | ||
if (shift > -1) { | ||
pressedKeys.splice(shift, 1) | ||
} | ||
render() { | ||
const { pressedKeys } = this.state | ||
if (alt > -1) { | ||
pressedKeys.splice(alt, 1) | ||
} | ||
return ( | ||
<WrappedComponent | ||
ref={ (wrapped) => this.wrapped = wrapped } | ||
pressedKeys={ pressedKeys } | ||
keyActions={ keyActions(pressedKeys) } /> | ||
) | ||
this.setState({ pressedKeys }) | ||
}; | ||
render() { | ||
const { pressedKeys } = this.state | ||
return ( | ||
<WrappedComponent | ||
ref={ (wrapped) => this.wrapped = wrapped } | ||
pressedKeys={ pressedKeys } | ||
keyActions={ keyActions(pressedKeys) } /> | ||
) | ||
} | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19064
493