Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

README.md 5.7 KiB

il y a 3 ans
il y a 3 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <p align="center" style="text-align: center !important; line-height: 1 !important; border: 0 !important;">
  2. <h1 align="center">
  3. <img src="https://raw.githubusercontent.com/TheoryOfNekomata/formxtra/master/docs/assets/formxtra.svg" alt="formxtra"/>
  4. </h1>
  5. <h2 align="center">
  6. The companion for Web forms!
  7. </h2>
  8. <div align="center">
  9. <b>
  10. Extract and set form values through the DOM&mdash;no frameworks required!
  11. </b>
  12. </div>
  13. <div align="center">
  14. Lightweight. Simple. It Just Works.
  15. </div>
  16. </p>
  17. ## Installation and Sources
  18. The package can be found on:
  19. - [Modal Pack](https://js.pack.modal.sh)
  20. - [npm](https://npmjs.com/package/@theoryofnekomata/formxtra)
  21. - [GitHub Package Registry](https://github.com/TheoryOfNekomata/formxtra/packages/793279)
  22. The package sources can be found on the [main Modal Code repository](https://code.modal.sh/TheoryOfNekomata/formxtra)
  23. with an [active GitHub mirror](https://github.com/TheoryOfNekomata/formxtra).
  24. ## Usage
  25. 1. Lay out your input elements (all valid input types supported including `<select>` and `<textarea>`) then bind them
  26. to a form:
  27. * Put them inside a `<form>`.
  28. * Alternatively, use the `form=""` attribute then specify the form `id` where they will be bound.
  29. 2. Add `name=""` attributes to your input elements.
  30. 3. Get your `<form>` element:
  31. * Query the form directly.
  32. * If you want to retrieve/set the form values through an individual input element (e.g. in the case of value change
  33. events like `onchange`), use the `inputElement.form` attribute.
  34. 4. Use `getFormValues()` to retrieve all bound input elements' values, or `setFormValues()` to set them (setting only
  35. some fields' values is supported).
  36. ### Example
  37. Interactive code samples can be found on Codepen:
  38. * [Vanilla JS usage](https://codepen.io/theoryofnekomata/pen/xxajmvJ)
  39. * [React integration](https://codepen.io/theoryofnekomata/pen/RwYyvZN)
  40. * [Vue integration](https://codepen.io/theoryofnekomata/pen/gOdzqzM)
  41. * [Solid integration](https://codepen.io/theoryofnekomata/pen/QWVrYem)
  42. For an example form:
  43. ```html
  44. <form id="loginForm" aria-label="Login Form">
  45. <button id="autofill" type="button">
  46. Autofill login form (username: admin, remember: true)
  47. </button>
  48. <hr />
  49. <fieldset>
  50. <legend>
  51. Login
  52. </legend>
  53. <div>
  54. <input type="text" name="username" placeholder="Username" />
  55. </div>
  56. <div>
  57. <input type="password" name="password" placeholder="Password" />
  58. </div>
  59. <div>
  60. <button type="submit" name="type" value="client">
  61. Log In As Client
  62. </button>
  63. <button type="submit" name="type" value="admin">
  64. Log In As Admin
  65. </button>
  66. </div>
  67. </fieldset>
  68. </form>
  69. <!-- Input elements can be placed outside the form element they are bound to. -->
  70. <label>
  71. <input type="checkbox" name="remember" form="loginForm" />
  72. Remember my login credentials
  73. </label>
  74. ```
  75. Use the library as follows (code is in TypeScript, but can work with JavaScript as well):
  76. ```typescript
  77. import { getFormValues, setFormValues } from '@theoryofnekomata/formxtra';
  78. const form: HTMLFormElement = document.getElementById('loginForm');
  79. const processResponse = async (response: Response) => {
  80. const result = await response.json();
  81. alert(`Welcome ${result.user}!`);
  82. };
  83. // Use formxtra in event handlers
  84. form.addEventListener('submit', async e => {
  85. const {
  86. currentTarget: thisForm,
  87. submitter,
  88. } = e;
  89. e.preventDefault();
  90. const values = getFormValues(thisForm, { submitter });
  91. // Get the form values and send as request to some API
  92. const response = await fetch(
  93. 'https://example.com/api/log-in',
  94. {
  95. method: 'POST',
  96. body: JSON.stringify(values),
  97. headers: {
  98. 'Content-Type': 'application/json',
  99. },
  100. },
  101. );
  102. if (response.ok) {
  103. processResponse(response);
  104. return;
  105. }
  106. alert('Invalid login!');
  107. });
  108. // You can use fomrxtra directly with elements as long as they are bound to a form.
  109. const autofillButton = document.getElementById('autofill');
  110. autofillButton.addEventListener('click', e => {
  111. setFormValues(e.currentTarget.form, { username: 'admin', remember: true });
  112. });
  113. ```
  114. ### API
  115. These are all the exported methods in the library:
  116. ```typescript
  117. import {
  118. getFormValues,
  119. setFormValues,
  120. clearFormValues,
  121. getValue,
  122. isElementValueIncludedInFormSubmit,
  123. isFieldElement,
  124. } from '@theoryofnekomata/formxtra';
  125. ```
  126. One would usually need only the `getFormValues()` and `setFormValues()` functions, however if the utility functions are
  127. needed, the proper usages are documented via TSDoc comments.
  128. ## Additional Information
  129. The library has been tested on the static DOM using JSDOM, and the real dynamic DOM using Cypress. This is to guarantee
  130. compatibility across environments.
  131. See the [documentation folder](https://code.modal.sh/TheoryOfNekomata/formxtra/src/branch/master/docs) for more details
  132. on this library.
  133. You may need the [event-submitter-polyfill package](https://github.com/idea2app/event-submitter-polyfill) for browsers
  134. that do not support the submitter property on submit events. The submitter element is needed for
  135. tracking which button triggered a form submission, and it may contain `name` and `value` attributes which are used to
  136. add values to the form (such as specifying which action to take for the rest of the form values). Refer to the
  137. [usability table for `SubmitEvent.submitter`](https://caniuse.com/mdn-api_submitevent_submitter) to check if your target
  138. browser is supported.
  139. There is no support for `<input type="image">` elements. It is recommended to handle the XY coordinates tracking
  140. separately as mouse events when using said elements as submitters.
  141. The sources are under the [MIT license](https://code.modal.sh/TheoryOfNekomata/formxtra/raw/branch/master/LICENSE).