All Projects → rmariuzzo → react-input-handler

rmariuzzo / react-input-handler

Licence: MIT license
⚡️ Utility function to handle input changes in React.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-input-handler

musicWebTemplate
Free website template built for musicians / artists to promote their music and connect to their audience.
Stars: ✭ 26 (+73.33%)
Mutual labels:  form
react-form-firebase
Simple form with Firebase
Stars: ✭ 26 (+73.33%)
Mutual labels:  form
ATGValidator
iOS validation framework with form validation support
Stars: ✭ 51 (+240%)
Mutual labels:  form
mobx-react-form-devtools
DevTools for MobX React Form
Stars: ✭ 30 (+100%)
Mutual labels:  form
FrontendForms
A module for ProcessWire CMS to create and validate forms on the frontend easily using the Valitron library.
Stars: ✭ 0 (-100%)
Mutual labels:  form
file-input-accessor
Angular directive that provides file input functionality in Angular forms.
Stars: ✭ 32 (+113.33%)
Mutual labels:  form
react-input
A single component for building forms in React
Stars: ✭ 44 (+193.33%)
Mutual labels:  form
PHP-FileUpload
Simple and convenient file uploads — secure by default
Stars: ✭ 53 (+253.33%)
Mutual labels:  form
craft3-forms
This craft CMS 3 form plugin makes it easy to create and use custom forms with the features the Yii 2 Framework offers. On top of this, the plugin provides even more functionalities for easy implementation of forms in twig templates.
Stars: ✭ 20 (+33.33%)
Mutual labels:  form
laravel-support-bubble
A non-intrusive support form that can be displayed on any page
Stars: ✭ 289 (+1826.67%)
Mutual labels:  form
veasy
A comprehensive react form solution which aims to eliminate all tedious logic.
Stars: ✭ 31 (+106.67%)
Mutual labels:  form
powermail
This is the official repository of the TYPO3 extension powermail! Powermail is a well-known, editor-friendly, powerful and easy mailform extension for TYPO3
Stars: ✭ 76 (+406.67%)
Mutual labels:  form
react-inputs-validation
A react component for form inputs validation. Online demo examples
Stars: ✭ 48 (+220%)
Mutual labels:  form
form-bunch
Form-bunch is a component like plugin that make it easier to write form, you could add the most of components what you want to form-bunch for build various form.
Stars: ✭ 18 (+20%)
Mutual labels:  form
Formicula
A template-driven form mailer for Zikula
Stars: ✭ 19 (+26.67%)
Mutual labels:  form
antd-form-mate
📦 基于 ant design 的表单组件,配置化实现表单功能。
Stars: ✭ 14 (-6.67%)
Mutual labels:  form
laravel-form-components
Form components built for Tailwind & Livewire.
Stars: ✭ 225 (+1400%)
Mutual labels:  form
ember-validity-modifier
Ember Octane addon to add custom validity (form validation) to form fields
Stars: ✭ 28 (+86.67%)
Mutual labels:  form
ir-city-select
List of Iran provinces and cities to use in HTML forms. ( ~2.3KB gzipped )
Stars: ✭ 22 (+46.67%)
Mutual labels:  form
jasper-helpers
A library of helpers for working with html for apps Crystal
Stars: ✭ 20 (+33.33%)
Mutual labels:  form




react-input-handler

⚡️ Utility function to handle input changes in React based on React's handling multiple input docs.




Features

  • Package size is: 1.66KB (0.8KB gzipped!).
  • Supports all <input />s, <textarea /> and <select /> elements.
  • Supports <select multiple />.
  • Supports checkboxes with same name via array notation.
  • Play well with deep state traversal using lodash's set method.
  • Multiple bundles: CJS, ESM and UMD.

Installation

yarn add react-input-handler

or

npm install react-input-handler --save

Usage

Two things needs to be done to use react-input-handler:

  1. Create a bound function (see 2nd line in constructor).
  2. Attach the bound function to onChange events.

Example

import React from 'react'
import ReactInputHandler from 'react-input-handler'

export default class Form extends React.Component {

  constructor(props) {
    super(props)

    this.state = {}

    this.handleChange = ReactInputHandler.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>Fullname:</label>
        <input type="text" name="user.fullname" onChange={this.handleChange} />

        <label>Biography:</label>
        <textarea type="text" name="user.bio" onChange={this.handleChange} />

        <label> Are you a developer?</label>
        <input type="checkbox" name="user.developer" value="yes" onChange={this.handleChange} />

        <button type="submit">Submit</button>
      </form>
    )
  }

  handleSubmit(event) {
    event.preventDefault()
    console.log(this.state)
    // Output: { user: { fullanme: "string", bio: "string", developer: true|false } }
  }
}

Documentation

React-input-handler is a single function which accept two argument: an event and a optional callback function that will be passed to the setState method.

The objective is simple: handle input changes and persist them into the component's state.

Array notation

By default, react-input-handler handles checkbox as boolean value. Sometimes, we may want two or more checkboxes to be handled as an array sharing the same name attribute. To achieve this we have to suffix the name attribute with []. For example:

Before:

  <input type="checkbox" name="one" value="1"   onChange={this.inputHandler} checked />
  <input type="checkbox" name="two" value="2"   onChange={this.inputHandler} />
  <input type="checkbox" name="three" value="3" onChange={this.inputHandler} checked />
  // state: { one: true, two: false, three: true }

After:

  <input type="checkbox" name="numbers[]" value="1" onChange={this.inputHandler} checked />
  <input type="checkbox" name="numbers[]" value="2" onChange={this.inputHandler} />
  <input type="checkbox" name="numbers[]" value="3" onChange={this.inputHandler} checked />
  // state: { numbers: ["1", "3"] }

Development

  1. Clone and fork this repo.
  2. Install dependencies running: yarn or npm install.
  3. Run tests.
  4. Prepare a pull request.

Test

  • yarn test - to run all tests.
  • yarn test -- --watch to run all tests in watch mode.

Publish

  1. Bump version: npm version x.x.x -m 'Version %s.'.
  2. Publish to NPM registry: npm publish.
  3. Publish the new created tag: git push origin --tags.

Made with ❤️ by Rubens Mariuzzo.

MIT license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].