Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
.DS_Store | ||
*.log | ||
node_modules | ||
lib | ||
dist | ||
es |
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,55 @@ | ||
// @flow | ||
import * as React from 'react'; | ||
import createListener from 'keyboardist'; | ||
|
||
const listeners = { | ||
keydown: createListener('keydown'), | ||
keyup: createListener('keyup'), | ||
}; | ||
|
||
type Props = { | ||
eventName: 'keydown' | 'keyup', | ||
bindings: { [string]: any }, | ||
monitor?: any, | ||
}; | ||
|
||
class Keyboard extends React.PureComponent<Props> { | ||
subs = []; | ||
|
||
keyboardListener: any; | ||
|
||
static defaultProps = { | ||
eventName: 'keydown', | ||
bindings: {}, | ||
monitor: null, | ||
}; | ||
|
||
constructor(props: Props) { | ||
super(props); | ||
this.keyboardListener = listeners[props.eventName]; | ||
} | ||
|
||
componentDidMount() { | ||
const { bindings, monitor } = this.props; | ||
|
||
Object.keys(bindings).forEach(eventName => { | ||
const callback = bindings[eventName]; | ||
const subscription = this.keyboardListener.subscribe(eventName, callback); | ||
this.subs.push(subscription); | ||
}); | ||
|
||
if (monitor) { | ||
this.keyboardListener.setMonitor(monitor); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
this.subs.forEach(subscription => subscription.unsubscribe()); | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
export default Keyboard; |
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,46 @@ | ||
import * as React from 'react'; | ||
import createListener from 'keyboardist'; | ||
|
||
class Input extends React.PureComponent { | ||
subs = []; | ||
|
||
static defaultProps = { | ||
component: 'input', | ||
eventName: 'keydown', | ||
bindings: {}, | ||
monitor: null, | ||
}; | ||
|
||
componentDidMount() { | ||
const { bindings, monitor, eventName } = this.props; | ||
|
||
this.keyboardListener = createListener(eventName, this.element); | ||
|
||
Object.keys(bindings).forEach(eventName => { | ||
const callback = bindings[eventName]; | ||
const subscription = this.keyboardListener.subscribe(eventName, callback); | ||
this.subs.push(subscription); | ||
}); | ||
|
||
if (monitor) { | ||
this.keyboardListener.setMonitor(monitor); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
this.subs.forEach(subscription => subscription.unsubscribe()); | ||
} | ||
|
||
render() { | ||
const { | ||
component: Component, | ||
eventName, | ||
bindings, | ||
monitor, | ||
...props | ||
} = this.props; | ||
return <Component ref={el => (this.element = el)} {...props} />; | ||
} | ||
} | ||
|
||
export default Input; |