npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-formdata

v3.0.0

Published

A library for generating an object of values from a set of inputs in React

Downloads

233

Readme

react-formdata NPM version Build Status

A library for generating an object of values from a set of inputs in React. Either by auto detecting all inputs with name, an ID or by manually adding inputs with specified name.

This is handy in cases where you want to dispatch an action to a global state and don't want the boiler plate of extracting all values yourself. See example below for usage.

Install

Install from NPM:

$ npm install --save react-formdata

Usage

Tip: See the example or the tests for short examples of usage.

formData(Decoratee) ⇒ Component

Creates a higher order component for easier form data handling using React. Especially handy when using global application state and a functional style approach to UI development.

By default, formData will generate values from every input fields with ID-attribute (textarea, input, select) except submit and general buttons. To ignore a input, you can either add a mapper removing it, or simply not add ID. If you want the input to have a different name, you can map it or use the injected addInput to explicitly add the form input with a name. This is also useful in cases where you don't want an ID on your input data.

The precedence order for naming is: Manual > Name attribute > ID attribute

Injects the following properties to the decorated component.

  • addInput(inputName) - Manually add input, with data name specified as argument
  • ocHook(synteticEvent) - onChange hook. Use this when you want onChange to be triggered. Most cases every input in your decoratee should have a onChange={ocHook}. Is composable
  • customChange(Object) - Add custom data to the onChange trigger.

Kind: global function
Returns: Component - Decorated Component - The newly derived component with additional behaviour.

| Param | Type | Description | | --- | --- | --- | | Decoratee | Component | component you'd like to decorate with form data behaviour. |

Example
Using onChange hook to aggregate and create a common onChange listener with data

var formData = require('react-formdata');

// `ocHook` is injected from `formData`:
var DecoratedMyForm = formData(function ({ addInput, ocHook }) {
  return (
    <ol>
      <li><input id="a" type="text" onChange={ocHook} value="Hello World" /></li>
      <li><textarea ref={addInput('b')} onChange={ocHook}>Hello World</textarea></li>
    </ol>
  );
});
const App = function () {
  return <DecoratedMyForm onChange={(values) => console.log(values)} />;
};

Outputs something like

{
  "a": "Hello World",
  "b": "Hello World"
}

Returned formData decorated Component: A React Component with the added behaviour of form data handling. All properties passed to decorated component, is transitive. This means it will be passed to the decoratee. In addition, two properties are injected to the decoratee; addInput and ocHook. @see formData

Decorated component has two props callbacks you can use:

  • valueMapper(Object) -> Object - Takes values and returns new mapped values. If you want to transform some of the data before triggering onChange or getData.
  • onChange(Object) - callback triggered when some of the decoratee triggers the on change hook. Is called with data values as argument.

In addition to props, you can use React refs to get the initial value when component is mounted: Example

<DecoratedForm ref={function (inputRef) {
  // Now the node is mounted, and we have a ref to it. We can access data and inputs:
  var myFormInputs = inputRef.getInputs();
  var myFormData   = inputRef.getValues(); // Will respect value mapper

  // You also have access to `addInput`:
  addInput('customInput')(ReactDOM.findDOMNode(this).querySelector('.custom'));
}} />

Contribute

Contributions are very welcome. To get the project running locally, you have to do the following:

$ git clone https://github.com/mikaelbr/react-formdata
$ cd react-formdata
$ npm install
$ npm test

You can test out the example by doing:

$ cd example/
$ npm install
$ npm run build
$ open index.html