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

nix-react-state-validate

v1.1.4

Published

Validation package for the state object in React

Downloads

4

Readme

nix-react-state-validate

Note: This package is no longer maintained.

A validation package for the state object in React. It takes in your current state, validates whichever value you want to validate, and outputs a state object with new success/error values depending on the validation. This is useful for form validation in React.

This package is library-independent, i.e., it works with Material-UI, React-Bootstrap, Sematic UI and all other popular React libraries.

I made this package after trying to use several React packages for form validation. Most give you custom components, and I found that often these custom components are not properly compatible with the components of the front-end library you are using (e.g., Material UI). Therefore, nix-react-state-validate does not use a custom component. This package gives you an easy way to change the state based on errors, but does not append any element into the page. It is left to the user to show the errors using whatever components he/she wants to use.

Note: For users who were using any of the versions 1.0.x, and then upgraded to 1.1.x, there are breaking changes. I recommend that you install the latest version and make changes to your code accordingly. Apologies for the inconvenience.

Install

npm install nix-react-state-validate --save

Quick Start Example

Let's suppose you're checking if the email entered by the user in the following form component is actually an email.

Example gif

DetailForm.js
import React from 'react';
import validator, {nixCheckInput} from 'nix-react-state-validate';


class DetailForm extends React.Component {
    constructor(props) {
        super(props);
    
        this.state = {
            details: {
                name: '',
                email: '',
                phone: ''
            },
            errors: {
                name: '',
                email: '',
                phone: ''
            }
        }
    }

    handleChange = event => {
        this.setState( 
            nixCheckInput({
                validity: validator.isEmail(event.target.value),
                componentState: this.state,
                componentStateSuccessItem: {
                  details: {
                    email: event.target.value
                  }
                },
                componentStateErrorItem: {
                  errors: {
                    email: "Not a valid email"
                  }
                }
            })
        );
    }
    
    render() {
        return (
            <div>
                Name<br />
                <input /><br />

                Email<br />
                <input onChange={this.handleChange} /><br />
                <div style={{color:'red'}}>{this.state.errors.email}</div>

                Phone<br />
                <input />
            </div>
        );
    }
}


export default DetailForm;

Options

| Key | Definition | Value | Required | | ----------------- | --------------------------------- | ------------- | -------- | | validity | A validator function that returns either true or false. You can choose to use any of the validators in validator.js. The latest validator.js library is installed along with nix-react-state-validate by default.You can also use your own validator function if you want.In case you're not passing a componentStateErrorItem, just insert a (validity) value of true. | Boolean | Yes | | componentState | Current state of the component. | Object (this.state) | Yes | | componentStateSuccessItem | The address of the success key within the state object and its value (if it turns out to be a success). For example, let's say you want to validate 'city' in the following state object: this.state = {  contactDetails: {    address: {      city: '',       country: ''    },    phone: {      landline: '',       mobile: ''    }  },  contactErrors: {    address: {      city: '',       country: ''    },    phone: {      landline: '',       mobile: ''    }  }}The componentStateSuccessItem will be an object that leads to the value of 'city' inside the state object, and will also contain the success value. componentStateSuccessItem:{  contactDetails: {    address: {      city: e.target.value    }  }}Note that in the componentStateSuccessItem, each nested object will contain only a single key. | Object | Yes | | componentStateErrorItem | The address of the error key within the state object and its value (if it turns out to be an error). The rest of the explanation is similar to componentStateSuccessItem above.In case you choose to not pass a componentStateErrorItem, nix-react-state-validate will show no errors and will behave just like the setState function.| Object | No |