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.
 
 
 
TheoryOfNekomata 8511d76949 Update README il y a 1 an
cypress Cover all input types il y a 1 an
docs Update README il y a 1 an
src Update documentation il y a 1 an
.eslintrc Switch to pridepack, add setFormValues il y a 1 an
.gitignore Add coverage reporting il y a 1 an
.npmignore Add coverage reporting il y a 1 an
.prettierrc Separate prettierrc il y a 3 ans
LICENSE Switch to pridepack, add setFormValues il y a 1 an
README.md Update README il y a 1 an
cypress.json Add support for multiple values il y a 1 an
package.json Update tests, fix multiple value setting il y a 1 an
pridepack.json Switch to pridepack, add setFormValues il y a 1 an
publish.sh Update usage notes, prepare for publishing il y a 3 ans
test-globals.js Switch to pridepack, add setFormValues il y a 1 an
tsconfig.eslint.json Update tsconfig il y a 1 an
tsconfig.json Update tsconfig il y a 1 an
vitest.config.js Switch to pridepack, add setFormValues il y a 1 an
yarn.lock Add coverage reporting il y a 1 an

README.md

formxtra

The companion for Web forms!

Extract and set form values through the DOM—no frameworks required!

Lightweight. Simple. It Just Works.

Installation

The package can be found on:

Usage

For an example form:

<button id="autofill" type="button">
  Autofill login form
</button>

<form id="loginForm">
  <input type="text" name="username" />
  <input type="password" name="password" />
  <button type="submit" name="type" value="client">
    Log In As Client
  </button>
  <button type="submit" name="type" value="admin">
    Log In As Admin
  </button>
</form>

<label>
  <input type="checkbox" name="remember" form="loginForm" />
  Remember my login credentials
</label>

Use the library as follows (code is in TypeScript, but can work with JavaScript as well):

import { getFormValues, setFormValues } from '@theoryofnekomata/formxtra';

// This is the only query we need. On libraries like React, we can easily get form elements when we
// attach submit event listeners.
const form: HTMLFormElement = document.getElementById('form');

const processResponse = async (response: Response) => {
  const result = await response.json();

  alert(`Welcome ${result.user}!`);
};

// Best use case is with event handlers
form.addEventListener('submit', async e => {
  const {
    currentTarget: thisForm,
    submitter,
  } = e;
  e.preventDefault();

  const values = getFormValues(thisForm, { submitter });

  // Get the form values and send as request to some API
  const response = await fetch(
    'https://example.com/api/log-in',
    {
      method: 'POST',
      body: JSON.stringify(values),
      headers: {
        'Content-Type': 'application/json',
      },
    },
  );

  if (response.ok) {
    processResponse(response);
  }
});

const autofillButton = document.getElementById('autofill');

autofillButton.addEventListener('click', e => {
  setFormValues(form, { username: 'admin', remember: true });
});

There are utility functions exported alongside getFormValues() and setFormValues(). You may want to use namespace import with this library, i.e. import * as formxtra from '@theoryofnekomata/formxtra'.

See the documentation folder for more details.

Tests

The library has been tested on the static DOM using JSDOM, and the real dynamic DOM using Cypress.

License

MIT