0.3.4 • Published 6 years ago

@uneksija/useform v0.3.4

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

useform

React hook for simple form control

npm (scoped) GitHub

Installation

npm install @uneksija/useform

Importing the hook

import useForm from '@uneksija/useform'

Example usage

function validate({ age }) {
  if (age < 18) return { age: '18+ only' }
}

function App() {
  const { values, errors, isValid, handleChange, handleSubmit } = useForm({
    validate,
    initialValues: {
      name: 'john',
      age: 23
    },
    onSubmit: console.log
  })

  return (
    <form onSubmit={handleSubmit}>
      <label>
        name
        <input
          name="name"
          type="text"
          value={values.name}
          onChange={handleChange}
        />
      </label>
      <br />
      <label>
        age
        <input
          name="age"
          type="number"
          value={values.age}
          onChange={handleChange}
        />
        {errors.age}
      </label>
      <br />
      <button type="submit" disabled={!isValid}>
        save
      </button>
    </form>
  )
}

Arguments

ArgumentDescription
initialValuesObject containing the form initial values
onSubmitFunction called with the values on form submit
validateValidate function, returns an object of error messages

Return object

KeyDescription
valuesForm values
errorsError messages
isValidFlag indicating if there are any errors
handleChangeOnChange handler, input name needs to match value key
handleSubmitOnSubmit handler
0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.0

6 years ago