Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
TheoryOfNekomata 4c69db6c02 Add coverage reporting pirms 1 gada
cypress Add date tests, duplicates pirms 1 gada
docs Update README pirms 1 gada
src Add date tests, duplicates pirms 1 gada
.eslintrc Switch to pridepack, add setFormValues pirms 1 gada
.gitignore Add coverage reporting pirms 1 gada
.npmignore Add coverage reporting pirms 1 gada
.prettierrc Separate prettierrc pirms 3 gadiem
LICENSE Switch to pridepack, add setFormValues pirms 1 gada
README.md Update README pirms 1 gada
cypress.json Add support for multiple values pirms 1 gada
package.json Add coverage reporting pirms 1 gada
pridepack.json Switch to pridepack, add setFormValues pirms 1 gada
publish.sh Update usage notes, prepare for publishing pirms 3 gadiem
test-globals.js Switch to pridepack, add setFormValues pirms 1 gada
tsconfig.eslint.json Update tsconfig pirms 1 gada
tsconfig.json Update tsconfig pirms 1 gada
vitest.config.js Switch to pridepack, add setFormValues pirms 1 gada
yarn.lock Add coverage reporting pirms 1 gada

README.md

formxtra

formxtra logo

The companion for Web forms!

Extract and set form values through the DOM.

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 });
});

Tests

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