Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

final-form-app-form

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

final-form-app-form

utils for final form using

  • 0.0.14
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Installation

npm install final-form-app-form

How to use

Simple

import { Field } from "react-final-form";
import { useForm } from "final-form-app-form";

interface IFormValues {
    firstName: string;
    lastName: string;
}

interface IPageProps {
    initialValues: IFormValues;
}

interface IPageCallProps {
    submit;
}

type Props = IPageProps & IPageCallProps;

const Page = (props: Props) => {
    const {
        initialValues,
        submit,
    } = props;

    const [ Form, submitOnClick ] = useForm<IFormValues>(submit);

    return (
        <div>
            <Form initialValues={initialValues}>
                <div>
                    <label>First Name</label>
                    <Field
                      name="firstName"
                      component="input"
                      type="text"
                    />
                </div>
                <div>
                    <label>Last Name</label>
                    <Field
                      name="lastName"
                      component="input"
                      type="text"
                    />
                </div>
            </Form>

            {/* some other components*/}

            <button onClick={submitOnClick}>
                submit form remotely
            </button>
        </div>
    );
};

With validator

import { Field } from "react-final-form";
import { useForm } from "final-form-app-form";
import { ModelState, IValidator } from "model-state-validation";

interface IFormValues {
    firstName: string;
    lastName: string;
}

class FormValuesValidator implements IValidator<IFormValues> {
    validate(model: IFormValues): ModelState {
        const modelState = new ModelState();
        if (!model.firstName) {
            modelState.addError("firstName", "required field");
        }
        if (!model.lastName) {
            modelState.addError("firstName", "required field");
        }
        return modelState;
    }
}

interface IPageProps {
    initialValues: IFormValues;
}

interface IPageCallProps {
    submit;
}

type Props = IPageProps & IPageCallProps;

const Page = (props: Props) => {
    const { 
        initialValues,
        submit,
    } = props;

    const [ Form, submitOnClick ] = useForm<IFormValues>(submit, new FormValuesValidator());

    return (
        <div>
            <Form initialValues={initialValues}>
                <div>
                    <label>First Name</label>
                    <Field name="firstName" >
                        {({ input, meta }) => (
                          <>
                            <input {...input} type="text" />
                            {meta.error && meta.touched && <span>{meta.error}</span>}
                          </>
                        )}
                    </Field>
                </div>
                <div>
                    <label>Last Name</label>
                    <Field name="lastName" >
                        {({ input, meta }) => (
                          <>
                            <input {...input} type="text" />
                            {meta.error && meta.touched && <span>{meta.error}</span>}
                          </>
                        )}
                    </Field>
                </div>
            </Form>

            {/* some other components*/}

            <button onClick={submitOnClick}>
                submit form remotely
            </button>
        </div>
    );
};

Using of FormState

import { FormState } from "final-form";
import { Field } from "react-final-form";
import { useForm } from "final-form-app-form";

interface IFormValues {
    firstName: string;
    lastName: string;
}

interface IPageProps {
    initialValues: IFormValues;
}

interface IPageCallProps {
    submit;
}

type Props = IPageProps & IPageCallProps;

const Page = (props: Props) => {
    const {
        initialValues,
        submit,
    } = props;

    const [ Form, submitOnClick ] = useForm<IFormValues>(submit);

    return (
        <div>
            <Form initialValues={initialValues} subscribe={{ pristine: true }}>
                {(state: FormState) => (
                    <>
                        <div>
                            <label>First Name</label>
                            <Field
                              name="firstName"
                              component="input"
                              type="text"
                            />
                        </div>
                        <div>
                            <label>Last Name</label>
                            <Field
                              name="lastName"
                              component="input"
                              type="text"
                            />
                        </div>
                        <p>
                            {state.pristine ? "form is pristine" : "form is dirty"}
                        </p>
                    </>
                )}
            </Form>

            {/* some other components*/}

            <button onClick={submitOnClick}>
                submit form remotely
            </button>
        </div>
    );
};

Additional settings

import { useState } from "react";
import { useForm } from "final-form-app-form";
import { ModelState, IValidator } from "model-state-validation";

interface IFormValues {
    firstName: string;
    lastName: string;
}

class FormValuesValidator implements IValidator<IFormValues> {
    validate(model: IFormValues): ModelState {
        // validation logic
        // ...
    }
}


const Page = (props) => {
    const {
        initialValues,
        submit,
    } = props;

    const [ validationMessage, setValidationMessage ] = useState("");

    const [ Form, submitOnClick ] = useForm<IFormValues>(
        submit,
        new FormValuesValidator(),
        { 
            setCommonErrorMessage: setValidationMessage,
            validateOnFieldsChange: true,
            resetValidationErrorOnActiveField: true,
            validateOnFieldsBlur: true,
            // validateSpecifiedFieldsOnBlur: [ "firstName" ],
        }
    );

    return (
        <div>
            // render form etc
            // ...

            <p style={{ color: "red" }}>
                {validationMessage}
            </p>
        </div>
    );
};

FAQs

Package last updated on 03 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc