Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

⚡️ Utility function to handle input changes in React.

License

NotificationsYou must be signed in to change notification settings

rmariuzzo/react-input-handler

Repository files navigation




⚡️ Utility function to handle input changes in React based onReact'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 viaarray notation.
  • Play well with deep state traversal usinglodash'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 usereact-input-handler:

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

Example

importReactfrom'react'importReactInputHandlerfrom'react-input-handler'exportdefaultclassFormextendsReact.Component{constructor(props){super(props)this.state={}this.handleChange=ReactInputHandler.bind(this)this.handleSubmit=this.handleSubmit.bind(this)}render(){return(<formonSubmit={this.handleSubmit}><label>Fullname:</label><inputtype="text"name="user.fullname"onChange={this.handleChange}/><label>Biography:</label><textareatype="text"name="user.bio"onChange={this.handleChange}/><label> Are you a developer?</label><inputtype="checkbox"name="user.developer"value="yes"onChange={this.handleChange}/><buttontype="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 thesetState 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 samename attribute. To achieve this we have to suffix thename attribute with[]. For example:

Before:

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

After:

<inputtype="checkbox"name="numbers[]"value="1"onChange={this.inputHandler}checked/><inputtype="checkbox"name="numbers[]"value="2"onChange={this.inputHandler}/><inputtype="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 ornpm 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 ❤️ byRubens Mariuzzo.

MIT license

About

⚡️ Utility function to handle input changes in React.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp