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

easy-form

v1.8.0

Published

A React HOC for validating form easily

Downloads

5

Readme

Easy Form

Easy Form is a React HOC that allow you to build forms easily and flexibly.

Example

Demo

Material-UI

Features

  • easy: manage form state conveniently
  • flexible: create awesome form easily with Material-UI, Ant-Design, Redux or any library you like.
  • powerful: support sync/async validation both.
  • tiny: only 19.7kb ungzipped.

Get Started

  1. Run yarn add easy-form react react-dom prop-types

  2. Render it!

import React from 'react';
import { ValidationField, createForm } from '../src';

const schema = {
  name: {
    validator: name =>
      new Promise((res, rej) => {
        setTimeout(() => {
          if (name) {
            res(name);
          } else {
            rej(name);
          }
        }, 200);
      }),
    message: 'Please input your username',
  },
  password: {
    validator: password => password,
    message: 'Please input your password',
  },
};

class LoginForm extends React.PureComponent {
  handleSubmit = e => {
    e.preventDefault();
    const { submit } = this.props;
    submit(data => console.log(data), error => console.log(error))();
  };
  render() {
    const { isValid } = this.props;
    return (
      <form onSubmit={this.handleSubmit}>
        <ValidationField name="name" label="Username">
          <input placeholder="Username" />
        </ValidationField>
        <ValidationField name="password" label="Password">
          <input placeholder="Password" />
        </ValidationField>
        <button
          style={{
            display: 'inline-block',
            marginLeft: 180,
          }}
          disabled={!isValid}
          type="submit">
          Login
        </button>
      </form>
    );
  }
}

export default createForm({}, schema)(LoginForm);

Apis

createForm(defaultValues, schema, options)

usage:

const schema = {
  birth: {
    validator: date => (date ? true : false),
    message: 'Please input your date of birth',
  },
  description: [
    {
      validator: description => (description ? true : false),
      message: 'Please input your description',
    },
    {
      validator(date) {
        return new Promise((res, rej) => {
          setTimeout(() => {
            date === 'loading' ? res(date) : rej(date);
          }, 0);
        });
      },
      message: name => `"${name}" is not my name!`,
    },
  ],
};
const DecoratedForm = createForm(
  {
    birth: '2018-05-28',
  },
  schema,
  { fieldRender },
)(CustomizedForm);

defaultValues: Object

Default values of the form.

schema: Object<[field: string]: Validator>

validator: (target: any, values: Object, preValues: Object, customOptions: Object) => bool | Promise

The validation rules of the form. You pass an array to customize more than one validators. And the validators will be executed sequentially. If validation passes, it should return true or a resolved promise. Else, it should return false or a rejected promise. The message should be a string or a function that receives value of input and result of validation and returns a string.

options: Object

| Property | Type | Default value | Description | | :---------------- | :--- | :---------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | fieldRender | Func | fieldRender | The field render prop. Arguments: fieldProps: Object - Props collection of form field Returns Object — The React node to render. | | onFormChange | Func | | Callback fired when the value of ValidationField gets changed. Arguments: props: Object — Props of The form component changedValue: Object — Value of the changed field defaultHandler: Func - Default handler | | onFormReset | Func | | Callback fired when the form is reset. Arguments: props: Object — Props of The form component newValues: Object — The reset value defaultHandler: Func - Default handler | | getValueFromEvent | Func | | Customized method to get value from event arguments. Arguments: same as event callback |

If the form has been decorated by createForm then it owns APIs as follows:

| Property | Type | Description | | :----------- | :--- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | isValid | bool | Whether the form is valid (has no validation error). | | isPristine | bool | Whether the current values of form are different from the initial values. | | isValidating | bool | Whether the form is validating. | | initialize | Func | Resets the form to specified values. | | submit | Func | Submits the form. Returns a promise that will be resolved when the form is submitted successfully, or rejected if the submission fails. Arguments: onSuccess: Func onFail: Func | | updateValues | Func | Updates values of the form. Arguments: newValues: Object | | updateSchema | Func | Updates schema of the form. Arguments: newSchema: Object | | validateAll | Func | Validates the form. | | validateItem | Func | Validates the specified field. Arguments: name: string - Name of the field to validate |

ValidationField

| Property | Type | Default value | Description | | :-------------- | :----- | :------------ | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | formatter | Func | | The Handler that format the value. Arguments: value: string \| boolean \| number — The value of input. Returns Object — The formatted value. | | name | string | Required | The unique identifier of field, corresponding to a value in the form values. | | onValidate | Func | | Callback fired after validation. Arguments: result: Object — The result of validation. You can pull out the return of the validator by accessing result.promiseValue. | | options | Object | | Additional options that can be passed to the validator function. | | render | Func | Required | A render prop. Use the property to get what to render. Arguments: props: Object — Please refer to options.fieldRender. Returns Object — The React node to render. | | trigger | string | onChange | When to collect the value of children node. | | validateTrigger | string | onChange | When to validate the value of children node. | | valuePropName | string | value | Prop that should be validated. For example, the valuePropName of checkbox is checked. |