Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

README.md 4.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <header align="center" style="text-align: center !important; line-height: 1 !important; border: 0 !important;">
  2. <h1 style="margin: 0 !important;">
  3. <img src="https://raw.githubusercontent.com/TheoryOfNekomata/formxtra/master/docs/assets/formxtra.svg" alt="formxtra"/>
  4. </h1>
  5. <h2 style="margin: 0 !important;">
  6. The companion for Web forms!
  7. </h2>
  8. <p style="font-size: 125% !important">
  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 and Sources
  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. The package sources can be found on the [main Modal Code repository](https://code.modal.sh/TheoryOfNekomata/formxtra)
  21. with an [active GitHub mirror](https://github.com/TheoryOfNekomata/formxtra).
  22. ## Usage
  23. 1. Lay out your input elements (all valid input types supported including `<select>` and `<textarea>`) then bind them
  24. to a form:
  25. * Put them inside a `<form>`.
  26. * Alternatively, use the `form=""` attribute then specify the form `id` where they will be bound.
  27. 2. Add `name=""` attributes to your input elements.
  28. 3. Get your `<form>` element:
  29. * Query the form directly.
  30. * If you want to retrieve/set the form values through an individual input element (e.g. in the case of value change
  31. events like `onchange`), use the `inputElement.form` attribute.
  32. 4. Use `getFormValues()` to retrieve all bound input elements' values, or `setFormValues()` to set them (setting only
  33. some fields' values is supported).
  34. ### Example
  35. Interactive code samples can be found on Codepen:
  36. * [Vanilla JS usage](https://codepen.io/theoryofnekomata/pen/xxajmvJ)
  37. * [React integration](https://codepen.io/theoryofnekomata/pen/RwYyvZN)
  38. * [Vue integration](https://codepen.io/theoryofnekomata/pen/gOdzqzM)
  39. * [Solid integration](https://codepen.io/theoryofnekomata/pen/QWVrYem)
  40. For an example form:
  41. ```html
  42. <form id="loginForm" aria-label="Login Form">
  43. <button id="autofill" type="button">
  44. Autofill login form (username: admin, remember: true)
  45. </button>
  46. <hr />
  47. <fieldset>
  48. <legend>
  49. Login
  50. </legend>
  51. <div>
  52. <input type="text" name="username" placeholder="Username" />
  53. </div>
  54. <div>
  55. <input type="password" name="password" placeholder="Password" />
  56. </div>
  57. <div>
  58. <button type="submit" name="type" value="client">
  59. Log In As Client
  60. </button>
  61. <button type="submit" name="type" value="admin">
  62. Log In As Admin
  63. </button>
  64. </div>
  65. </fieldset>
  66. </form>
  67. <!-- Input elements can be placed outside the form element they are bound to. -->
  68. <label>
  69. <input type="checkbox" name="remember" form="loginForm" />
  70. Remember my login credentials
  71. </label>
  72. ```
  73. Use the library as follows (code is in TypeScript, but can work with JavaScript as well):
  74. ```typescript
  75. import { getFormValues, setFormValues } from '@theoryofnekomata/formxtra';
  76. const form: HTMLFormElement = document.getElementById('loginForm');
  77. const processResponse = async (response: Response) => {
  78. const result = await response.json();
  79. alert(`Welcome ${result.user}!`);
  80. };
  81. // Use formxtra in event handlers
  82. form.addEventListener('submit', async e => {
  83. const {
  84. currentTarget: thisForm,
  85. submitter,
  86. } = e;
  87. e.preventDefault();
  88. const values = getFormValues(thisForm, { submitter });
  89. // Get the form values and send as request to some API
  90. const response = await fetch(
  91. 'https://example.com/api/log-in',
  92. {
  93. method: 'POST',
  94. body: JSON.stringify(values),
  95. headers: {
  96. 'Content-Type': 'application/json',
  97. },
  98. },
  99. );
  100. if (response.ok) {
  101. processResponse(response);
  102. return;
  103. }
  104. alert('Invalid login!');
  105. });
  106. // You can use fomrxtra directly with elements as long as they are bound to a form.
  107. const autofillButton = document.getElementById('autofill');
  108. autofillButton.addEventListener('click', e => {
  109. setFormValues(e.currentTarget.form, { username: 'admin', remember: true });
  110. });
  111. ```
  112. ### API
  113. These are all the exported methods in the library:
  114. ```typescript
  115. import {
  116. getFormValues,
  117. setFormValues,
  118. getValue,
  119. isElementValueIncludedInFormSubmit,
  120. isFieldElement,
  121. } from '@theoryofnekomata/formxtra';
  122. ```
  123. One would usually need only the `getFormValues()` and `setFormValues()` functions, however if the utility functions are
  124. needed, the proper usages are documented via TSDoc comments.
  125. ## Additional Information
  126. The library has been tested on the static DOM using JSDOM, and the real dynamic DOM using Cypress. This is to guarantee
  127. compatibility across environments.
  128. See the [documentation folder](./docs) for more details on this library.
  129. The sources are under the [MIT license](./LICENSE).