@react-typed-forms/core
TypeScript icon, indicating that this package has built-in type declarations

3.5.1 • Public • Published

npm

See the documentation here

Install

npm install @react-typed-forms/core
import { Finput, notEmpty, useControl } from "@react-typed-forms/core";
import React, { useState } from "react";

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

export default function SimpleExample() {
  const formState = useControl(
    { firstName: "", lastName: "" },
    { fields: { lastName: { validator: notEmpty("Required field") } } }
  );
  const fields = formState.fields;
  const [formData, setFormData] = useState<SimpleForm>();
  return (
    <form
      onSubmit={(e) => {
        e.preventDefault();
        setFormData(formState.current.value);
      }}
    >
      <label>First Name</label>
      <Finput id="firstName" type="text" state={fields.firstName} />
      <label>Last Name *</label>
      <Finput id="lastName" type="text" state={fields.lastName} />
      <div>
        <button id="submit">Validate and toObject()</button>
      </div>
      {formData && (
        <pre className="my-2">{JSON.stringify(formData, undefined, 2)}</pre>
      )}
    </form>
  );
}

Readme

Keywords

Package Sidebar

Install

npm i @react-typed-forms/core

Weekly Downloads

157

Version

3.5.1

License

ISC

Unpacked Size

390 kB

Total Files

31

Last publish

Collaborators

  • doolse