Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

README.md 4.0 KiB

3 anos atrás
3 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <header style="text-align: center; line-height: 1">
  2. <h1 style="margin: 0">
  3. <img src="https://raw.githubusercontent.com/TheoryOfNekomata/formxtra/master/docs/assets/formxtra.svg" alt="formxtra"/>
  4. </h1>
  5. <h2 style="margin: 0">
  6. The companion for Web forms!
  7. </h2>
  8. <p style="font-size: 125%">
  9. Extract and set form values through the DOM&mdash;no frameworks required!
  10. </p>
  11. <p>
  12. Lightweight. Simple. It Just Works.
  13. </p>
  14. </header>
  15. ## Installation
  16. The package can be found on:
  17. - [Modal Pack](https://js.pack.modal.sh)
  18. - [npm](https://npmjs.com/package/@theoryofnekomata/formxtra)
  19. - [GitHub Package Registry](https://github.com/TheoryOfNekomata/formxtra/packages/793279)
  20. ## Usage
  21. 1. Lay out your input elements (all valid input types supported including `<select>` and `<textarea>`) then bind them
  22. to a form:
  23. * Put them inside a `<form>`.
  24. * Alternatively, use the `form=""` attribute then specify the form `id` where they will be bound.
  25. 2. Add `name=""` attributes to your input elements.
  26. 3. Get your `<form>` element:
  27. * Query the form directly.
  28. * If you want to retrieve/set the form values through an individual input element (e.g. in the case of value change
  29. events like `onchange`), use the `inputElement.form` attribute.
  30. 4. Use `getFormValues()` to retrieve all bound input elements' values, or `setFormValues()` to set them (setting only
  31. some fields' values is supported).
  32. ### Example
  33. For an example form:
  34. ```html
  35. <form id="loginForm" aria-label="Login Form">
  36. <button id="autofill" type="button">
  37. Autofill login form (username: admin)
  38. </button>
  39. <hr />
  40. <fieldset>
  41. <legend>
  42. Login
  43. </legend>
  44. <div>
  45. <input type="text" name="username" placeholder="Username" />
  46. </div>
  47. <div>
  48. <input type="password" name="password" placeholder="Password" />
  49. </div>
  50. <div>
  51. <button type="submit" name="type" value="client">
  52. Log In As Client
  53. </button>
  54. <button type="submit" name="type" value="admin">
  55. Log In As Admin
  56. </button>
  57. </div>
  58. </fieldset>
  59. </form>
  60. <!-- Input elements can be placed outside the form element they are bound to. -->
  61. <label>
  62. <input type="checkbox" name="remember" form="loginForm" />
  63. Remember my login credentials
  64. </label>
  65. ```
  66. Use the library as follows (code is in TypeScript, but can work with JavaScript as well):
  67. ```typescript
  68. import { getFormValues, setFormValues } from '@theoryofnekomata/formxtra';
  69. // This is the only query we need. On libraries like React, we can easily get form elements when we
  70. // attach submit event listeners.
  71. const form: HTMLFormElement = document.getElementById('form');
  72. const processResponse = async (response: Response) => {
  73. const result = await response.json();
  74. alert(`Welcome ${result.user}!`);
  75. };
  76. // Best use case is with event handlers
  77. form.addEventListener('submit', async e => {
  78. const {
  79. currentTarget: thisForm,
  80. submitter,
  81. } = e;
  82. e.preventDefault();
  83. const values = getFormValues(thisForm, { submitter });
  84. // Get the form values and send as request to some API
  85. const response = await fetch(
  86. 'https://example.com/api/log-in',
  87. {
  88. method: 'POST',
  89. body: JSON.stringify(values),
  90. headers: {
  91. 'Content-Type': 'application/json',
  92. },
  93. },
  94. );
  95. if (response.ok) {
  96. processResponse(response);
  97. }
  98. });
  99. // You can use fomrxtra directly with elements as long as they are bound to a form.
  100. const autofillButton = document.getElementById('autofill');
  101. autofillButton.addEventListener('click', e => {
  102. setFormValues(e.currentTarget.form, { username: 'admin', remember: true });
  103. });
  104. ```
  105. There are utility functions exported alongside `getFormValues()` and `setFormValues()`. You may want to use namespace
  106. import with this library, i.e. `import * as formxtra from '@theoryofnekomata/formxtra'`.
  107. See the [documentation folder](./docs) for more details.
  108. ## Tests
  109. The library has been tested on the static DOM using JSDOM, and the real dynamic DOM using Cypress.
  110. ## License
  111. [MIT](./LICENSE)