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-validate-framework

v0.15.6

Published

React validation framework.

Downloads

226

Readme

react-validate-framework

A lightweight and extensible React validation component

npm travis-ci Coverage Status npm

中文 README

Demo: https://minjieliu.github.io/react-validate-framework

You can check out the code to see examples.

How to use?

npm i react-validate-framework --save

Import:

import formConnect, {
  Checkbox,
  Radio,
  Select,
  Text,
  Textarea,
  Message,
} from 'react-validate-framework';

Rules and messages like this:

const schemas = {
  email: {
    rules: 'required | isEmail | maxLength(32) | validateFromServer',
    messages: 'Can not be empty! | Please enter a valid email address. | Can not exceed {{param}} characters. | | The email already exists.',
  },
  hobby: {
    rules: 'requiredField(phone) | selectLimit(2)',
    messages: 'email and hobby at least one entry! | Select at least {{param}}.',
  },
};

const methods = {
  selectLimit(field, param) {
    if (Array.isArray(field.value)) {
      return field.value.length >= param;
    }
    return false;
  },
  requiredField(field, param) {
    const otherField = this.fields[param];
    return this.required(field) || (otherField.result && this.required(otherField));
  },
  async validateFromServer(field, param) {
    return await doServerAPI();
  },
};

The BasicForm like this:

@formConnect(schemas, methods)
export default class BasicForm extends React.Component {
  
  static propTypes = {
    formControl: PropTypes.object,
  };

  constructor(props) {
    super(props);
    props.formControl.init({
      email: '',
      phone: '',
    }, {
      static: 'form-control',
      success: 'valid-success',
      error: 'valid-error',
    });
  }

  handleSubmit = async () => {
    const { formControl } = this.props;
    if (await formControl.validate()) {
      // Submit.
    }
  };

  render() {
    return (
      <div className="form-group">
        <Text
          name="email"
          placeholder="Please input your email"
          delay={100} // Asynchronous validation
        />
        <Message className="valid-error-message" name="email" />
        <Text name="phone" />
        <button onClick={this.handleSubmit}>提交</button>
      </div>
    );
  }
}

Validate methods can refer to validate-framework-utils

Form components

  • Checkbox
  • Radio
  • Select
  • Text
  • Textarea
  • Message

The name attribute is required in form components, delay debounce, Other parameters can be overridden.

Of course, you can also use unencapsulated form components, just specify value and onChange on the line:

const {
  formControl: {
    fields,
    onFormChange,
  },
} = this.props;

return (
  <input
    className={fields.email.className}
    name="email"
    type="text"
    onChange={onFormChange}
    value={fields.email.value}
    placeholder="Please input your email"
  />
);

API

Form params

| name | type | return | setState | description | | :--- | :--- | :--- | :--- | :--- | | fields | Object | | | The collection of fields | | isAllValid | Boolean | | | Gets the global validation status | | formValues | Object | | | Gets a list of form values | | init | function | | this | Initializes the form value and classes | | initClassNames | function | this | false | Initializes classes | | onFormChange | function | | true | Form change event listener | | changeValues | function | this | true | Customize to change the values | | validate | function | Promise => Boolean | true | Validate all fields | | validateByNames | function | Promise => Boolean | true | Validate the component through names | | addValues | function | this | true | Add one or more value | | removeValues | function | this | true | Remove one or more value, If there is no name, it will all be removed | | resetValues | function | this | true | Reset one or more value, If there is no name, it will all be init | | addSchemas | function | this | false | Add one or more validation rules | | removeSchemas | function | this | true | Remove one or more validation rules, If there is no name, it will all be removed | | formDidChange | function | | | Callback |

You can invoke the changeValues method to simulate a form change event.

Update log

0.15.x

  • Remove the delay parameter from FormControl.
  • Add the delay parameter to the props of the form component.