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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-form-core

v6.0.0

Published

React Form Core is a lightweight utility to create your form components easily.

Readme

React Form Core

React Form Core is a lightweight, only 1.1 kB, utility to create your form components easily. Just use the Form component, and it works out of the box.

Demo

Demo CodeSandbox.

typescript minified + gzip dependencies license

Install

You can use npm or yarn to install it.

$ npm install --save react-form-core

Let's Play

First of all, let's create a Validator component to validate the values of the fields if it is required.

./components/validator.js

import React, { useContext } from 'react';
import { FormContext } from 'react-form-core';

export const Validator = ({ children, validation, name, ...rest }) => {
    const { errors, setError, setSuccess, successes, values } = React.useContext(FormContext);
    const element = children;
    const onChange = (ev) => {
        validation({
            errors,
            setError: (error) => setError(name, error),
            setSuccess: (success) => setSuccess(name, success),
            successes,
            value: ev.target.value,
            values,
        });
    };

    return React.cloneElement(element, {
        ...rest,
        onChange,
    });
};

Create a simple Field component wrapper with an error message. Use the FormContext to get and set data to the context of the form, and the Validation component to validate the field values.

./components/field.js

import React, { useContext } from 'react';
import { FormContext } from 'react-form-core';
import { Validator } from './validator';

export const Field = ({ children, label, name, validation }) => {
    const { errors } = useContext(FormContext);
    const errorMessage = errors && errors[name];
    const errorClassName = errorMessage ? 'has-error' : '';

    return (
        <label className={errorClassName}>
            <span>{label}</span>
            {validation ? (
                <Validator name={name} validation={validation}>
                    {children}
                </Validator>
            ) : (
                children
            )}
            {errorMessage && <span>{errorMessage}</span>}
        </label>
    );
};

Use the Field component to create an input.

./components/input.js

import React from 'react';
import { Field } from './components/field';

export const Input = ({ label, validation, ...rest }) => {
    return (
        <Field label={label} validation={validation} {...rest}>
            <input {...rest} />
        </Field>
    );
};

As you will need to submit the form, let's create a submit button and disabled it if there is an error.

./components/submit.js

import React, { useContext } from 'react';
import { FormContext } from 'react-form-core';

export const Submit = ({ ...rest }) => {
    const { errors } = useContext(FormContext);
    const hasErrors = !!Object.keys(errors).length;

    return (
        <button disabled={hasErrors} type="submit" {...rest}>
            Submit
        </button>
    );
};

Add the components to the form and create the application view.

./views/app.js

import React, { useContext } from 'react';
import { Form } from 'react-form-core';
import { Input } from './components/input';
import { Submit } from './components/submit';

export const App = () => {
    const validation = ({ value, setError }) => value && setError('Submit disabled ;)');
    const onSubmit={({ ev, errors, values }) => {
        // send the form using the values or the form event 
    }}

    return (
        <Form onSubmit={onSubmit}>
            <p><Input label={'First name: '} name={'firstName'} validation={validation} /></p>
            <p><Submit /></p>
        </Form>
    );
}

FormContext API

API to get and set errors and successes messages, and get the values from the form.

  • errors:

    Object of errors by field name

    { <name>: <error_message> }

  • setError:

    Set errors by field name

    setError(<name>, <error_message>)

  • setSuccess:

    Set successes by field name

    setError(<name>, <success_message>)

  • successes:

    Object of successes by field name

    { <name>: <success_message> }

  • values:

    Object of values by field name

    { <name>: <value> }

Compatible Versioning

Summary

Given a version number MAJOR.MINOR, increment the:

  • MAJOR version when you make backwards-incompatible updates of any kind
  • MINOR version when you make 100% backwards-compatible updates

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR format.

ComVer

Contribute

First off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else.

Reporting Issues

Found a problem? Want a new feature? First of all, see if your issue or idea has already been reported. If it hasn't, just open a new clear and descriptive issue.

Commit message conventions

We are following AngularJS Git Commit Message Conventions. This leads to more readable messages that are easy to follow when looking through the project history.

Submitting pull requests

Pull requests are the greatest contributions, so be sure they are focused in scope and do avoid unrelated commits.

  • Fork it!
  • Clone your fork: git clone http://github.com/<your-username>/react-form-core
  • Navigate to the newly cloned directory: cd react-form-core
  • Create a new branch for the new feature: git checkout -b my-new-feature
  • Install the tools necessary for development: npm install
  • Make your changes.
  • npm run build to verify your change doesn't increase output size.
  • npm test to make sure your change doesn't break anything.
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request with full remarks documenting your changes.

License

MIT License