Git Product home page Git Product logo

reform's Introduction

ReForm.re

All Contributors

Reasonably making forms sound good

Docs

Check our Docusaurus https://astrocoders.dev/reform

Installation

yarn add bs-reform reschema

Then add it to bsconfig.json

"bs-dependencies": [
 "bs-reform",
 "reschema"
]

Then add lenses-ppx

yarn add [email protected] -D

And update your bsconfig.json with ppx-flags

"ppx-flags": [
 "lenses-ppx/ppx"
]

Features

  • Hook API
  • Schema API
  • Type safe, handleChange properly infers the value of the field it is handling
  • Context Provider
  • Field component
  • Validation strategy, OnDemand and OnChange

What this is and why

Code that deals with strongly typed forms can quickly become walls of repeated text. We created ReForm to be both deadly simple and to make forms sound good leveraging ReasonML's powerful typesytem. Even the schemas we use are nothing more than constructors built-in in the language itself with a small size footprint.

Basic usage

Checkout https://github.com/Astrocoders/reform/blob/master/packages/demo/src/PostAddNext.re for a more complete demo

open BsReform;

module StateLenses = [%lenses
  type state = {
    description: string,
    title: string,
    acceptTerms: bool,
  }
];
module PostAddForm = ReForm.Make(StateLenses);

module FieldString = {
  [@react.component]
  let make = (~field, ~label) => {
    <PostAddForm.Field
      field
      render={({handleChange, error, value, validate}) =>
        <label>
          <span> {React.string(label)} </span>
          <input
            value
            onChange={Helpers.handleChange(handleChange)}
            onBlur={_ => validate()}
          />
          <p> {error->Belt.Option.getWithDefault("")->React.string} </p>
        </label>
      }
    />;
  };
};

[@react.component]
let make = () => {
  let reform =
    PostAddForm.use(
      ~validationStrategy=OnDemand,
      ~schema={
        PostAddForm.Validation.Schema([|
          StringMin(Title, 20),
          StringNonEmpty(Description),
          Custom(
            AcceptTerms,
            values =>
              values.acceptTerms == false
                ? Error("You must accept all the terms") : Valid,
          )
        |]);
      },
      ~onSubmit=
        ({state}) => {
          Js.log2("title", state.values.description);
          Js.log2("description", state.values.description);
          Js.log2("acceptTerms", state.values.description);
          None;
        },
      ~initialState={title: "", description: "", acceptTerms: false},
      (),
    );

  <PostAddForm.Provider value=reform>
    <form
      onSubmit={event => {
        ReactEvent.Synthetic.preventDefault(event);
        reform.submit();
      }}>
      <FieldString field=StateLenses.Title label="Title" />
      <FieldString field=StateLenses.Description label="Description" />
      <PostAddForm.Field
        field=StateLenses.AcceptTerms
        render={({handleChange, error, value}) =>
          <label>
            <p>
              <span> {"Accept terms? " |> React.string} </span>
              <input
                type_="checkbox"
                value={string_of_bool(value)}
                onChange={event =>
                  ReactEvent.Form.target(event)##checked |> handleChange
                }
              />
            </p>
            <p> {error->Belt.Option.getWithDefault("")->React.string} </p>
          </label>
        }
      />
      {reform.state.formState == Submitting
         ? <p> {React.string("Saving...")} </p>
         : <button type_="submit"> {"Submit" |> React.string} </button>}
    </form>
  </PostAddForm.Provider>;
};

Alternatives

Publishing

lerna version major|patch|minor

and then

lerna publish from-git

Support

We usually hang out at https://discord.gg/reasonml or https://reasonml.chat so feel free to ask anything there.

Contributors โœจ

Thanks goes to these wonderful people (emoji key):


Thomas Deconinck

๐Ÿ“– ๐Ÿ’ป

Gabriel Rubens

๐Ÿ’ป ๐Ÿ› ๐Ÿค” ๐Ÿ–‹ ๐Ÿ“–

Lucas Besen

๐Ÿšง

Jefferson Carvalho

๐Ÿ’ป

Luiz Augusto Moratelli

๐Ÿ’ป

amythos

๐Ÿ’ป

Lalli Nuorteva

๐Ÿ›

Matt

๐Ÿšง

Kyle Davis

๐Ÿ‘€

Ulugbek Abdullaev

๐Ÿ›

Khoa Nguyen

๐Ÿ’ป

Medson Oliveira

๐Ÿ’ป ๐Ÿค”

Ana Luiza Portello Bastos

๐Ÿ“–

This project follows the all-contributors specification. Contributions of any kind welcome!

reform's People

Contributors

fakenickels avatar luizmoratelli avatar allcontributors[bot] avatar medson10 avatar jeffersoncarvalh0 avatar dependabot[bot] avatar happylinks avatar blocknomad avatar thangngoc89 avatar amythos avatar hew avatar lucasbesen avatar anabastos avatar eliabejr avatar dckt avatar marcelcutts avatar yatesco avatar glennsl avatar guilhermedecampo avatar lalnuo avatar ulugbekna avatar maarekj avatar a-gierczak avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.