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-validation-form

v0.2.4

Published

A form component for React Library, supporting form validation.

Downloads

19

Readme

ReactForm

思想

高阶组件

Usage 使用

WARNING: This component is still under development

To install react-form:

npm install react-validation-form --save

How to use:

import React, { Component, PropTypes } from 'react';
import { createForm, FormItem } from 'react-validation-form';

const propTypes = {
  value: PropTypes.string,
};

class Input extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return <input type="text" value={this.props.value} {...this.props} />
  }
}

Input.propTypes = propTypes;

class TestForm extends Component {
  render() {
    return (
      <form>
        <FormItem>
          <Input {...this.props.form.getInputProps('username', {
            initialValue: 'username',
            onlyFirst: true, // default false
            validates: [
              {
                rules: [{
                  required: true
                },
                {
                  validator: (value, rule, formdata, callback) => {
                    setTimeout(() => {
                      callback(new Error('不合法的输入'));
                    }, 1000);
                  },
                }],
                triggers: ['onChange', 'onBlur'],
              },
            ]
          })} />
        </FormItem>
        <FormItem>
          <Input {...this.props.form.getInputProps('password', {
            validates: [
              {
                rules: [{
                  validator: (value, rule, formdata, callback) => {
                    setTimeout(() => {
                      callback(new Error('不合法输入'));
                    }, 1000);
                  },
                }],
                triggers: ['onChange', 'onBlur'],
              },
            ]
          })} />
        </FormItem>
        <FormItem>
          <Input {...this.props.form.getInputProps('nickname', {
            validates: [
              {
                rules: [
                  {
                    required: true,
                    type: 'string',
                    max: 20,
                    min: 5,
                  },
                  {
                  validator: (value, rule, formdata, callback) => {
                    setTimeout(() => {
                      callback(new Error('不合法输入'));
                    }, 1000);
                  },
                }],
                triggers: ['onBlur'],
              },
            ]
          })} />
        </FormItem>
        <button onClick={(e) => {
          e.preventDefault();
          this.props.form.validateAllInputs((err, namevalues) => {
            console.log(err);
          })
        }}>提交</button>
      </form>
    )
  }
}

// now you can use the form.
export default createForm(TestForm);

问题记录

问题1

对于异步验证,举个例子,加入是 onChange 触发验证,那么对于频繁修改表单,会不断地触发回调,造成显示上出现验证和验证结束UI之间的闪烁 (初步解决方案,通过观察现在的值跟之前放进去的值是否一致来决定是否触发回调)

经验证,以上方法仍然会闪动,当来回删除键入同一个字符即可。

解决方案: 目前采用记录一个 validateId 来解决,记录每一个验证的 validateId,效果良好

问题2

加入脏检查

原因:一个表单元素可能在 onChange 的时候不触发验证,然后在 onBlur 的时候触发验证,如果表单元素的值没有被改过,不应该触发验证,使用脏检查可以避免不必要的验证

问题3

onChangeonBlur 是检测两种错误的,onChange 检测出了一个错误,然后错误显示出来,接着触发 onBlur,此时 onBlur 触发的错误应该和 onChange 触发的错误同时显示出来,而不是只显示 onChange 触发的错误,但是两次触发 onBlur 只应该显示最后一次发生的错误,但是,onChangeonBlur 可能是检测同一个错误的,这就要求程序要记录 validates 在数组中的序号,这样就能达到要求,不过目前 Validator 的设计是传入 rules 数组(会把 validates 数组进行转换变成 rules 数组),所以现在暂时是显示最后一次验证得出的错误。