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

v0.2.8

Published

A React form library with RxJS validation streams

Downloads

6

Readme

React Formic

Asynchronous form validation made easy.

##The problem

As soon as an application moves beyond simple asynchronous flows, callbacks and Promises start to struggle. This point arrives fairly quickly when trying to perfom complex form validations involving debouncing or server-side validation, with code quickly turning into hard to fathom spaghetti.

##The solution

RxJS provides a rich and mature API for dealing with asynchronicity in JavaScript. For example, the two problems mentioned above can be solved in three extremely clear and declarative lines:

userNameInputValueStream
.debounce(300)
.flatMapLatest(value => isUnique(value).map(response => response.body.userNameExists))

The stream above debounces the user name input and checks that it doesn't already exists on the server via the isUnique method that returns a stream containing the server's response. This value is then mapped to a boolean making it easy to consume for the application.

React Formic provides a way to leverage the features of RxJS in this problem space as well as providing an extensible API with default implementations for state storage using component state and Redux.

If you're unfamiliar with RxJS and functional reactive programming, make sure to check out this excellent introduction by Andre Staltz, author of Cycle.js.

##Quick start

More documentation is on the way, but for now the quickest way to get going is to clone the repo and take a look at the examples app.

Install via NPM:

npm install react-formic

And a snippet showing how to construct a simple form:

import React from 'react';
import { isEmail } from 'validator';
import {
  ErrorMessage,
  Input,
  SubmitButton,
	initialize,
  validity,
  checkboxStates,
} from 'react-formic';
import { connectLocalState } from 'react-formic/lib/stateWrappers/localStateWrapper';

const { INVALID, VALID } = validity;
const { CHECKED } = checkboxStates;

const config = {
  stateWrapper: connectLocalState,
  fields: {
    email: {
      isRequired: true,
      valueStream: valueStream => valueStream
        .startWith('[email protected]')
        .map(value => value.toLowerCase()),
      validationStream: valueStream => valueStream
        .debounce(300)
        .map(value => ({
          validity: value && isEmail(value) ? VALID : INVALID,
          validityMessage: 'Must be a valid email',
        })),
    },
    receiveDarkSideEmail: {
      isRequired: true,
      validationStream: valueStream => valueStream
        .map(value => ({
          validity: value === CHECKED ? VALID : INVALID,
          validityMessage: 'Tick it or die!',
        })),
    },
  },
};

const SignUpForm = () => (
  <div>
    <h2>Email*</h2>
    <Input
      fieldName="email"
      type="text"
    />
    <ErrorMessage fieldName="email" />

		<h2>Confirmation*</h2>
    <Input
      fieldName="receiveDarkSideEmail"
      id="receiveDarkSideEmail"
      type="checkbox"
    />
    I would like to receive the Dark Side of the Force newsletter
    <ErrorMessage fieldName="receiveDarkSideEmail" />

    <SubmitButton
      className="Form_SubmitButton"
      onClick={event => {
        console.log('Submit!');
      }}
    >Submit</SubmitButton>
  </div>
);

export default initialize(config)(SignUpForm);

##Roadmap

  • Write documentation
  • Add more extensive tests
  • Some more exciting things