TheoryOfNekomata eef3b80ccc | pirms 1 gada | |
---|---|---|
cypress | pirms 1 gada | |
docs | pirms 1 gada | |
src | pirms 1 gada | |
.eslintrc | pirms 1 gada | |
.gitignore | pirms 1 gada | |
.npmignore | pirms 1 gada | |
.prettierrc | pirms 3 gadiem | |
LICENSE | pirms 1 gada | |
README.md | pirms 1 gada | |
cypress.json | pirms 1 gada | |
package.json | pirms 1 gada | |
pridepack.json | pirms 1 gada | |
publish.sh | pirms 3 gadiem | |
test-globals.js | pirms 1 gada | |
tsconfig.eslint.json | pirms 1 gada | |
tsconfig.json | pirms 1 gada | |
vitest.config.js | pirms 1 gada | |
yarn.lock | pirms 1 gada |
The package can be found on:
The package sources can be found on the main Modal Code repository with an active GitHub mirror.
<select>
and <textarea>
) then bind them
to a form:
<form>
.form=""
attribute then specify the form id
where they will be bound.name=""
attributes to your input elements.<form>
element:
onchange
), use the inputElement.form
attribute.getFormValues()
to retrieve all bound input elements’ values, or setFormValues()
to set them (setting only
some fields’ values is supported).Interactive code samples can be found on Codepen:
For an example form:
<form id="loginForm" aria-label="Login Form">
<button id="autofill" type="button">
Autofill login form (username: admin, remember: true)
</button>
<hr />
<fieldset>
<legend>
Login
</legend>
<div>
<input type="text" name="username" placeholder="Username" />
</div>
<div>
<input type="password" name="password" placeholder="Password" />
</div>
<div>
<button type="submit" name="type" value="client">
Log In As Client
</button>
<button type="submit" name="type" value="admin">
Log In As Admin
</button>
</div>
</fieldset>
</form>
<!-- Input elements can be placed outside the form element they are bound to. -->
<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';
const form: HTMLFormElement = document.getElementById('loginForm');
const processResponse = async (response: Response) => {
const result = await response.json();
alert(`Welcome ${result.user}!`);
};
// Use formxtra in 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);
return;
}
alert('Invalid login!');
});
// You can use fomrxtra directly with elements as long as they are bound to a form.
const autofillButton = document.getElementById('autofill');
autofillButton.addEventListener('click', e => {
setFormValues(e.currentTarget.form, { username: 'admin', remember: true });
});
These are all the exported methods in the library:
import {
getFormValues,
setFormValues,
getValue,
isElementValueIncludedInFormSubmit,
isFieldElement,
} from '@theoryofnekomata/formxtra';
One would usually need only the getFormValues()
and setFormValues()
functions, however if the utility functions are
needed, the proper usages are documented via TSDoc comments.
The library has been tested on the static DOM using JSDOM, and the real dynamic DOM using Cypress. This is to guarantee compatibility across environments.
See the documentation folder for more details on this library.
The sources are under the MIT license.