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-aptform

v1.1.2

Published

Practical forms for react

Downloads

7

Readme

About

npm gzip size build status

react-aptform is a library to work with forms in react efficiently. It aims for both DX and UX to make forms less tedious.

Installation

npm install --save react-aptform

Or with yarn:

yarn add react-aptform

The package expects at least [email protected].

Project goals

  • practicality first
  • well performant for reasonably small forms (< 30 inputs)
  • specify common form behaviour declaratively
  • API that doesn't go in your way

Documentation

Docs lives in docs directory of the repository.

Highlights:

See it in action on deployed examples and also check the example sources.

Many edge cases are implemented in the tests in the repository.

Features

Functional

  • storing values of controlled inputs
  • tracking input meta state (for custom UI e.g. to make UX better)
  • consistent forms behaviour throughout your app (preconfigure)
  • declarative input configuration allows consistent behavior of same inputs in different forms throughout your app
  • validation framework (i18n friendly errors, suggestions on showing the validation error/success)
  • submit framework (reset data on success, set server errors, managing submitting state)

Non functional

  • no need for custom class components for inputs or forms
  • does not render anything on its own neither it listens to DOM events
  • provides mature input meta state modeling
  • no setup needed (main component works out-of-box)
  • flow typed

Show validation insight

API includes methods to show validation error / success based on custom configured rules in order to improve UX.

i18n friendly validation

Validation is boolean-based rather string-based, it makes i18n simpler and its less fragile.

No actual rendering

We provide behavioral components that only hook into React system, but leaves rendering up to a function you provide. This avoids e.g. styling problems. You have total control of how your inputs look.

Use cases

  • login and signup forms
  • profile page
  • any temporary forms (including custom input components)

For more complex use cases see below for Alternatives.

Usage example

What you gain in this example by using the package:

  • you know whether a form is valid
  • you know whether an input is valid
  • you have a place to store input states to leverage controlled inputs (you can use the current value however you want in render)
  • you will receive all the values in a single object (onSubmit)
  • you can set default value for each input with a single object

What you need to do:

  • you must set all the required handlers on form and input components (you can leverage getPassProps)
  • you must provide a configuration for an input if its required or has validation

Notes:

  • value, name and onChange input props can be passed automatically with JSX expression {...email.getPassProps()}
  • you can set form handlers automatically too with {...form.getPassProps()}
  <Aptform
    initialValues={{
      name: 'Eliana Rendón',
      email: '[email protected]',
    }}
    onSubmit={values => {
      console.log('Name value: ', values.name);
      console.log('Email value: ', values.email);
    }}
    inputs={{
      name: { required: true },
      email: { validations: { isEmail: val => /@/.test(val) } },
    }}
    render={({ inputs, form }) => {
      const { name, email } = inputs;
      return (
        <form onBlur={form.onBlur} onFocus={form.onFocus} onSubmit={form.onSubmit}>
          <div>
            Name: <input type="text" value={name.value} name={name.name} onChange={name.onChange} />
          </div>
          <div>
            Email:
            <input type="email" value={email.value} name={email.name} onChange={email.onChange} />
          </div>
          <button type="submit" disabled={!form.isValid()}>
            Submit
          </button>
        </form>
      );
    }}
  />

Limitations

  • Typescript support (use Formik)
  • React native support (use Formik)
  • Nested fields (use Formik or react-final-form)

Project constraints:

  • browser only
  • no complex form solutions (nested, multi-step forms, ...)
  • must be i18n-friendly
  • conventions for common tasks (submit, validation)
  • only one API
  • enable great UX

Alternatives

This library was inherently inspired by following react form solutions:

  • https://github.com/jaredpalmer/formik (declarative configuration, input states)
  • https://github.com/final-form/react-final-form (input states, use cases in examples)
  • https://github.com/vacuumlabs/react-custom-validation (show validation recommendation)
  • https://github.com/christianalfoni/formsy-react (convenience)
  • https://github.com/erikras/redux-form (input states)

The package was created a moment before Formik and react-final-form were released. They're both good and this package is definitely not "an order of magnitude better". I still use it because of simplicity (no duplicate API), i18n-friendliness and made conventions, so I don't have to think about how to implement forms over and over.