@telenko/react-form
TypeScript icon, indicating that this package has built-in type declarations

0.0.3-beta • Public • Published

react-form

Simple set of hooks to organize form validation (analog to formik's useFormik)

why?

Much more simple and efficient tiny package

plans

Work in progress, current version is beta docs ..tbd

example

type PersonForm = { name: string; age: number };

const SomeForm: React.FC = () => {
  const yupValidator = useMemo(() => {
    yup.object().shape({
      name: yup.string().required(),
      age: yup.string().optional(),
    });
  }, []);
  const [validator] = useYupSyncValidator<PersonForm>(yupValidator);
  const form = useForm<PersonForm>(
    {           // default values
      name: "",
      age: 0,
    },
    validator,  // validator instance
    (person) => {
                // on submit
      save(person);
    }
  );
  return (
    <form onSubmit={form.submit} novalidate>
      <TextField
        value={form.values.name}
        onChange={form.handleEventChange}
        error={form.needHighlight("name")}
        helperText={form.needHighlight("name") && form.errors.name}
        {...someProps}
      />
      <TextField
        type="number"
        value={form.values.age}
        onChange={form.handleEventChange}
        error={form.needHighlight("age")}
        helperText={form.needHighlight("age") && form.errors.age}
        {...someProps}
      />
      <input type="submit" />
    </form>
  );
};

Package Sidebar

Install

npm i @telenko/react-form

Weekly Downloads

1

Version

0.0.3-beta

License

MIT

Unpacked Size

9.3 kB

Total Files

5

Last publish

Collaborators

  • andrii.telenko