0.3.2 • Published 2 years ago

form-react-form v0.3.2

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

size npm version npm downloads license

Install

npm install form-react-form

or

yarn add form-react-form

Usage

import React, { FC } from "react";
import { useForm } from "form-react-form";

import schema from "./schema";

export interface FormInput {
  email: string;
  password: string;
}

const Form: FC = () => {
  const { values, errors, onChange, wrapSubmit } = useForm<FormInput>({
    schema,
    defaults: {
      email: "",
      password: "",
    },
  });

  const onSubmit = wrapSubmit(async (values) => {});

  return (
    <form onSubmit={onSubmit}>
      <input name="email" placeholder="Email" value={values.email} onChange={onChange} />
      {errors.email && <span>{errors.email[0]}</span>}

      <input
        name="password"
        type="password"
        placeholder="Password"
        value={values.password}
        onChange={onChange}
      />
      {errors.password && <span>{errors.password[0]}</span>}

      <button>Login</button>
    </form>
  );
};

Using fastest-validator under the hood for schema validation.

export default {
  email: {
    type: "string",
    email: true,
    empty: false,
    min: 2,
    max: 255,
  },
  password: {
    type: "string",
    empty: false,
    min: 8,
    max: 64,
  },
};

API

const { values, errors, setValues, setErrors, onChange, wrapChange, wrapSubmit } = useForm(options);

Return

NameInput/OutputDescription
values{ name: value }Object containing values of each input by name.
errors{ name: string[] }Object containing errors of each input by name.
onChange(event) => void (value, name) => void ({ [name]: value }) => voidFunction that handles input changes.
wrapChange(onChange) => voidWrapper function to have custom control over input change before value gets passed to useForm for handling.
wrapSubmit((values) => void) => voidWrapper function to have custom control over input change before value gets passed to useForm for handling.
setValues({ [name]: value }) => voidFunction to update values.
setErrors({ [name]: string[] }) => voidFunction to update errors. Commonly used in wrapSubmit to set errors sent from server.

Options

  • defaults?: Object containing input defualt values. When not provided, initial value for each input will be undefined sometimes making inputs uncontrolled. To avoid that, it is recomended to set defaults, defaultValue on html inputs or set default value paramater on custom components.
  • schema: Valid schema for fastest-validator.

Typescript

When using forms with different input and output values you should also provide FormOutput types because fastest-validator unfortunately can't convert them using schema.

// ...

export interface FormInput {
  age: string;
}

export interface FormOutput {
  age: number;
}

const Form: FC = () => {
  const { values, onChange, wrapSubmit } = useForm<FormInput, FormOutput>({
    schema,
    defaults: {
      age: "",
    },
  });

  const onSubmit = wrapSubmit(async (values) => {});

  return (
    <form onSubmit={onSubmit}>
      <input name="age" placeholder="Age" value={values.age} onChange={onChange} />

      <button>Set</button>
    </form>
  );
};
0.3.2

2 years ago

0.3.1

3 years ago

0.3.0

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago