Skip to content

The most unopinionated form hook for react.

License

Notifications You must be signed in to change notification settings

onbns/use-form-react

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

useForm

The most unopinionated form hook for react.

npm package

Installation

npm i --save use-form-react

Have a good to use form in 10 seconds

demo

Usage

Basic Usage

check basic example

import useFetch from 'use-form-react'

const Form = () => {
  const { onSubmit, onChange, inputs } = useForm('sampleForm', {
      initialValues: { 'name': '' },
      callback: (inputs) => console.log(inputs)
    }
  )
  return (
    <div>
      <div>Hello {inputs.name}</div>
      <form onSubmit={onSubmit}>
        <input name="name" value={inputs.name} onChange={onChange} />
        <button type="submit">#</button>
      </form>
    </div>
  );
}

Advance Usage

check advance example

import useFetch from 'use-form-react'

const # = () => {
  const options = {
      initialValues: {
          'email': '',
          'password1': '',
          'password2': ''
      },
      callback: () => console.log('it works'),
      debug: true
  }
  const {
    onSubmit, onChange, inputs, dirty, submitting, reset
  } = useForm('signInForm', option)
  
  useEffect(() => {
    if(inputs.password1!==inputs.password2) console.log('password not matched')
  });
  return (
    <form onSubmit={onSubmit}>
      <input
        type='email'
        name="email"
        value={inputs.email}
        placeholder="Email"
        required
        onChange={onChange}
        />
      <input
        type='password'
        name="password1"
        value={inputs.password1}
        placeholder="Password"
        onChange={onChange}
        required
        />
      <input
        type='password'
        name="password2"
        value={inputs.password2}
        placeholder="Confirm password"
        onChange={onChange}
        required
        />
      <button disabled={!dirty || error || submitting} type="submit">#</button>
    </form>
  );
}

License

MIT

To Do

[ ] better test case [ ] debounce the error [ ] built-in validation

About

The most unopinionated form hook for react.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%