Skip to content

Commit

Permalink
fixed .gitignore issue
Browse files Browse the repository at this point in the history
  • Loading branch information
soska committed Jun 26, 2018
1 parent 9bc8161 commit f8ae5b7
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,5 +1,5 @@
.DS_Store
*.log
node_modules
lib
dist
es
7 changes: 4 additions & 3 deletions package.json
@@ -1,13 +1,14 @@
{
"name": "react-keyboardist",
"version": "0.2.0",
"description": "A simple keyboard global manager for react. Powered by keyboardist.",
"main": "lib/index.js",
"description":
"A simple keyboard global manager for react. Powered by keyboardist.",
"main": "dist/index.js",
"scripts": {
"flow": "flow",
"favicon": "cpx \"src/docs/favicon.*\" \"docs\"",
"dev": "webpack-dev-server --mode development",
"build:lib": "babel src/lib -d lib",
"build:lib": "babel src/lib -d dist",
"build:docs": "webpack --mode production",
"build": "npm run build:lib && npm run build:docs && npm run favicon"
},
Expand Down
55 changes: 55 additions & 0 deletions src/lib/global.js
@@ -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;
46 changes: 46 additions & 0 deletions src/lib/input.js
@@ -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;

0 comments on commit f8ae5b7

Please sign in to comment.