1.1.10 • Published 4 years ago

react-hook-form-input v1.1.10

Weekly downloads
4,034
License
MIT
Repository
github
Last release
4 years ago

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

PropTypeRequiredDefaultDescription
asComponentComponent reference eg: Select from react-select
namestringUnique name to register the custom input
setValueFunction(optional when using FormContext) React Hook Form setValue function
registerFunction(optional when using FormContext) React Hook Form register function
unregisterFunction(optional when using FormContext) React Hook Form unregister function
modestringonSubmitMode option for triggering validation
rulesObjectundefinedValidation rules according to register at React Hook Form
typestringinputCurrently support checkbox or input input type includes: radio and select
onChangeNamestringThis prop allow you to target that specific event name, eg: when onChange event is named onTextChange
onChangeEventFunctionCallback function to return value or checked. event callback argument may have different signature and this props allow you to customise the value return.
onBlurNamestringThis prop allow you to target that specific event name, eg: when onBlur event is named onTextBlur
onBlurEventFunctionCallback function to return value or checked. event callback argument may have different signature and this props allow you to customise the value return.
...restObjectAny 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

  • Inspiration goes to Jed Watson @github issue #3855.
  • Shaping better API Jerome De Leon
1.1.10

4 years ago

1.1.10-beta.2

4 years ago

1.1.1-0.beta.1

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.6-beta.1

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.2

4 years ago

1.1.2-beta.1

4 years ago

1.1.0

4 years ago

1.0.14

4 years ago

1.0.14-beta.2

4 years ago

1.0.14-beta.1

4 years ago

1.0.13

4 years ago

1.0.13-beta.1

4 years ago

1.0.12

4 years ago

1.0.12-beta.1

4 years ago

1.0.11

4 years ago

1.0.11-beta.2

4 years ago

1.0.11-beta.1

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago