tttp / react-hook-form-input

πŸ“‹ Wrapper component for controlled inputs eg: MUI, AntD and React-Select

Home Page:https://codesandbox.io/s/react-hook-form-hookforminput-rzu9s

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Performant, flexible and extensible forms with easy to use validation.

npm downloads npm npm Tweet Join the community on Spectrum

Why?

React Hook Form embrace uncontrolled components and native inputs, however it's hard to avoid working with external controlled component such as React-Select, AntD and Material-UI. This wrapper component will make your life easier to work with them.

Features

  • Isolate re-rendering at component level
  • Integrate easily with React Hook Form (skip custom register at useEffect)
  • Enable reset API with Controlled Components without external state

Install

$ npm install react-hook-form-input

Demo

Check out this demo.

Quickstart

import React from 'react';
import useForm from 'react-hook-form';
import { RHFInput } from 'react-hook-form-input';
import Select from 'react-select';

const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' },
];

function App() {
  const { handleSubmit, register, setValue, reset } = useForm();

  return (
    <form onSubmit={handleSubmit(data => console.log(data))}>
      <RHFInput
        as={<Select options={options} />}
        rules={{ required: true }}
        name="reactSelect"
        register={register}
        setValue={setValue}
      />
      <button
        type="button"
        onClick={() => {
          reset({
            reactSelect: '',
          });
        }}
      >
        Reset Form
      </button>
      <button>submit</button>
    </form>
  );
}

API

Prop Type Required Default Description
as Component βœ“ Component reference eg: Select from react-select
name string βœ“ Unique name to register the custom input
setValue Function (optional when using FormContext) React Hook Form setValue function
register Function (optional when using FormContext) React Hook Form register function
mode string onSubmit Mode option for triggering validation
rules Object undefined Validation rules according to register at React Hook Form
type string input Currently support checkbox or input input type includes: radio and select
...rest Object Any props assigned will be pass through to your Input component

Contributors

Thanks goes to these wonderful people. [Become a contributor].

Backers

Thank goes to all our backers! [Become a backer].

Credit

About

πŸ“‹ Wrapper component for controlled inputs eg: MUI, AntD and React-Select

https://codesandbox.io/s/react-hook-form-hookforminput-rzu9s


Languages

Language:TypeScript 76.0%Language:JavaScript 24.0%