Skip to main content

Form

v.7.1.10

Installation

yarn add @mercell/form-react

First render

Reusable form solution is based on Javascript objects/records which are similar to JSON schema solution.

As an example of simple render of text field, you need provide the schema which will match our FormSchema type.

import {
FormWrapper,
FormSchema,
} from '@mercell/form-react';
import {
Button
} from '@mercell/button-react';
Live Editor
Result

Form properties and label

You can provide main label and any formProperties based on JSX.IntrinsicElements[form] type to the form.

import {
FormWrapper,
FormSchema,
} from '@mercell/form-react';
import {
Button
} from '@mercell/button-react';
Live Editor
Result

Form with initial value

Initial values are crucial for already saved forms. The most important principle is you have to match the schema structure with values structure.

import {
FormWrapper,
FormSchema,
} from '@mercell/form-react';
import {
Button
} from '@mercell/button-react';
Live Editor
Result

childrenRender vs Children

To provide more information back to the developers of what is going on with the form during render action we created a gate to access specific values/flags.

Since React Final Form is subscription based form solution, here you can find information to which form state params you can subscribe.

By default each form subscribe to those information:

submitting: true,
pristine: true,
hasValidationErrors: true,
submitFailed: true,
submitErrors: true,
submitSucceeded: true,

but you can easily overwrite or add your own setup.

In all previous examples we were using normal children property where our Button was rendered and each submit happened as it should. However this time we will use childrenRender property to show you the access to the subscribed information of the form.

When you open console, you will see information after the form was rendered, and also after you click submit.

import {
FormWrapper,
FormSchema,
} from '@mercell/form-react';
import {
Button
} from '@mercell/button-react';
Live Editor
Result

In the example below we will also track values. After each type in text field you will see new entry in console.log.

Live Editor
Result

AutoSave, Conditional render, Validations

All topics are documented in the utils section.

AutoSave information
Conditional render information
Validations information

Read only render method

This property is necessary to setup all possible readOnly type fields. What is really important to understand is that this method will be invoked on every occasion whenever you would like to render just a text regarding the values provided in form in specific fields.

As an example we will have 3 fields, one text, one checkbox, one select. And each of them we will 'convert' to the readOnly.

import {
FormWrapper,
FormSchema,
} from '@mercell/form-react';
import {
Button
} from '@mercell/button-react';
Live Editor
Result

There is also a possibility to use readOnly type with value passed directly in schema, if e.g. you don't have the value saved in initialValue property,

Live Editor
Result

Types, definitions, etc.

FormTypeProps
export type FormTypeProps = {
schema: FormSchema;
formProperties?: JSX.IntrinsicElements['form'];
formLabel?: ReactNode;
initialValue?: Record<string, any>;
onAutoSave?: (formValues: Record<string, any>) => void;
autoSaveThrottleDelay?: number;
childrenRender?: (
props: FormState<any> & FormApi & Partial<FormProps>;
) => ReactNode;
showErrorsOnInitialRender?: boolean;
validationCallback?: (
fieldValue: any,
validationSchema: AnySchema
) => string;
readOnlyFieldRender: (
readOnlyContent: any,
name: string,
restReadOnlyProperties: ReadOnlyFieldStructure,
fieldOrigin?: FieldOrigin,
elementProperties?: DetailedHTMLProps<
HTMLAttributes<HTMLElement>,
HTMLElement
>
) => ReactNode;
} & FormProps

FormProps are properties allowed by React Final Form

AnySchema is property allowed by yup library