1.0.0 • Published 5 years ago

react-form-input-handler v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

react-form-input-handler

package version package downloads standard-readme compliant package license make a pull request JavaScript Style Guide

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

  1. Fork it and create your feature branch: git checkout -b my-new-feature
  2. Commit your changes: git commit -am "Add some feature"
  3. Push to the branch: git push origin my-new-feature
  4. Submit a pull request

License

MIT © tiaanduplessis