项目作者: react-html-form

项目描述 :
Friendly form library for React ✌️
高级语言: JavaScript
项目地址: git://github.com/react-html-form/react-html-form.git
创建时间: 2018-09-14T00:51:37Z
项目社区:https://github.com/react-html-form/react-html-form

开源协议:MIT License

下载


" class="reference-link">react-html-form

The simplest form component for React

Notice: This project is in beta. I’m seeking beta testers and co-authors/maintainers.

Immediate roadmap: feedback/bug reports from the community, documentation, unit tests.

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 and Constraint Validation API.

Install

  1. $ npm install react-html-form@beta --save
  2. $ # or
  3. $ yarn add react-html-form@beta

Usage

See https://codesandbox.io/embed/x70lxkzkvo?hidenavigation=1&view=split for a full “KitchenSink” demo.

  1. import Form from "react-html-form";
  2. import React from "react";
  3. class MyPage extends React.Component {
  4. handleSubmit(event, formState, formReference) {
  5. // formState = {
  6. // values: {
  7. // usersName: 'demo',
  8. // usersEmail: 'demo@example.com',
  9. // },
  10. // errors: {},
  11. // dirty: {
  12. // usersName: true,
  13. // usersEmail: true,
  14. // },
  15. // touched: {
  16. // usersName: true,
  17. // usersEmail: true,
  18. // },
  19. // blurred: {
  20. // usersName: true,
  21. // usersEmail: true,
  22. // },
  23. // isDirty: true,
  24. // isValid: true,
  25. // isValidating: false,
  26. // submitCount: 1
  27. // }
  28. yourHttpClient.post("http://api.example.com/", formState.values);
  29. }
  30. render() {
  31. return (
  32. <React.Fragment>
  33. <Form
  34. // include `WithData` to any form event handler to get the
  35. // form state included for free as the second argument.
  36. // The third argument is a reference to the form itself
  37. // You can still use the standard `onSubmit` if you please
  38. onSubmitWithData={this.handleSubmit}
  39. >
  40. <label>Name:</label>
  41. <input
  42. required
  43. data-errormessage="Name can only include letters"
  44. pattern="A-Za-z+"
  45. name="usersName"
  46. type="text"
  47. />
  48. <br />
  49. <label>Email</label>
  50. <input required name="email" type="email" />
  51. <br />
  52. <button type="submit">Submit</button>
  53. </Form>
  54. </React.Fragment>
  55. );
  56. }
  57. }

Credits

  • Logomark via align by Chameleon Design from the Noun Project

Footnotes:

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

License

MIT