Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Rationale
  2. **Why the need for another form library?**
  3. Let us set some facts about existing form libraries:
  4. * Form libraries, especially framework-specific ones have verbose syntax.
  5. * Verbose, as in dealing with terms that are unique to its respective form library API.
  6. * Please go to your form library of choice and compare the syntax yourself.
  7. * Form libraries are dependent on the architecture of the framework (such as [React Hook Form](https://www.npmjs.com/package/react-hook-form) and [Formik](https://www.npmjs.com/package/formik)) and
  8. requires using their features.
  9. * Form libraries have differing ways on how to manage state, usually piggybacking on the data flow of the framework they
  10. are dependent on.
  11. * Form libraries are relatively complex to what they are supposed to be doing such as providing wrappers to custom
  12. components, in which compatibility should be in the hands of the component author.
  13. **What does `formxtra` offer?**
  14. * `formxtra` aims to simplify the syntax to just invoking two functions - `getFormValues()` and `setFormValues()`.
  15. * `formxtra` is not dependent to any library and is highly interoperable.
  16. * `formxtra` is dependent only to the DOM. Since accessing the DOM is fully synchronous, the same can be said for the
  17. entire operation of `formxtra`.
  18. * There are no other paradigms introduced by `formxtra`. For the library to do its intentions, it only requires
  19. respecting the HTML DOM spec, such as providing names to inputs and binding them correctly to forms, which is what
  20. all (data-driven) websites should do anyway.
  21. * `formxtra` is lightweight, even smaller than [React Hook Form](https://www.npmjs.com/package/react-hook-form) and [Formik](https://www.npmjs.com/package/formik).
  22. * `formxtra` is already type-safe, being written in TypeScript and providing types, thanks to [pridepack](https://www.npmjs.com/package/pridepack) as a scaffold.
  23. * `formxtra` is guaranteed to work with all major browsers (however `formxtra` is not yet tested with older browsers.
  24. If you want to contribute for support on an older browser, a PR would be appreciated).
  25. **What does `formxtra` not offer?**
  26. * `formxtra` is neither a validation library, nor does it provide utility functions for validation.
  27. * However, one could use `formxtra` in tandem with other validation libraries
  28. such as `ajv` or `yup` for instance, by validating the values returned by `getFormValues()`.
  29. * `formxtra` does not provide out-of-the-box compatibility with custom components.
  30. * Because custom components have different
  31. implementations that mostly favor user experience over compliance, `formxtra` does not guarantee it can work with them
  32. out of the box.
  33. * However, the solution for this is to provide a corresponding `<input type="hidden">` element for each custom\
  34. component, which should always get the latter's serializable value. In return, this can also simplify the submission
  35. of the form as the custom components' values are already in the form.
  36. ---
  37. Forms are used to package related data, typically sent to an external location or processed internally. In the browser,
  38. the default behavior of submitting form data is not always preferred, as this is done through loading or reloading a
  39. document as soon as the form is submitted. In addition, [applications have limited control over how the data are
  40. formatted on submission](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-fs-enctype) with
  41. this approach. This is why the new way of sending form data is done through AJAX/fetch requests, wherein the data are
  42. serialized into formats like JSON. To turn form data into a specific format requires access to the elements holding the
  43. values to each field in the form.
  44. Libraries made for extracting form values query field elements in the DOM, which is inefficient since they need to
  45. traverse the DOM tree in some way, using methods such as `document.getElementsByTagName()` and
  46. `document.querySelector()`. This is the same case with setting each form values for, say, prefilling values to save
  47. time. It might be a simple improvement to the user experience, but the logic behind can be unwieldy as there may be
  48. inconsistencies in setting up each field value depending on the form library being used.
  49. Upon retrieving the field values somehow, some libraries attempt to duplicate the values of the fields as they change,
  50. for instance by attaching event listeners and storing the new values into some internal store such as an object or a map.
  51. This is then retrieved by some other exposed function or mechanism within that library. This is common with reactive
  52. frameworks, where changes to the document are essential to establish functionality and improved user experience.
  53. This is why `formxtra` was made, in order to show a very simplistic and straightforward relation between the form as
  54. interacted with the user, and the state of the form controls as it is manifested in the DOM.