npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@neolution-ch/react-hook-form-components

v2.13.0

Published

a collection of react hook form components to reduce boilerplate and figuring out how to integrate react hook form with other libraries

Downloads

1,976

Readme

react-hook-form-components

The idea of this package is to have a collection of components that can be used with react-hook-form without having to write a lot of boilerplate code. And also to make the life of the developer easier so they don't have to figure out how to integrate external libraries with react-hook-form.

Supported input types:

  • all standard html input types (text, email, number, etc.)
  • date picker (react-datepicker)
  • typeahead (react-bootstrap-typeahead)
  • numeric / pattern formats (react-number-format)

:warning: Basic usage is also possible without yup but you will have to handle validation and type conversions yourself. So we highly recommend using yup. See the example below.

Documentation

Installation

npm install @neolution-ch/react-hook-form-components
yarn add @neolution-ch/react-hook-form-components

Getting started

Basic usage

import { Form, Input } from 'react-hook-form-components';

interface FormInputs {
    testInput: string;
}

....

<Form<FormInputs> onSubmit={(data) => console.log(data)}>
    <Input<FormInputs> name={"testInput"} label={"Test Input"} />
    <input type="submit" />
</Form>

Yup

To use it with yup please install @hookform/resolvers and yup.

To make your life easier you can use the method described in this blog article to get strongly typed validation schema from yup: Strongly typed validation schema with yup.

import { Form, Input } from 'react-hook-form-components';
import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup";

interface FormInputs {
    numberInput: number;
}

const schema = yup.object({
  // this will trigger validation and show error message if the input is empty
  // it will also convert the input value to a number
  numberInput: yup.number().required(),
});

...

<Form<FormInputs> onSubmit={(data) => console.log(data)} resolver={yupResolver(schema)}>
    <Input<FormInputs> name={"numberInput"} label={"Number Input"} type="number" />
    <input type="submit" />
</Form>

Typeahead

To use typeahead you have to include their CSS file in your project:

import "react-bootstrap-typeahead/css/Typeahead.css";

Static Typeahead

Use the StaticTypeaheadInput component for a static list of options.

<StaticTypeaheadInput name="inputName" options={["one", "two"]} label="Static Typeahead" />

Options can also be an array of LabelValueOption objects. In this case you can have a different label and value.

<StaticTypeaheadInput
  name="inputName"
  options={[
    {
      label: "one",
      value: "one",
    },
    {
      label: "two",
      value: "two",
    },
  ]}
  label="Static Typeahead"
/>

Async Typeahead

To use an async typeahead you have to provide a function that returns a promise. The function will be called with the current input value.

<AsyncTypeaheadInput
  name="inputName"
  queryFn={async (query) => {
    return ["one", "two"];
  }}
  label="Async Typeahead"
/>

And also here you can have an array of LabelValueOption objects.

<AsyncTypeaheadInput
  name="inputName"
  queryFn={async (query) => {
    return [
      { value: "one", label: "one" },
      { value: "two", label: "two" },
    ];
  }}
  label="Async Typeahead"
/>

Datepicker

To use the DatepickerInput component you have to include their CSS file in your project:

import "react-datepicker/dist/react-datepicker.css";

Basic example:

<DatePickerInput name="datepickerInput" label="Date Picker" />

You get full access to the react-datepicker component. So you can pass all props to the datePickerProps prop of the DatePickerInput component.

So for example if you don't like the default date format of dd.MM.yyyy you can change it to yyyy-MM-dd like this:

<DatePickerInput
  name="datepickerInput"
  label="Date Picker"
  datePickerProps={{
    dateFormat: "yyyy-MM-dd",
  }}
/>

:warning: The date will always be set with a time so it matches midnight of the selected date when converted to UTC (which JSON.stringify does). So you will always get a

Numeric and Pattern Format

Numeric format

To use a numeric format (for example with a thousand seperator) you can use the Input component and supply numericFormat.

Refer to the react-number-format documentation for more information. If you use the numericFormat prop and declare the variable as a number with yup, you will get the unformatted value in your onSubmit function.

<FormattedInput
  name={"name"}
  label={"Numeric Format"}
  numericFormat={{
    thousandSeparator: "'",
  }}
/>

Pattern format

To use a pattern format (for example for a phone nr) you can use the Input component and supply patternFormat.

Refer to the react-number-format documentation for more information.

<FormattedInput
  name={"name"}
  label={"Pattern fomrat"}
  patternFormat={{
    format: "###-###-####",
    allowEmptyFormatting: true,
    mask: "_",
  }}
/>

Form Context

In order to correctly use the form context you have to import it like this:

import { useFormContext } from "@neolution-ch/react-hook-form-components";

This is needed, as importing it directly from the react-hook-form, will produce runtime errors. In addition, our context will provide some additional properties for your form.

Tooltip

To use the tooltip option of FormGroupLayoutLabel you have to import the package styles like this:

import "@neolution-ch/react-hook-form-components/styles.css";

Storybook

The storybook is a visual testing tool that makes it easy to test and tinker with the components.

It can be found at https://neolution-ch.github.io/react-hook-form-components