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

formlite

v2.2.4

Published

a tool for building forms in react and validation form data

Downloads

53

Readme

formlite

A React component for building validation forms

Inspire ~~Copy~~ by react-jsonschema-form

Options

属性|描述|可选值类型 -|:-:|- label|标题|string description|描述|string required|是否必填|boolean initialValue|初始值|any validator|验证|function(value, callback){} -|-|[{pattern:/.{5,10}/, message: '长度必须是5到10位'}, ...]

Input.js

import React from 'react';
export default function Input({value='', onChange, ...others}){
  return <input value={value} onChange={(event)=>onChange(event.target.value)} {...others}/>;
}
Input.displayName = 'Input';

FormRenderer.js

import React from 'react';
export default class FormRenderer extends React.Component {
  render() {
    let {label, help, required, description, children, validating, error, decorator} = this.props;
    let className = error?'error':'success';
    return (
      <div className="form-renderer">
        <label className={`label ${required?'required':''}`}>{label}</label>
        <div className="decorator">
          {
            React.cloneElement(children, {className: `input ${className}`})
          }
          <div className="error-text">{error}</div>
        </div>

        {/*decorator?decorator(children):children*/}

        {validating && 'validating……'}
      </div>
    );
  }
}

Usage

常用

import FormLite from 'formlite';
import DatePicker from './DatePicker';
import Input from './Input';
import FormRenderer from './FormRenderer';

FormLite.registerComponent([DatePicker, Input]); // 注册可用的组件
FormLite.setDefaultItemRenderer(FormRenderer); // 设置表单渲染模板默认值
FormLite.setDefaultMode(FormLite.ALL); // 设置表单值变化时验证模式, ALL: 自动验证所有值, ITEM: 只验证当前修改值, NONE: 不自动验证

@FormLite
class Example extends React.Component{
  onDateChange = (name, value)=>{
    console.log(value);
    this.props.form.setValues({c: value.format('YYYY-MM-DD hh:mm:ss')});
    return true;
  };
  render(){
    let {Input, DatePicker} = this.props.form;
    return (
      <div>
        <Input name="a" style={{color: 'blue'}}/>
        <Input name="b"/>
        <Input name="c"/>
        <Input name="c" style={{color: 'blue'}} readOnly/>
        <DatePicker name="d" onChange={this.onDateChange}/>
        <button onClick={this.onSubmit}>submit</button>
        <button onClick={this.onReset}>reset</button>
      </div>
    );
  }
}

create

import FormLite from 'formlite';
@FormLite.create({
  a: {
    label: 'LabelA',
    description: 'A的描述',
    required: true,
    initialValue:"AAA",
    validator: [{
      pattern:/^.{5,10}$/, message: '长度必须是5到10位'
    }, {
      pattern:/^[0-9]+$/, message: '必须是数字'
    }]
  },
  b: {
    label: 'LabelB',
    description: 'B的描述',
    initialValue: 'BBB',
    validator(value, callback){
      setTimeout(()=>{
        callback(value == '123456'? null: '请输入123456');
      }, 1000);
    }
  }
})
class Example extends React.Component{
  render(){
    // ……
  }
}

Functions

import FormLite from 'formlite';
@FormLite
class Example extends React.Component{
  componentWillMount(){
      this.props.form.registerComponent([Input, DatePicker, Select]); // 注册私有组件
      this.props.form.setItemRenderer(FormRenderer); // 注册私有表单渲染模板
      this.props.form.setMode(FormLite.ALL); // 设置私有验证模式
      this.props.form.setOption({a: {}})
      // 设置默认值
      this.props.form.setInitialValues({b: 'initialValue B', a: 'initialValue A', c: moment('2030-12-24')});
   }
  render(){
    // ……
  }
}