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

hookform-field

v1.1.1

Published

Easy manage form fields, ensuring type-safety and strong reusability built with react-hook-form

Downloads

16

Readme

Thumbnail

Build Size Version Downloads

Documentation

The documentation here

Features

  • Type-safe
  • Strongly reusable
  • Easy to use
  • Extends all the awesome features from react-hook-form.

Overview

This documentation will guide you through the usage of the custom form components built using React Hook Form. This package help you manage easily form fields such as text input, number input, file input, and select dropdown, etc.. Below, you will find detailed instructions on how to set up and use these components in your React application.

Installation

First, install the package via npm/yarn/pnpm:

# npm
npm install hookform-field react-hook-form

# yarn
yarn add hookform-field react-hook-form

# pnpm
pnpm install hookform-field react-hook-form

Usage

Step 1: Define Your Custom Fields

You can create custom form fields by using the createField function. For example, to create text, number, file, and select fields:


// Example: <home>/<your-project>/src/components/form/field.tsx

import React from "react";
import { createField } from "hookform-field";

import Foo from 'your_component_path'

interface InputProps {}
interface NumberProps {}
interface FileProps {}
interface SelectProps {
  options: any[];
}

const Field = createField({
  text: (props: InputProps) => <input type="text" {...props} />,
  number: (props: NumberProps) => <input type="number" {...props} />,
  file: (props: FileProps) => <input type="file" {...props} />,
  select: (props: SelectProps) => (
    <select {...props}>
      {props.options.map((option, index) => (
        <option key={index} value={option.value}>
          {option.label}
        </option>
      ))}
    </select>
  ),

  foo: Foo,
});

export default Field;

Step 2: Create Your Form

Next, create a form using the Form component and include your custom fields:

import React from "react";
import { Form } from "hookform-field";
import Field from "@/components/form/field"; // Import the Field component you created

import { z } from "zod"
import { zodResolver } from "@hookform/resolvers/zod" // Import your resolver from hookform

const schema = z.object({
  name: z.string(),
  amount: z.number(),
  avatar: z.any(),
  age: z.string(),
})

type SchemaInferred = z.infer<typeof schema>

const resolver = zodResolver(schema)

const MyForm = () => {
  return (
    // `useForm` wrapped by <Form /> of hookform-field help you save time define it
    <Form<SchemaInferred>
      resolver={resolver}
      defaultValues={{ name: "Bob" }}
      onSubmit={(values) => console.log(values)} // <-- type-safe / infer type
    >
      <Field label="Name" component="text" name="name" />
      <Field component="number" name="amount" />
      <Field label="File" component="file" name="avatar" />
      <Field component="select" name="age" options={[{ value: '1', label: '1' }, { value: '2', label: '2' }]} />
    </Form>
  );
};

export default MyForm;

The <Field /> component will be type-safe based on your component values, suggesting the correct props based on the component props.

Step 3: Render Your Form

Finally, render your form in your application:

import React from "react"
import ReactDOM from "react-dom"
import MyForm from "./MyForm" // Import the MyForm component you created

const App = () => {
  return (
    <div>
      <h1>My Custom Form</h1>
      <MyForm />
    </div>
  )
}

Testing

TBD

Conclusion

This documentation provides a comprehensive guide to using the custom form components in your React application. By following these steps, you can create and render various form fields and ensure they function correctly through testing.