Skip to content

Commit fb75b91

Browse files
committed
Added first rough idea.
1 parent f2c26b8 commit fb75b91

11 files changed

+1086
-0
lines changed

.babelrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"env": {
3+
"test": {
4+
"presets": ["es2015"],
5+
"plugins": ["syntax-object-rest-spread", "transform-object-rest-spread"]
6+
},
7+
"production": {
8+
"presets": ["es2015-rollup"],
9+
"plugins": ["syntax-object-rest-spread", "transform-object-rest-spread"]
10+
}
11+
}
12+
}

dist/react-input-handler.esm.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-input-handler.esm.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-input-handler.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-input-handler.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-input-handler.umd.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-input-handler.umd.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "react-input-handler",
3+
"version": "0.0.1",
4+
"description": "Utility function to handle input changes in React.",
5+
"main": "dist/react-input-handler.js",
6+
"module": "dist/react-input-handler.esm.js",
7+
"browser": "dist/react-input-handler.umd.js",
8+
"repository": "git@github.com:rmariuzzo/react-input-handler.git",
9+
"author": "Rubens Mariuzzo <rubens@mariuzzo.com>",
10+
"license": "MIT",
11+
"files": [
12+
"LICENSE",
13+
"dist",
14+
"README.md",
15+
"yarn.lock"
16+
],
17+
"devDependencies": {
18+
"babel-core": "^6.26.0",
19+
"babel-plugin-transform-object-rest-spread": "^6.26.0",
20+
"babel-preset-es2015": "^6.24.1",
21+
"babel-preset-es2015-rollup": "^3.0.0",
22+
"cross-env": "^5.1.1",
23+
"rollup": "^0.51.5",
24+
"rollup-plugin-babel": "^3.0.2",
25+
"rollup-plugin-filesize": "^1.4.2",
26+
"rollup-plugin-uglify": "^2.0.1",
27+
"uglify-es": "^3.1.9"
28+
},
29+
"scripts": {
30+
"build": "cross-env NODE_ENV=production rollup -c"
31+
}
32+
}

rollup.config.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import babel from 'rollup-plugin-babel'
2+
import uglify from 'rollup-plugin-uglify'
3+
import filesize from 'rollup-plugin-filesize'
4+
import { minify } from 'uglify-es'
5+
import pkg from './package.json';
6+
7+
export default [
8+
9+
// browser-friendly UMD build
10+
{
11+
input: 'src/ReactInputHandler.js',
12+
output: {
13+
file: pkg.browser,
14+
format: 'umd',
15+
sourcemap: true,
16+
},
17+
name: 'ReactInputHandler',
18+
plugins: [
19+
babel(),
20+
uglify({}, minify),
21+
filesize(),
22+
]
23+
},
24+
25+
// CommonJS (for Node) and ES module (for bundlers) build.
26+
{
27+
input: 'src/ReactInputHandler.js',
28+
external: ['ms'],
29+
output: [
30+
{ file: pkg.main, format: 'cjs', sourcemap: true },
31+
{ file: pkg.module, format: 'es', sourcemap: true },
32+
],
33+
plugins: [
34+
babel(),
35+
uglify({}, minify),
36+
filesize(),
37+
]
38+
}
39+
];

src/ReactInputHandler.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict'
2+
3+
export default handler
4+
5+
function handler(event) {
6+
7+
if (!event) {
8+
throw new Error('event must be defined')
9+
}
10+
11+
if (typeof event !== 'object') {
12+
throw new Error('event must be an object')
13+
}
14+
15+
if (typeof this.setState !== 'function') {
16+
throw new Error('react-input-handler must be bound to the component instance')
17+
}
18+
19+
const target = event.target
20+
const name = target.name
21+
22+
if (!name) {
23+
throw new Error('all input must have a name prop')
24+
}
25+
26+
this.setState({
27+
[name]: getValue(target)
28+
})
29+
30+
}
31+
32+
function getValue(element) {
33+
switch(element.type) {
34+
case 'checkbox':
35+
return element.checked
36+
case 'select-multiple':
37+
return [ ...element.querySelectorAll(':checked') ]
38+
.map((checked) => checked.value)
39+
default:
40+
return element.value
41+
}
42+
}

0 commit comments

Comments
 (0)