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

redux-react-form

v0.1.4

Published

A lightweight library that makes form validation easy with redux and validate.js

Downloads

11

Readme

Build Status peerDependency Status devDependency Status npm

Redux React Form

A simple way to validate a form using Redux and React. Form errors are available as state to anybody who cares to use them. Everyone loves their UI framework so we are not re-creating the wheel by creating our own form elements. All that you have to do is wrap you formFields in a react element that expects certain props and knows how to set valid/invalid state of the form field. That and the few other steps you need to do to integrate us into your app are detailed below!

Demo at http://helpdotcom.github.io/redux-react-form/

Dependencies

Our only peer dependencies are Redux, React and validate.js plus you can use your favorite build system.

Why re-create a library for validation of this one is perfectly suited to validate in whichever format you want. It can do composite validation, custom validation and it has a bunch of built-ins.

Future

Even thought validate.js works for us it might not for everyone. In future releases we want to pass the validation library as an injection in the reducer similar to the below. As long as that validation library return an array of strings as errors per formData key all should work!

function validateForm(state, action) {
  const forms = Object.assign({}, state);
  const form = forms[action.formID];
  const errors = action.validate(action.formData, form.validationRules);
  if (errors) {
    form.errors = errors;
  } else {
    delete form.errors;
  }
  return forms;
}

Installation

We currently support only npm so just install us as a dependency.

npm install --save redux-react-form

Development

To contribute to us (we would love some feedback!) simply clone the Github repository and follow the step below.

  1. Install dependencies
npm install
  1. Run us locally
npm start
  1. For any changes the only thing we require are tests (we use tape and sinon). The tests will run locally in a headless browser. We don't have a test runner yet but we would gladly accept a pull request that provides one.
npm run test

Integration

At the end of the day all we are is some actions, a reducer and a form component. We chose this path so that we could met everyone's needs without being opinionated as to what framework to use.

Our npm package exports those three items like such.

import { FormComponent, formActions, formReducer } from 'redux-react-form';

Step 1

You need to add the reducer into your combineReducers function with the key formValidator like so;

import { formReducer } from 'redux-react-form';

const reducer = combineReducers({
  forms: formReducer,
  ...
});

const store = createStore(reducer);

Step 2

Next you have to create a component that extends from FormComponent.

look at form1. A form needs to have a unique id, we are hard-coding in our example but we recommend using lodash uniqueId to accomplish this. If you do not provide a formID we will set one for you. You also need to write a mapStateToProps function to be the first parameter of the connect function. We did not want to do this for you since there might be multiple state locations the component needs to get data from. Ours is simple:

function select(state) {
  return {
    forms: state.forms,
    //other state you care about
    ...
  };
}

Load the rules you want to use for the form, since your form is connected (smart component), you can you dispatch a load constraints event in componentDidMount like below. Rules are form-bound and are not shared across forms.

componentWillMount(...args) {
  super.componentWillMount(...args);
  this.dispatch(formActions.loadValidationRules(this.formID, registrationRules));
}

If you want to know from your form component when formData state has been updated just create onFormFieldChanged method, yay!

onFormFieldChanged() {
  //do something or the other
  ...
}

Step 3

For the form fields in your form component you need to use formFieldProps to pass the necessary props to down.

<Input floatingLabelText="Username" hintText="Enter your username" {...this.formFieldProps('username')}/>

The first argument is the ref that will be passed to the form field. An optional parameter tells the form what prop to use to pass the value of the form field. These form fields will always be controlled, meaning that the parent (the form component) will be managing the value of the form field.

Creating a Form Field Wrapper

Our job is not to dictate what UI component framework to use but to help you validate your form. That's why wrapping those form fields is the easiest way to go about it. Sure it's kind of a pain but you only have to do it once. Also it allows you to create your own form fields without having to dump us.

Look at our form-field wrapper that essentially just renders a material input. Its only job really is to figure out how to render errors from it's errors prop, how to handle onChange and hookup onBlur and onFocus callbacks to the form field and that's it!

Browser Support

We will attempt to support the same browsers that React and Redux supports.

License

MIT