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

reactive-form

v0.2.2

Published

Simple form management for react

Downloads

42

Readme

Barebone Form Validation for ReactJS

![NPM version][npm-image] ![npm download][download-image]

Bring simple validation to any react froms. To use, simply wrap a React input using the connectInput() API to instantenously give it validation capabilities (see bellow 1).

Inputs

  1. Start by creating connected inputs using the connectInput API. i.g.:

TextInput.js

import React, { Component } from 'react';
import { connectInput } from '../../src/';

class TextInput extends Component {
  handleChange(e) {
    // This propagate the input value to the state
    this.props.propagateValue(e.target.value)
  }

  render() {
    const {
      showErrors,
      value
    } = this.props;

    return (
      <input className={showErrors && 'error'}
        type="text"
        value={ value }
        onChange={ (e) => this.handleChange(e) } />
    );
  }
}
export default connectInput(TextInput);

This is will provide your inputs with 2 functionalities :

  • Validation: They can now take a validate property that accepts an array. See bellow the Connected From section
  • Character restriction: Display current length of the input, and restrict to a maximun

Connected Form

Now lets' connect the form. Simply create a react from and wrap it in reactivizeForm(). This is will provide your form with 3 APIs:

  1. isValid: Return true or false
  2. showErrors: Will set the displayErrors attribute to true, allowing to display error message on your form
  3. getFormData: Creates a object of all the inputs values

i.g.:

export class SimpleForm extends Component {
  handleSubmit(e) {
    e.preventDefault();
    if (this.props.isValid()) {
      // Use the form data as you wish
      console.log('Here is your form data: ', this.props.getFormData());
    } else {
      // Tell the form to show all errors
      this.props.showErrors();
    }
  }

  render() {
    const {displayErrors} = this.props
    return(
      <form onSubmit={ (e) => this.handleSubmit(e) }>
        <TextInput ref="simpleInput"
            validate={['required']} // Set validation rules
            displayErrors={displayErrors}/>
        <button type="submit">Validate</button>
      </form>
    )
  }
}

default export reactivizeForm(SimpleForm);

Do not forget to give your inputs a ref and to pass them the displayErrors paramter provider by the form

Validation rules

Currently avaialble validations:

  • email
  • alpha-num
  • required
  • num
  • int
  • phone
  • isImage

Those can be used on a connectInput()ed input, like so:

  <TextInput validate={['required']} displayErrors={displayErrors} />

connectInput config object

A second, optional config object parameter, can be passed to connectInput. It contains the following options:

  • defaultErrorMessages: An object, containing custom error message for each validator ('email', 'num', etc.). For application wide consistency, these message should be stored into a separate module, and passed to all connectInput()ed inputs:
connectInput(TextInput, {
  email: 'Enter a valid email addres',
  num: 'Numerical characters only'
  ...
})
  • errorFormatter: A function throught which will be passed all error messages. Mostly useful for running message throught a translation system

  • errorFormatter: A function throught which will be passed all error messages. Mostly useful for running message throught a translation system

  • defaultValidator: A object containing a validation function (that takes a value and spit out true or false) and a string for error message:

const validateURL = (value) => {
  if (!value) return false;
  const target = value.target;
  return /^(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(target);
}


const defaultValidator = {
  validationFunction: validateURL,
  errorMessage: 'forms.errors.URL_not_valid'
}

export default connectInput(TextInput, {defaultValidator});