Code Monkey home page Code Monkey logo

react-use-form's Introduction

rc-use-form

manage form state use React Hooks. https://ariesjia.github.io/react-use-form/

NPM Build Status

Install

// use yarn
yarn add rc-use-form
// use npm
npm install rc-use-form

Demo

simple

import useForm from 'rc-use-form';

const Demo = () => {
  const [form, field]  = useForm({
    name: '',
    password: ''
  })
  
  const handleSubmit = (event) => {
    event.preventDefault()
    console.log(form.value)
  }
  
  return (
    <div>
      <form onSubmit={handleSubmit}>
        <div>
          <label>username</label>
          <input type="text" {...field("name")}/>
        </div>
        <div>
          <label>password</label>
          <input type="password" {...field("password")} />
        </div>
        <button type='submit'>submit</button>
      </form>
    </div>
  )
}

validate

import useForm from 'rc-use-form';

const Demo = () => {
  const [form, field]  = useForm({
    name: '',
    password: ''
  })
  
  const handleSubmit = (event) => {
    event.preventDefault()
    form.validate((errors) => {
      if(!errors) {
        console.log(form.value)
        alert('submit')
      }
    })
  }
  
  return (
    <div>
      <form onSubmit={handleSubmit}>
        <div>
          <label>username</label>
          <input type="text" {...field("name", {
            rules: [{type: "string", required: true}]
          })}
          />
          {
            form.errors.name && <div>
                {form.errors.name[0].message}
            </div>
          }
        </div>
        <div>
          <label>password</label>
          <input type="password" {...field("password", {
            rules: [{type: "string", required: true}]
          })}
          />
          {
            form.errors.password && <div>
                {form.errors.password[0].message}
            </div>
          }
        </div>
        <button type='submit'>submit</button>
      </form>
    </div>
  )
}

form

  • value: The form data
  • touched: The field had been changed by user
  • errors: The form validate errors
  • validate: The form validate function
  • getValue: The form getValue function, always return current value

field

field(name, [options])
  • name: The field field (required).

Options

react-use-form's People

Contributors

ariesjia avatar hyperbolic-versor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

brust8023 0x0xy

react-use-form's Issues

可以考虑支持 getValue、setError方法

不错的库!
能不能提供方法手动获取单个值,以及手动设置错误,比如getValuesetError
具体可以参考antd的form表单,你这个库的处理方式跟antd的form表单很像。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.