DEV Community

Nickolay Stepanov (Nick)
Nickolay Stepanov (Nick)

Posted on

Why you need a new library for forms on React?

Hi all!
At the moment there are many libraries for forms on React.
After analysis, we found problems in most of them.
I want to talk about it and suggest our solution.

Problem: You need to use a specific syntax in your JSX.

Example 1

import { Formik, Field, Form, ErrorMessage } from 'formik';

<Formik>
    <Form>
        <label htmlFor="firstName">First Name</label>
        <Field name="firstName" type="text" />
        <ErrorMessage name="firstName" />
   </Form>
</Formik>
Enter fullscreen mode Exit fullscreen mode

Example 2

import { Form, Field } from ‘react-final-form’;
<Form>
    <Field name="bio"render={({ input, meta }) => (
        <div>
          <label>Bio</label>
         <textarea {...input} />
    {meta.touched && meta.error && <span>{meta.error}</span>}
        </div>
        )}
    />
</Form>

Enter fullscreen mode Exit fullscreen mode

What issue??

  1. If you currently have a project you need to redoing all your UI components to specific syntax.
  2. Until today a developers have millions UI libraries. Why can't you just install it and use it? You must make a wrapper components, adapt logic to use library.

We calculated time for redoing a large app.
It need about from few months to fixing bugs, changing syntax…
For a business it crazy huge.

Solution

When you need to think about a form library at first moment?
Yes, when you need a validations.
A form library only must know about data.
For example, that a data row is valid or not.
**- Not about your app structure.

  • Not about JSX or UI components inside.
  • Not about UI logic. A form system must be abstract. It's like a smart useState().**

You should make your app easy, just connect your components to data.

How it can look.

After long analyse we decided to make our library.
For creating your forms you need two simple steps:

  1. Define a scheme which describes validation and some properties of form data.
  2. Connect your scheme via methods to your UI components.

Scheme

// scheme.ts
export default {
 valid: null,
 formValue: {
    first_name: "",
    last_name: "",
 },
 rules: {
    full_name: [
        ["empty", "please enter your full name"]
    ],
    email: [
        ["empty", "please enter your email"],
        ["email", "is not email"],
    ]
 }
}
Enter fullscreen mode Exit fullscreen mode

Form

//MyForm.ts
import {useFormMod} from "formmod";

export const MyForm = () => {
const {setValue, getValue, getError, validate} = useFormMod(
    FORM_SCHEME
);
return (
<form onSubmit={handlerSubmit}>
    …
    <MyTextInput
        label={"Full name"}
        value={getValue("full_name")}
        error={getError("full_name")}
        onChange={(value: string) => setValue("full_name", value)}
    />
    …
</form>
);
Enter fullscreen mode Exit fullscreen mode

Full documentation :

https://doc.formmod.org/

**WE RECOMMEND TO USE LAPTOP OR DESKTOP DEVICE FOR READING DOCUMENTATION.

Advantages:

  • No dependencies. This is the power of simple work.This form system don’t know about your components, JSX, your app, store…You can use it with any UI components. No longer need to make wrappers components, understanding JSX syntax.Just use it with anything.
  • Easy system, easy code. It’s very simple.
  • Save time. Just connect properties to your components.

Important

We have finished our library recently.
Until today we have 151 commits, 14 releases in our repository and this is just the beginning of the work.

We started work with community, fixing documentation.

We need your support, just set a star in gitHub here:
gitHub page

Other features

Also our library can work with optional, group, composite components.
It has described work with CRUD, store (about mutable data) and more.

It is absolutely free (MIT).

We are working for world community.
We want to make development easer for everybody.

Thank you for reading!

Top comments (0)