0.0.0 • Published 6 years ago

react-html-form v0.0.0

Weekly downloads
53
License
-
Repository
-
Last release
6 years ago

The simplest form component for React

Overview

"HTML form elements work a little bit differently from other DOM elements in React, because form elements naturally keep some internal state."1 What this means, unfortunately, is that managing form state in React is not an easy feat.

Our mission is to build a great developer experience around forms. We let you manage forms with a straightforward API by embracing the fact that forms keep internal state, not fighting against it.

Problems with current form libraries

Other React form libraries introduce patterns that aren’t very ergonomic or have a large API surface. They often require the user to build their own input primitives that are tightly coupled to the library. Even more, they usually require the user to bring their own validation, something the browser actually offers for free!

How react-html-form is different

We keep our API surface small by pulling form state (values and errors) directly out of the DOM through the HTMLFormElement interface.

Install

$ npm install react-html-form --save
$ # or
$ yarn add react-html-form

Usage

import Form from "react-html-form";
import React from "react";

class MyPage extends React.Component {
  handleSubmit(event, formState) {
    // formState = {
    //   values: {
    //     usersName: 'demo',
    //     usersEmail: 'demo@example.com',
    //   },
    //   errors: {},
    //   dirty: {
    //     usersName: true,
    //     usersEmail: true,
    //   },
    //   touched: {
    //     usersName: true,
    //     usersEmail: true,
    //   },
    //   blurred: {
    //     usersName: true,
    //     usersEmail: true,
    //   },
    //   isDirty: true,
    //   isValid: true,
    //   isValidating: false,
    //   submitCount: 1
    // }
    yourHttpClient.post("http://api.example.com/", formState.values);
  }

  render() {
    return (
      <React.Fragment>
        <Form onSubmitWithData={this.handleSubmit}>
          <label>Name:</label>
          <input
            required
            data-errormessage="Name can only include letters"
            pattern="A-Za-z+"
            name="usersName"
            type="text"
          />
          <br />
          <label>Email</label>
          <input required name="email" type="email" />
          <br />
          <button type="submit">Submit</button>
        </Form>
      </React.Fragment>
    );
  }
}

Credits

  • Logomark via align by Chameleon Design from the Noun Project

Footnotes:

1: https://reactjs.org/docs/forms.html

License

MIT

0.0.0-beta.11

6 years ago

0.0.0-beta.10

6 years ago

0.0.0-beta.9

6 years ago

0.0.0-beta.8

6 years ago

0.0.0-beta.7

6 years ago

0.0.0-beta.6

6 years ago

0.0.0-beta.5

6 years ago

0.0.0-beta.4

6 years ago

0.0.0-beta.3

6 years ago

0.0.0-beta.2

6 years ago

0.0.0-beta.1

6 years ago

0.0.0-beta.0

6 years ago

0.0.0

6 years ago