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 3.5 KiB

3 anos atrás
3 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <header style="text-align: center; line-height: 1">
  2. <h1 style="margin: 0">
  3. <img src="./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. <button id="autofill" type="button">
  36. Autofill login form
  37. </button>
  38. <form id="loginForm">
  39. <input type="text" name="username" />
  40. <input type="password" name="password" />
  41. <button type="submit" name="type" value="client">
  42. Log In As Client
  43. </button>
  44. <button type="submit" name="type" value="admin">
  45. Log In As Admin
  46. </button>
  47. </form>
  48. <label>
  49. <input type="checkbox" name="remember" form="loginForm" />
  50. Remember my login credentials
  51. </label>
  52. ```
  53. Use the library as follows (code is in TypeScript, but can work with JavaScript as well):
  54. ```typescript
  55. import { getFormValues, setFormValues } from '@theoryofnekomata/formxtra';
  56. // This is the only query we need. On libraries like React, we can easily get form elements when we
  57. // attach submit event listeners.
  58. const form: HTMLFormElement = document.getElementById('form');
  59. const processResponse = async (response: Response) => {
  60. const result = await response.json();
  61. alert(`Welcome ${result.user}!`);
  62. };
  63. // Best use case is with event handlers
  64. form.addEventListener('submit', async e => {
  65. const {
  66. currentTarget: thisForm,
  67. submitter,
  68. } = e;
  69. e.preventDefault();
  70. const values = getFormValues(thisForm, { submitter });
  71. // Get the form values and send as request to some API
  72. const response = await fetch(
  73. 'https://example.com/api/log-in',
  74. {
  75. method: 'POST',
  76. body: JSON.stringify(values),
  77. headers: {
  78. 'Content-Type': 'application/json',
  79. },
  80. },
  81. );
  82. if (response.ok) {
  83. processResponse(response);
  84. }
  85. });
  86. const autofillButton = document.getElementById('autofill');
  87. autofillButton.addEventListener('click', e => {
  88. setFormValues(form, { username: 'admin', remember: true });
  89. });
  90. ```
  91. There are utility functions exported alongside `getFormValues()` and `setFormValues()`. You may want to use namespace
  92. import with this library, i.e. `import * as formxtra from '@theoryofnekomata/formxtra'`.
  93. See the [documentation folder](./docs) for more details.
  94. ## Tests
  95. The library has been tested on the static DOM using JSDOM, and the real dynamic DOM using Cypress.
  96. ## License
  97. [MIT](./LICENSE)