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-form-required-fields

v1.1.4

Published

A simple decorator that can be used to easily add required fields to the context of a component tree that leverages Redux Form Fields.

Downloads

63

Readme

ReduxForm Required Fields Decorator

npm monthly downloads Build Status

A simple decorator that can be used to easily add required fields to the context of a component tree that leverages Redux Form Fields.

This package is discussed in this blog post: https://blog.hellojs.org/redux-form-is-an-awesome-way-to-handle-forms-in-a-redux-application-in-my-opinion-6b2713a815fa

If you use ReduxForm >=6, then this decorator can help to simplify your management of required fields on a form. In version 6 and up in ReduxForm, the Field component is at your disposal for use. This Field takes a UI component as a prop for what to actually render in the UI. You can provide it an input, text area, radio buttons, etc. This is an awesome tool to have available to create forms.

In creating forms, it is common practice to do something to indicate in the UI that a form field is required, and to also handle validation for required fields. A common example is an asterisk * to denote required. The purpose of this decorator (which can also be used as a Function to which you pass your form component) is to simplify the code needed to handle required.

It accomplishes this simplification by taking in a simple array of field names Array<string> and putting that array on the context the parent form component so that the array is available to any component further down the tree (using React Context https://reactjs.org/docs/context.html).

In doing so, it makes it possible to have a check in your component rendered for your Field component for whether it is required. Basically, wherever you manage your field components you can check this.context.requiredFields.includes(currentFieldName). One recommendation would be to have your own wrapper around ReduxForm's Field that handles every possible field UI you would need to render. Then, in this one wrapper component you can handle your checking for required, and pass required as true to the UI components themselves. Then, handle the * or other UI to indicate required in the UI component.

Usage

import { Component } from 'react';
import Field from 'path/to/fieldComponent';
import { reduxForm } from 'redux-form'
import RequireFields from 'redux-form-required-fields';
import someValidator from 'someValidatorYouWrote';

const FOO = 'foo';

// NOTE: it is recommended to define your required fields once as a constant to be used anywhere you need it
const REQUIRED_FIELDS = [ FOO ];

@RequiredFields(REQUIRED_FIELDS)
class SomeForm extends Component {
    render() {
        return <Field name={ FOO } />
    }
}

SomeForm = reduxForm({ form: 'SomeForm', validate: formProps => someValidator(formProps, REQUIRED_FIELDS) })(SomeForm);

export default SomeForm // of course connect with redux if need-be

The steps are pretty simple:

  1. import the decorator
  2. define a constant for your required fields as an array of field name
  3. use that array by passing it into the decorator, and optionally into your validator function if you centralize your validation

tl;dr

This is a super simple decorator that is only here to make your life easier by adding an array of required field names to the context of a Redux Form at the highest level, so that every component down the tree will have access and can use that array to conditionally render UI specific to required fields.

Additional Notes

You will want to make sure you include the right Babel transform plugins to be able to handle decorators. Here is an example of a setup that works for this and other things as well:

{
    "presets": ["react", "es2015", "stage-1"],
    "plugins": ["transform-decorators-legacy"]
}

License

MIT © RedLock