|
@@ -1,11 +1,11 @@ |
|
|
<header style="text-align: center; line-height: 1"> |
|
|
|
|
|
<h1 style="margin: 0"> |
|
|
|
|
|
|
|
|
<header align="center" style="text-align: center !important; line-height: 1 !important; border: 0 !important;"> |
|
|
|
|
|
<h1 style="margin: 0 !important;"> |
|
|
<img src="https://raw.githubusercontent.com/TheoryOfNekomata/formxtra/master/docs/assets/formxtra.svg" alt="formxtra"/> |
|
|
<img src="https://raw.githubusercontent.com/TheoryOfNekomata/formxtra/master/docs/assets/formxtra.svg" alt="formxtra"/> |
|
|
</h1> |
|
|
</h1> |
|
|
<h2 style="margin: 0"> |
|
|
|
|
|
|
|
|
<h2 style="margin: 0 !important;"> |
|
|
The companion for Web forms! |
|
|
The companion for Web forms! |
|
|
</h2> |
|
|
</h2> |
|
|
<p style="font-size: 125%"> |
|
|
|
|
|
|
|
|
<p style="font-size: 125% !important"> |
|
|
Extract and set form values through the DOM—no frameworks required! |
|
|
Extract and set form values through the DOM—no frameworks required! |
|
|
</p> |
|
|
</p> |
|
|
<p> |
|
|
<p> |
|
@@ -13,7 +13,7 @@ |
|
|
</p> |
|
|
</p> |
|
|
</header> |
|
|
</header> |
|
|
|
|
|
|
|
|
## Installation |
|
|
|
|
|
|
|
|
## Installation and Sources |
|
|
|
|
|
|
|
|
The package can be found on: |
|
|
The package can be found on: |
|
|
|
|
|
|
|
@@ -21,6 +21,9 @@ The package can be found on: |
|
|
- [npm](https://npmjs.com/package/@theoryofnekomata/formxtra) |
|
|
- [npm](https://npmjs.com/package/@theoryofnekomata/formxtra) |
|
|
- [GitHub Package Registry](https://github.com/TheoryOfNekomata/formxtra/packages/793279) |
|
|
- [GitHub Package Registry](https://github.com/TheoryOfNekomata/formxtra/packages/793279) |
|
|
|
|
|
|
|
|
|
|
|
The package sources can be found on the [main Modal Code repository](https://code.modal.sh/TheoryOfNekomata/formxtra) |
|
|
|
|
|
with an [active GitHub mirror](https://github.com/TheoryOfNekomata/formxtra). |
|
|
|
|
|
|
|
|
## Usage |
|
|
## Usage |
|
|
|
|
|
|
|
|
1. Lay out your input elements (all valid input types supported including `<select>` and `<textarea>`) then bind them |
|
|
1. Lay out your input elements (all valid input types supported including `<select>` and `<textarea>`) then bind them |
|
@@ -37,12 +40,19 @@ The package can be found on: |
|
|
|
|
|
|
|
|
### Example |
|
|
### Example |
|
|
|
|
|
|
|
|
|
|
|
Interactive code samples can be found on Codepen: |
|
|
|
|
|
|
|
|
|
|
|
* [Vanilla JS usage](https://codepen.io/theoryofnekomata/pen/xxajmvJ) |
|
|
|
|
|
* [React integration](https://codepen.io/theoryofnekomata/pen/RwYyvZN) |
|
|
|
|
|
* [Vue integration](https://codepen.io/theoryofnekomata/pen/gOdzqzM) |
|
|
|
|
|
* [Solid integration](https://codepen.io/theoryofnekomata/pen/QWVrYem) |
|
|
|
|
|
|
|
|
For an example form: |
|
|
For an example form: |
|
|
|
|
|
|
|
|
```html |
|
|
```html |
|
|
<form id="loginForm" aria-label="Login Form"> |
|
|
<form id="loginForm" aria-label="Login Form"> |
|
|
<button id="autofill" type="button"> |
|
|
<button id="autofill" type="button"> |
|
|
Autofill login form (username: admin) |
|
|
|
|
|
|
|
|
Autofill login form (username: admin, remember: true) |
|
|
</button> |
|
|
</button> |
|
|
|
|
|
|
|
|
<hr /> |
|
|
<hr /> |
|
@@ -81,9 +91,7 @@ Use the library as follows (code is in TypeScript, but can work with JavaScript |
|
|
```typescript |
|
|
```typescript |
|
|
import { getFormValues, setFormValues } from '@theoryofnekomata/formxtra'; |
|
|
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 form: HTMLFormElement = document.getElementById('loginForm'); |
|
|
|
|
|
|
|
|
const processResponse = async (response: Response) => { |
|
|
const processResponse = async (response: Response) => { |
|
|
const result = await response.json(); |
|
|
const result = await response.json(); |
|
@@ -91,7 +99,7 @@ const processResponse = async (response: Response) => { |
|
|
alert(`Welcome ${result.user}!`); |
|
|
alert(`Welcome ${result.user}!`); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// Best use case is with event handlers |
|
|
|
|
|
|
|
|
// Use formxtra in event handlers |
|
|
form.addEventListener('submit', async e => { |
|
|
form.addEventListener('submit', async e => { |
|
|
const { |
|
|
const { |
|
|
currentTarget: thisForm, |
|
|
currentTarget: thisForm, |
|
@@ -115,7 +123,10 @@ form.addEventListener('submit', async e => { |
|
|
|
|
|
|
|
|
if (response.ok) { |
|
|
if (response.ok) { |
|
|
processResponse(response); |
|
|
processResponse(response); |
|
|
|
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
alert('Invalid login!'); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// You can use fomrxtra directly with elements as long as they are bound to a form. |
|
|
// You can use fomrxtra directly with elements as long as they are bound to a form. |
|
@@ -126,15 +137,28 @@ autofillButton.addEventListener('click', e => { |
|
|
}); |
|
|
}); |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
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'`. |
|
|
|
|
|
|
|
|
### API |
|
|
|
|
|
|
|
|
|
|
|
These are all the exported methods in the library: |
|
|
|
|
|
|
|
|
|
|
|
```typescript |
|
|
|
|
|
import { |
|
|
|
|
|
getFormValues, |
|
|
|
|
|
setFormValues, |
|
|
|
|
|
getValue, |
|
|
|
|
|
isElementValueIncludedInFormSubmit, |
|
|
|
|
|
isFieldElement, |
|
|
|
|
|
} from '@theoryofnekomata/formxtra'; |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
See the [documentation folder](./docs) for more details. |
|
|
|
|
|
|
|
|
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. |
|
|
|
|
|
|
|
|
## Tests |
|
|
|
|
|
|
|
|
## Additional Information |
|
|
|
|
|
|
|
|
The library has been tested on the static DOM using JSDOM, and the real dynamic DOM using Cypress. |
|
|
|
|
|
|
|
|
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. |
|
|
|
|
|
|
|
|
## License |
|
|
|
|
|
|
|
|
See the [documentation folder](./docs) for more details on this library. |
|
|
|
|
|
|
|
|
[MIT](./LICENSE) |
|
|
|
|
|
|
|
|
The sources are under the [MIT license](./LICENSE). |