JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q35524F
  • License MIT

manage form state use React Hooks

Package Exports

  • rc-use-form

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (rc-use-form) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

rc-use-form

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

Build Status

Install

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

Demo

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")} />
        </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

field

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

Options

  • rules: validate rules use async-validate
  • trigger: Event which is listened to validate