Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tusbar committed Apr 25, 2017
0 parents commit d5a1eff
Show file tree
Hide file tree
Showing 12 changed files with 3,484 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": [
"es2015"
],
"plugins": [
"transform-object-rest-spread"
]
}
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2
13 changes: 13 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"parser": "babel-eslint",
"env": {
"jest/globals": true
},
"extends": "airbnb",
"plugins": [
"jest"
],
"rules": {
"comma-dangle": "off"
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lib/

node_modules/
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js

node_js:
- '7'
- '6'
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Bertrand Marron <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "react-event-utils",
"version": "1.0.0",
"description": "Shorthand utils for dealing with React events",
"main": "lib/index.js",
"repository": "[email protected]:tusbar/react-event-utils.git",
"author": "Bertrand Marron <[email protected]>",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-eslint": "^7.2.3",
"babel-jest": "^19.0.0",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"eslint": "^3.19.0",
"eslint-config-airbnb": "^14.1.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jest": "^19.0.1",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.10.3",
"jest": "^19.0.2"
},
"scripts": {
"transpile": "babel src --out-dir lib",
"lint": "eslint src",
"jest": "jest",
"test": "yarn lint && yarn transpile && yarn jest",
"prepublish": "yarn test"
}
}
57 changes: 57 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# react-event-utils

> Shorthand utils for dealing with React events
# Install

```cli
$ yarn add react-event-utils
```

# About

This library exposes shorthand methods that call event methods for you.

This will allow you to stop having components like this:

```jsx
class Form extends React.Component {
constructor(props) {
super(props);

this.handleSubmit = this.handleSubmit.bind(this);
}

handleSubmit(e) {
e.preventDefault();

doSomething(e.target.title.value);
}

render() {
return (
<form onSubmit={this.handleSubmit}>
<input type="text" name="title" />
</form>
);
}
}
```

While you could just write:

```jsx
import { preventDefault /*, persist, stopPropagation */ } from 'react-event-utils';

export const Form = () =>
<form onSubmit={preventDefault(e => doSomething(e.target.title.value))}>
<input type="text" name="title" />
</form>;
```

Currently `persist`, `preventDefault` and `stopPropagation` are implemented.


# License

MIT © [Bertrand Marron](https://github.com/tusbar)
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
'persist',
'preventDefault',
'stopPropagation'
].forEach((name) => {
module.exports[name] = func => (e, ...args) => {
e[name]();
return func(e, ...args);
};
});
37 changes: 37 additions & 0 deletions test/events.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { persist, preventDefault, stopPropagation } from '../lib';

test('should be a function', () => {
expect(persist).toBeInstanceOf(Function);
expect(preventDefault).toBeInstanceOf(Function);
expect(stopPropagation).toBeInstanceOf(Function);
});

test('should return a function', () => {
expect(persist()).toBeInstanceOf(Function);
expect(preventDefault()).toBeInstanceOf(Function);
expect(stopPropagation()).toBeInstanceOf(Function);
});

test('should call the appropriate function', () => {
const persistMock = jest.fn();
const event = {
persist: persistMock
};

persist(() => {})(event);

expect(persistMock).toBeCalledWith();
});

test('should pass all the arguments to the passed function', () => {
const funcMock = jest.fn();
const args = [
{ preventDefault: () => {} },
'hi',
45
];

preventDefault(funcMock)(...args);

expect(funcMock).toBeCalledWith(...args);
});
9 changes: 9 additions & 0 deletions test/exports.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import utils from '../lib';

test('export 3 functions', () => {
expect(Object.keys(utils)).toEqual([
'persist',
'preventDefault',
'stopPropagation'
]);
});
Loading

0 comments on commit d5a1eff

Please sign in to comment.