GithubHelp home page GithubHelp logo

doc22940 / react-hook-form-input Goto Github PK

View Code? Open in Web Editor NEW

This project forked from react-hook-form/input

0.0 1.0 0.0 1.14 MB

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

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

JavaScript 9.63% TypeScript 90.37%

react-hook-form-input's Introduction

๐Ÿ“ฃ UPDATE

This component is now a part of React Hook Form V4, and renamed to Controller with much simpler API.

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 React Web demo or React Native demo.

Quickstart

React Web

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' },
];

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">Reset Form</button>
      <button>submit</button>
    </form>
  );
}

React Native

import * as React from 'react';
import { View, TextInput, Button } from 'react-native';
import useForm from 'react-hook-form';
import { RHFInput } from './index';

export default () => {
  const { register, setValue, handleSubmit } = useForm();
  const onSubmit = data => console.log(data);

  const onChange = args => ({
    value: args[0].nativeEvent.text,
  });

  return (
    <View style={styles.container}>
      <RHFInput
        register={register}
        setValue={setValue}
        as={<TextInput />}
        onChangeEvent={onChange}
        name="firstName"
      />

      <Button title="Button" onPress={handleSubmit(onSubmit)} />
    </View>
  );
};

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
unregister Function (optional when using FormContext) React Hook Form unregister 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
onChangeName string This prop allow you to target that specific event name, eg: when onChange event is named onTextChange
onChangeEvent Function Callback function to return value or checked. event callback argument may have different signature and this props allow you to customise the value return.
onBlurName string This prop allow you to target that specific event name, eg: when onBlur event is named onTextBlur
onBlurEvent Function Callback function to return value or checked. event callback argument may have different signature and this props allow you to customise the value return.
...rest Object Any props assigned will be pass through to your Input component

Backers

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

Contributors

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

Thanks to

react-hook-form-input's People

Contributors

bluebill1049 avatar gprost avatar iamchanii avatar jeromedeleon avatar joelsaupe avatar riccardocampitelli avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.