react-form-input-handler
v1.0.0
Published
Tiny utility for working with multiple inputs
Downloads
2
Maintainers
Readme
react-form-input-handler
Tiny utility for working with multiple inputs. Taken from the React documentation
Table of Contents
Install
This project uses node and npm.
$ npm install react-form-input-handler
$ # OR
$ yarn add react-form-input-handler
Usage
import React, { Component } from 'react'
import formInputHandler from 'react-form-input-handler'
class App extends Component {
constructor() {
super()
this.state = {
username: '',
password: ''
}
this.handleChange = formInputHandler.bind(this)
}
render() {
return <>
<input onChange={this.handleChange} name="username"/>
<input onChange={this.handleChange} password="password"/>
</>
}
}
export default App
Or if your not in the mood for a extra dependency. Here is the source:
export default function formInputHandler(e) {
if (!this.setState) {
throw Error('Form input handler not bound to Component')
}
const {target} = e
const name = target.name
const val = target.type === 'checkbox' ? target.checked : target.value
this.setState({[name]: val})
}
Contribute
- Fork it and create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am "Add some feature"
- Push to the branch:
git push origin my-new-feature
- Submit a pull request
License
MIT © tiaanduplessis