0.2.6 • Published 3 months ago

react-geek-form v0.2.6

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Logo

MIT License Status WIP

A set of form utilities built on top of react-hook-form and zod.

Installation

npm i react-geek-form zod

Quickstart

import { z } from 'zod';
import { createForm } from 'react-geek-form';

const { forwardFormContext } = createForm({
  zodSchema: z.object({
    email: z.string().min(1, 'Required'),
    password: z.string().min(1, 'Required'),
  }),
});

const LoginForm = forwardFormContext((props, ctx) => {
  return (
    <form onSubmit={ctx.handleSubmit((values) => {
      // do anything with values
    })}>
      <input {...ctx.register('email')} />
      <input {...ctx.register('password')} />
    </form>
  )
});

API's 📖

createForm

createForm is a utility that creates a closure that encapsulates all the functionality of react-hook-form's "useForm" hook and zod .

Props

  • zodSchema

    • Defines the validation schema of your form using "z" object from zod. This will also enables typescript type safety and code auto complete specially while using the ctx object.

    Return

  • forwardFormContext

    • Wraps your form component and injects the ctx object to the second parameter (just like the react's forwardRef).
    • Adds optional onInitializedFormContext prop to wrapped component.
const LoginForm = forwardFormContext((props, ctx) => {
  ...
});

const MyApp = () => {
  return (
    <div>
      <LoginForm
        onInitializedFormContext={(ctx) => {
          // do anything with ctx
          // e.g. ctx.getValues() to get the form values outside the component!
        }}
      />
    </div>
  );
};
  • withFieldContext
    • Wraps custom field component and pass appropriate props such as "register" or "control" and "error".
    • Adds type safety / auto complete to name prop.
    • Requirements;
      • Field component that will be wrap must have "register" or "control" and "name" props.
type Props = {
  name: string;
  register: UseFormRegister<any>
}

const CustomInput = ({name, register}: Props) => {
  return <input {...register(name)} />
}

const Input = withFieldContext(CustomInput);

Actual screenshot of the implementation. The "name" prop type "string" transform to union type ("email" | "password") from keyof the defined zodSchema in createForm. Furthermore, the "register" (or "control") prop will automatically be stripped in the type Props because it will be injected by "withFieldContext".

  • OtherCustomFields (If you use createGeekFormInstace)

What is ctx object ?

Basically it is the same object that the "useForm" hook returns (click here to know more) but with one addition, that is setFormConfigs. What is setFormConfigs for? ... ... ... You've guest it right! It is the function we use to pass the config behind the scene to useForm(<configs>).

createGeekFormIntance

TODO: Document the following;

  • createGeekFormInstance

Used By

This project is used by the following companies:

  • Multisys Technologies Corp.

Contributors

0.2.6

3 months ago

0.2.5

5 months ago

0.0.25

7 months ago

0.1.0

6 months ago

0.2.1

5 months ago

0.2.0

5 months ago

0.0.26

7 months ago

0.2.3

5 months ago

0.2.2

5 months ago

0.2.4

5 months ago

0.0.23

8 months ago

0.0.24

8 months ago

0.0.22

8 months ago

0.0.21

8 months ago

0.0.20

8 months ago

0.0.19

8 months ago

0.0.18

8 months ago

0.0.17

8 months ago

0.0.16

8 months ago

0.0.15

8 months ago

0.0.14

8 months ago

0.0.13

8 months ago

0.0.12

8 months ago

0.0.11

8 months ago

0.0.10

8 months ago

0.0.9

8 months ago

0.0.8

8 months ago

0.0.7

8 months ago

0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

9 months ago

0.0.3

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago