Skip to content

Commit 900e161

Browse files
committed
upgraded to react-0.14.0, upped to v1.0, rewrote in es6
1 parent 3de42e4 commit 900e161

17 files changed

+253
-124
lines changed

.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"stage": 0,
3+
"loose": "all"
4+
}

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
webpack.config*.js
2+
node_modules

.eslintrc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"extends": "eslint-config-airbnb",
3+
"env": {
4+
"browser": true,
5+
"mocha": true,
6+
"node": true
7+
},
8+
"rules": {
9+
"react/jsx-uses-react": 2,
10+
"react/jsx-uses-vars": 2,
11+
"react/react-in-jsx-scope": 2,
12+
"comma-dangle": 0, // not sure why airbnb turned this on. gross!
13+
"indent": [2, 2, {"SwitchCase": 1}],
14+
15+
//Temporarirly disabled due to a possible bug in babel-eslint (todomvc example)
16+
"block-scoped-var": 0,
17+
// Temporarily disabled for test/* until babel/babel-eslint#33 is resolved
18+
"padded-blocks": 0
19+
},
20+
"plugins": [
21+
"react"
22+
]
23+
}

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
.idea
12
node_modules
2-
*.iml
3+
dist
4+
lib
5+
npm-debug.log
6+
.DS_Store

.npmignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
src
2+
examples
3+
test
4+
.babelrc
5+
.eslint*
6+
.idea
7+
.npmignore
8+
.travis.yml
9+
webpack.*

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
3+
node_js:
4+
- "0.12"
5+
- "4.0"
6+
- "4"
7+
8+
script:
9+
- npm run lint

AUTHORS

Lines changed: 0 additions & 1 deletion
This file was deleted.

LICENSE-MIT renamed to LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2014 Erik Rasmussen
1+
Copyright (c) 2015 Erik Rasmussen
22

33
Permission is hereby granted, free of charge, to any person
44
obtaining a copy of this software and associated documentation

NativeListener.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

README.md

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
55
## THIS IS UNRELATED TO react-native!
66

7-
Please don't confuse this library with anything to do with [React Native](https://facebook.github.io/react-native/). This library is for dealing directly with _**browser**_ native events.
7+
Please don't confuse this library with anything to do with [React Native](https://facebook.github.io/react-native/).
8+
This library is for dealing directly with _**browser**_ native events.
89

910
## Why?
1011

