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

react-formutils

v1.0.1

Published

Utility functions and components for building forms with react quickly.

Downloads

1

Readme

React-Formutils

Make Form handling with React great again

Installation

npm

npm install react-formutils

yarn

yarn add react-formutils

Also you need to have react (v15 or higher) installed because React-Formutils relies on it as a peer dependency.

Examples

import {InputBase, FieldErrorContainer, FormBase} from "react-formutils"

/**
* An Input that sync its value with a parent form and receives an error message from the form.
* The inputs error message will only be shown, when the input was touch (e.g. blured) at least once.
*/
class MyInput extends InputBase {
    render() {
        return (
            <p>
                <label>{this.props.label}</label>
                { this.renderInput({type: this.props.type}) }
                { this.isTouched() && this.getError() && <FieldErrorContainer forms={this.props.forms}/> }
            </p>
        )
    }
}

/**
* A form that will show an error message if its value is invalid and that prevents submission as long as its data is invalid.
* On every value change its `getErrors` function will be called to calculate current error state and pass it to the corresponding controls.
*/
class MyForm extends FormBase {
    render() {
        return (
            <div>
                <MyInput label="Username" forms={[this, "username"]}/>
                <MyInput label="Password" type="password" forms={[this, "password"]}/>
                <MyInput name="Stay loged in" type="checkbox" forms={[this, "logged_in"]}/>
                {!this.isValid() &&
                <b>Cannot submit: Form is invalid</b>
                }
                <button disabled={!this.canSubmit()} onClick={this.submit}>Submit</button>
            </div>
        );
    }

    onSubmit() {
        alert(JSON.stringify(this.state.form.data))
    }

    getErrors() {
        const {username, password, color} = this.state.form.data;
        return {
            username: (!username || !username.length ) && "Reguired",
            password: (!password || password.length < 6) && "Min length is 6 characters"
        }
    }    
}
import {InputContainer, FieldErrorContainer, FormContainer} from "react-formutils"


/*
* This is basically the same example as above but using "Container Components". They are in fact extensions of the inputbase and formbase classes.
*/

const MyInput = ({label, forms, type}) => <InputContainer
        type={type}
        forms={forms}
        render={({input, touched, error}) =>
            <p>
                <label>{label}</label>
                <input {...input} type={type} />
                { touched && error && <FieldErrorContainer forms={forms}/> }
            </p>
        }
    />

const MyForm = () => <FormContainer
    render={({form, valid, canSubmit, submit}) =>
        <div>
            <MyInput label="Username" forms={[form, "username"]}/>
            <MyInput label="Password" type="password" forms={[form, "password"]}/>
            <MyInput label="Stay loged in" type="checkbox" forms={[form, "logged_in"]}/>
            {!valid &&
            <b>Cannot submit: Form is invalid</b>
            }
            <button disabled={!canSubmit} onClick={submit}>Submit</button>
        </div>
    }
    onSubmit={(data) =>
        alert(JSON.stringify(data))
    }
    validate={({username, password}) =>
        ({
            username: (!username || !username.length ) && "Reguired",
            password: (!password || password.length < 6) && "Min length is 6 characters"
        })
    }    
    />

For more examples, see the examples directory

Api & Documentation

View the Api

License

MIT