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

self-validation-hoc

v2.0.3

Published

A minimal and sensible form validation library for React apps

Downloads

9

Readme

SelfValidationHoc

A minimal and sensible form validation library for React apps.

Build Status Code Coverage

Requirements for This Library

  • Adds validation to exiting forms
  • Reduces interaction with the form to a bare minimum
  • Uses standard validation attributes
  • It's not a mixin, rather a HOC, if necessary
  • Marks form elements as "touched" when input is changed or form is submitted
  • Works for both, react-bootstrap and standard forms
  • Custom form fields can be easily adapted to it

Resulting Properties

  • Can validate any custom field that's built on top of a standard form element
  • For other custom fields, it exposes a simple API for them to report their validity to their form
  • Always cancels submit event, assuming no one will ever want a submit to navigate off the page

Usage

This library consists of three higher order components (HOCs). SelfValidating HOC runs validity checks on submit event of the provided form component, which has to be built on top of the standard form, and calls onSubmit handler if the form is valid. Return values of higher order functions, Touchable and TouchableCustom, manage a couple of CSS classes, touched and invalid, of provided form field components.

Standard Fields

You only need to pass your form through the SelfValidating function, and optionally your fields through the Touchable function.

import React from 'react';
import {FormGroup, FormControl, ControlLabel, HelpBlock, Button} from 'react-bootstrap';

+import {SelfValidating, Touchable} from './index';
+
+const SelfValidatingStandardForm = SelfValidating(props => (
+    <form {...props} />
+));
+const TouchableFormControl = Touchable()(FormControl);
+
const Page = () => (
-    <form onSubmit={() => { alert('sumbitted'); }}>
+    <SelfValidatingStandardForm onSubmit={() => { alert('sumbitted'); }}>
        <FormGroup controlId="short-text">
-            <FormControl
+            <TouchableFormControl
                required
            />
            <ControlLabel>
                A short text field
            </ControlLabel>
            <HelpBlock>This field is required</HelpBlock>
        </FormGroup>
        <Button type="submit">Submit</Button>
-    </form>
+    </SelfValidatingStandardForm>
);

export default Page;

Custom Fields That Don't Fire change or invalid

You first need to make a few adaptations to your custom field.

// CustomField.jsx

import React from 'react';

export default class CustomField extends React.Component {
    static defaultProps = {
+        onValidityChange: () => {},
+        onEndCheckValidity: () => {},
+        endCheckValidity: false,
    };
    
    componentWillReceiveProps(nextProps) {
+        if (nextProps.endCheckValidity) {
+            nextProps.onEndCheckValidity(true); // or false
+        }
    }
    
    handleChange() {
+        this.props.onValidityChange(true); // or false
    }
    
    render() {}
}

That is all your custom field is required to have, three additional properties, onValidityChange, onEndCheckValidity and endCheckValidity and a way to determine validity of its input value. It should call onEndCheckValidity when the self-validating form wants it, that is when endCheckValidity is true, and onValidityChange whenever the input changes.

You then pass your CustomField through the TouchableCustom function.

Unlike with standard fields, where using Touchable is optional, you have to pass your custom field through TouchableCustom in order for self-validating form to determine the validity of the field.

import React from 'react';

import CustomField from './CustomField';

+const SelfValidatingStandardForm = SelfValidating(props => (
+    <form {...props} />
+));
+const TouchableCustomField = TouchableCustom()(CustomField);
+
const Page = () => (
-    <form onSubmit={() => { alert('sumbitted'); }}>
+    <SelfValidatingStandardForm onSubmit={() => { alert('sumbitted'); }}>
        <div className="form-group">
-            <CustomField
+            <TouchableCustomField
                id="custom-field"
                required
            />
            <label htmlFor="custom-field">
                A custom field
            </label>
            <div className="help-block">This field is required</div>
        </div>
        <button>Submit</button>
-    </form>
+    </SelfValidatingStandardForm>
);

export default Page;

Changing CSS Classes

touched and invalid CSS classes can be renamed to adhere to your project's conventions by passing a configuration object to Touchable/TouchableCustom.

-const TouchableFormControl = Touchable()(FormControl);
+const TouchableFormControl = Touchable({
+    touchedClassName: 'input--touched',
+    invalidClassName: 'input--invalid',
+})(FormControl);