@@ -33,40 +34,44 @@ put your event listener properties (e.g. `onClick`, `onKeyDown`) on `<NativeList
3334

3435
So, instead of this...
3536

36-
```jsx
37-
/** @jsx React.DOM */
38-
var MyComponent = React.createClass({
39-
onButtonClick: function(event) {
37+
```javascript
38+
import React, {Component} from 'react';
39+
40+
export default class MyComponent extends Component {
41+
handleButtonClick(event) {
4042
// do something (event is React's SyntheticEvent)
41-
},
42-
render: function() {
43+
}
44+
45+
render() {
4346
return (
4447
<div>
45-
<button onClick={this.onButtonClick}>Click Me!</button>
48+
<button onClick={this.handleButtonClick.bind(this)}>Click Me!</button>
4649
</div>
4750
);
4851
}
49-
});
52+
}
5053
```
5154
...do this:
5255

53-
```jsx
54-
/** @jsx React.DOM */
55-
var NativeListener = require('react-native-listener');
56-
var MyComponent = React.createClass({
57-
onButtonClick: function(event) {
56+
```javascript
57+
import React, {Component} from 'react';
58+
import NativeListener from 'react-native-listener';
59+
60+
export default class MyComponent extends Component {
61+
handleButtonClick(event) {
5862
// do something (event is native browser event)
59-
},
60-
render: function() {
63+
}
64+
65+
render() {
6166
return (
6267
<div>
63-
<NativeListener onClick={this.onButtonClick}>
68+
<NativeListener onClick={this.handleButtonClick.bind(this)}>
6469
<button>Click Me!</button>
6570
</NativeListener>
6671
</div>
6772
);
6873
}
69-
});
74+
}
7075
```
7176

7277
**IMPORTANT:** The event passed to your function is the native browser event, _NOT
@@ -78,11 +83,12 @@ If all you want to do is stop the propagation of an event, there are convenience
7883
`stopClick`, `stopKeyDown`. For example, say you wanted to allow normal hyperlinks to work, but your
7984
component is inside some element that JQuery is calling `event.preventDefault()` for clicks...
8085

81-
```jsx
82-
/** @jsx React.DOM */
83-
var NativeListener = require('react-native-listener');
84-
var MyComponent = React.createClass({
85-
render: function() {
86+
```javascript
87+
import React, {Component} from 'react';
88+
import NativeListener from 'react-native-listener';
89+
90+
export default class MyComponent extends Component {
91+
render() {
8692
return (
8793
<div>
8894
<NativeListener stopClick>
@@ -108,4 +114,4 @@ e.g. `onClickCapture`, `onKeyDownCapture`.
108114

109115
---
110116

111-
Module submitted by [Erik Rasmussen](https://www.npmjs.org/~erikras) [@erikras](https://twitter.com/erikras)
117+
Module written by [Erik Rasmussen](https://www.npmjs.org/~erikras) [@erikras](https://twitter.com/erikras)

index.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

package.json

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,56 @@
11
{
22
"name": "react-native-listener",
33
"description": "A utility component to allow easy access to browser native events",
4-
"version": "0.0.6",
4+
"version": "1.0.0",
55
"homepage": "https://github.com/erikras/react-native-listener",
6-
"author": {
7-
"name": "Erik Rasmussen",
8-
"url": "http://erikras.com/"
9-
},
106
"repository": {
117
"type": "git",
128
"url": "git://github.com/erikras/react-native-listener.git"
139
},
10+
"scripts": {
11+
"build": "npm run build:lib && npm run build:umd && npm run build:umd:min",
12+
"build:lib": "babel src --out-dir lib",
13+
"build:umd": "webpack src/index.js dist/react-native-listener.js --config webpack.config.development.js",
14+
"build:umd:min": "webpack src/index.js dist/react-native-listener.min.js --config webpack.config.production.js",
15+
"clean": "rimraf dist lib",
16+
"lint": "eslint src",
17+
"prepublish": "npm run lint && npm run test && npm run clean && npm run build"
18+
},
19+
"author": "Erik Rasmussen <[email protected]> (http://github.com/erikras)",
20+
"license": "MIT",
1421
"bugs": {
1522
"url": "git://github.com/erikras/react-native-listener/issues"
1623
},
17-
"licenses": [
18-
{
19-
"type": "MIT",
20-
"url": "https://github.com/erikras/react-native-listener/blob/master/LICENSE-MIT"
21-
}
22-
],
2324
"engines": {
2425
"node": ">= 0.10.0"
2526
},
2627
"main": "index.js",
27-
"dependencies": {
28-
"react": "~0.13.0"
29-
},
3028
"keywords": [
3129
"react",
3230
"event",
3331
"listener",
3432
"native",
35-
"reactjs"
33+
"reactjs",
34+
"browser"
3635
],
37-
"files": [
38-
"index.js",
39-
"NativeListener.js",
40-
"README.md",
41-
"LICENSE-MIT"
42-
]
36+
"devDependencies": {
37+
"babel": "^5.8.23",
38+
"babel-eslint": "^4.1.3",
39+
"babel-loader": "^5.3.2",
40+
"eslint": "^1.6.0",
41+
"eslint-config-airbnb": "^0.1.0",
42+
"eslint-plugin-react": "^3.5.1",
43+
"react": "^0.14.0",
44+
"react-dom": "^0.14.0",
45+
"webpack": "^1.12.2"
46+
},
47+
"npmName": "react-native-listener",
48+
"npmFileMap": [
49+
{
50+
"basePath": "/dist/",
51+
"files": [
52+
"*.js"
53+
]
54+
}
55+
]
4356
}

src/NativeListener.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {Component, PropTypes} from 'react';
2+
import ReactDOM from 'react-dom';
3+
const events = [
4+
'KeyDown',
5+
'KeyPress',
6+
'KeyUp',
7+
'Click',
8+
'ContextMenu',
9+
'DoubleClick',
10+
'Drag',
11+
'DragEnd',
12+
'DragEnter',
13+
'DragExit',
14+
'DragLeave',
15+
'DragOver',
16+
'DragStart',
17+
'Drop',
18+
'MouseDown',
19+
'MouseEnter',
20+
'MouseLeave',
21+
'MouseMove',
22+
'MouseOut',
23+
'MouseOver',
24+
'MouseUp'
25+
];
26+
27+
const aliases = {
28+
'DoubleClick': 'dblclick'
29+
};
30+
31+
const toEventName = event =>
32+
(aliases[event] || event).toLowerCase();
33+
34+
export default class NativeListener extends Component {
35+
static displayName = 'NativeListener';
36+
static propTypes = {
37+
children: (props, propName) => {
38+
if (props[propName].length) {
39+
return new Error('NativeListener can only wrap one element');
40+
}
41+
},
42+
...events.reduce((accumulator, event) => ({
43+
...accumulator,
44+
[`on${event}`]: PropTypes.func,
45+
[`on${event}Capture`]: PropTypes.func,
46+
[`stop${event}`]: PropTypes.bool
47+
}), {})
48+
};
49+
50+
componentDidMount() {
51+
const props = this.props;
52+
const element = ReactDOM.findDOMNode(this);
53+
events.forEach(event => {
54+
const capture = props['on' + event + 'Capture'];
55+
const bubble = props['on' + event];
56+
const stop = props['stop' + event];
57+
if (capture && typeof capture === 'function') {
58+
element.addEventListener(toEventName(event), capture, true);
59+
}
60+
if (bubble && typeof bubble === 'function') {
61+
element.addEventListener(toEventName(event), bubble, false);
62+
}
63+
if (stop === true) {
64+
element.addEventListener(toEventName(event), nativeEvent => nativeEvent.stopPropagation(), false);
65+
}
66+
});
67+
}
68+
69+
render() {
70+
return this.props.children;
71+
}
72+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import NativeListener from './NativeListener';
2+
export default NativeListener;

0 commit comments

Comments
 (0)