controlled-form-hook
TypeScript icon, indicating that this package has built-in type declarations

2.0.11 • Public • Published

controlled-form-hook

React hook for handling forms with controlled components and validation.

NPM JavaScript Style Guide

Install

npm install --save controlled-form-hook
yarn add controlled-form-hook

Usage

import * as React from 'react'

import { useForm } from 'controlled-form-hook';
import { Validators } from 'tiny-validation';
const { isPresent, isEmail, isTrue, maxChars } = Validators;

type FormValues = {
  name: string;
  email: string;
  password: string;
  tos: false;
};

// ... inside your component

const {
    handleSubmit,
    handleFieldChange,
    isSubmitting,
    isDisabled,
    values,
    visited,
    errors,
    reset,
  } = useForm<FormValues>({
    onSubmit: submit,
    schema: {
      name: [isPresent()],
      email: [isPresent(), isEmail()],
      password: [isPresent(), maxChars(30)],
      tos: [isTrue()],
    },
    initialValues: {
      name: 'User McUserson',
      email: '',
      password: '',
      tos: false,
    },
  });

See the example folder in the repo for a full working example.

License

MIT © fractal-ly


This hook is created using create-react-hook.

Readme

Keywords

none

Package Sidebar

Install

npm i controlled-form-hook

Weekly Downloads

1

Version

2.0.11

License

MIT

Unpacked Size

720 kB

Total Files

34

Last publish

Collaborators

  • fractally