-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs: update examples in readme fp-63 * docs: update examples in readme fp-63 * docs: add demos to readme fp-63
- Loading branch information
1 parent
78cacbf
commit dc8de33
Showing
4 changed files
with
116 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,127 +3,97 @@ | |
[![Continuous Integration](https://github.com/what1s1ove/whatislove.dev/actions/workflows/ci.yml/badge.svg)](https://github.com/what1s1ove/whatislove.dev/actions/workflows/ci.yml) | ||
[![Continuous Delivery](https://github.com/what1s1ove/whatislove.dev/actions/workflows/cd.yml/badge.svg)](https://github.com/what1s1ove/whatislove.dev/actions/workflows/cd.yml) | ||
|
||
Gets form-payload (or payload for a named form field) - via `form.elements`. | ||
Gets _proper_ form payload – via `form.elements`. | ||
|
||
## Install | ||
|
||
``` | ||
npm install form-payload | ||
``` | ||
|
||
## Demo | ||
|
||
- [Basic (JavaScript)](https://stackblitz.com/edit/form-payload-basic?file=index.js) | ||
- [Advanced (TypeScript + Validation)](https://stackblitz.com/edit/form-payload-advanced?file=index.ts,get-form-payload.ts) | ||
- [Framework (React + TypeScript)](https://stackblitz.com/edit/form-payload-framework?file=src%2FApp.tsx) | ||
|
||
PS. _The library works perfectly with any framework. It doesn't matter which framework is used; Just use a valid [HTMLFormElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement). The same applies to validations and any other libraries. Just create your own wrappers on top of the functions exported by form-payload library._ | ||
|
||
## Usage | ||
|
||
```html | ||
<!-- index.html --> | ||
<form name="general"> | ||
<label> | ||
Name | ||
<input type="text" name="name" value="Jon" /> | ||
<input type="text" name="name" value="John" /> | ||
</label> | ||
<label> | ||
Birthday | ||
<input type="date" name="birthday" value="2021-03-27" /> | ||
</label> | ||
<label> | ||
Friends Count | ||
<input type="number" name="friendsCount" value="1" /> | ||
</label> | ||
<fieldset name="friend"> | ||
<legend>Friend</legend> | ||
<label> | ||
Friend Name | ||
<input type="test" name="friendName" value="Kate" /> | ||
</label> | ||
</fieldset> | ||
<button type="submit">Submit</button> | ||
</form> | ||
|
||
... | ||
|
||
<form name="mailing"> | ||
<label> | ||
Mailing | ||
<input type="email" name="mail" name="[email protected]" /> | ||
<input type="email" name="mail" value="[email protected]" /> | ||
</label> | ||
</form> | ||
``` | ||
|
||
```js | ||
// index.js | ||
import { getFormPayload, getFormControlPayload } from 'form-payload'; | ||
|
||
const { general: generalFormNode, mailing: mailingFormNode } = document.forms; | ||
|
||
generalFormNode.addEventListener('submit', (evt) => { | ||
evt.preventDefault(); | ||
|
||
console.log(getFormPayload(generalFormNode)); | ||
// { | ||
// name: 'Jon', | ||
// birthday: Sat Mar 27 2021 18:06:42 GMT+0200 (Eastern European Standard Time), | ||
// friendsCount: 1, | ||
// friend: { | ||
// friendName: 'Kate', | ||
// }, | ||
// } | ||
}); | ||
|
||
mailingFormNode.addEventListener('change', (evt) => { | ||
console.log(getFormControlPayload(evt.target)); | ||
// '[email protected]' | ||
}); | ||
``` | ||
|
||
## With Frameworks | ||
<script> | ||
import { getFormPayload, getFormControlPayload } from 'form-payload'; | ||
_It doesn't matter which framework you use, you just need to pass the valid [HTMLFormElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement)._ | ||
const generalFormNode = document.querySelector('form[name="general"]'); | ||
const mailingFormNode = document.querySelector('form[name="mailing"]'); | ||
### React | ||
|
||
```jsx | ||
import { getFormPayload, getFormControlPayload, ControlType } from 'form-payload'; | ||
|
||
const SimpleForm = () => { | ||
const handleSubmit = (evt: React.FormEvent<HTMLFormElement>) => { | ||
generalFormNode.addEventListener('submit', (evt) => { | ||
evt.preventDefault(); | ||
console.log(getFormPayload(evt.target as HTMLFormElement)); | ||
// { | ||
// name: 'Jon', | ||
// birthday: Sat Mar 27 2021 18:06:42 GMT+0200 (Eastern European Standard Time), | ||
// } | ||
}; | ||
|
||
const handleChange = (evt: React.ChangeEvent<HTMLInputElement>) => { | ||
console.log(getFormControlPayload(evt.target)); | ||
// '[email protected]' | ||
}; | ||
|
||
return ( | ||
<> | ||
<form onSubmit={handleSubmit}> | ||
<label> | ||
Name | ||
<input name="name" type={ControlType.TEXT} defaultValue="Jon" /> | ||
</label> | ||
<label> | ||
Date | ||
<input name="birthday" type={ControlType.DATE} defaultValue="2021-03-27" /> | ||
</label> | ||
<button type="submit">Submit</button> | ||
</form> | ||
|
||
... | ||
|
||
<form> | ||
<label> | ||
Mailing | ||
<input name="mail" type={ControlType.EMAIL} onChange={handleChange} /> | ||
</label> | ||
</form> | ||
</> | ||
); | ||
}; | ||
const formPayload = getFormPayload(generalFormNode); | ||
// => { name: 'John', birthday: 'Sat Mar 27 2021 02:00:00 GMT+0200' } | ||
}); | ||
mailingFormNode.addEventListener('input', (evt) => { | ||
const formControlPayload = getFormControlPayload(evt.target); | ||
// => '[email protected]' | ||
}); | ||
</script> | ||
``` | ||
|
||
## MIT Licensed | ||
## Value Correspondence Table | ||
|
||
| HTMLElement | Attributes | Included | Value | | ||
| ------------------------------------------------------------------------------------------- | ---------------------------- | -------- | ----------------------------------------------------------------------------------------------- | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="text"` | ✅ | `string` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="password"` | ✅ | `string` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="email"` | ✅ | `string` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="search"` | ✅ | `string` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="url"` | ✅ | `string` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="tel"` | ✅ | `string` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="color"` | ✅ | `string` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="radio"` | ✅ | `string` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="hidden"` | ✅ | `string` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="number"` | ✅ | `number` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="range"` | ✅ | `number` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="checkbox"` | ✅ | `boolean` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="date"` | ✅ | [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="time"` | ✅ | [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="datetime-local"` | ✅ | [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="month"` | ✅ | [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="week"` | ✅ | [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="file"` | ✅ | [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) or `null` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="file"` and `multiple` | ✅ | `Array<`[`File`](https://developer.mozilla.org/en-US/docs/Web/API/File)`>` | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="button"` | ❌ | – | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="submit"` | ❌ | – | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="reset"` | ❌ | – | | ||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="image"` | ❌ | – | | ||
| [HTMLTextAreaElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextareaElement) | – | ✅ | `string` | | ||
| [HTMLSelectElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement) | – | ✅ | `string` | | ||
| [HTMLSelectElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement) | `multiple` | ✅ | `Array<string>` | | ||
| [HTMLOutputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLOutputElement) | – | ✅ | `string` | | ||
| [HTMLFieldsetElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFieldsetElement) | – | ✅ | `Object<name: string, value: unknown>` (recursive values of nested elements) | | ||
| [HTMLButtonElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement) | `type="button"` | ❌ | – | | ||
| [HTMLButtonElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement) | `type="submit"` | ❌ | – | | ||
| [HTMLButtonElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement) | `type="reset"` | ❌ | – | | ||
| [HTMLObjectElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement) | – | ❌ | – | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,40 @@ | ||
const ControlType = /** @type {const} */ ({ | ||
BUTTON: 'button', | ||
CHECKBOX: 'checkbox', | ||
COLOR: 'color', | ||
DATE: 'date', | ||
DATETIME_LOCAL: 'datetime-local', | ||
TEXT: 'text', | ||
PASSWORD: 'password', | ||
EMAIL: 'email', | ||
FIELDSET: 'fieldset', | ||
FILE: 'file', | ||
SEARCH: 'search', | ||
URL: 'url', | ||
TEL: 'tel', | ||
COLOR: 'color', | ||
RADIO: 'radio', | ||
HIDDEN: 'hidden', | ||
IMAGE: 'image', | ||
MONTH: 'month', | ||
|
||
NUMBER: 'number', | ||
OUTPUT: 'output', | ||
PASSWORD: 'password', | ||
RADIO: 'radio', | ||
RANGE: 'range', | ||
|
||
CHECKBOX: 'checkbox', | ||
|
||
DATE: 'date', | ||
TIME: 'time', | ||
DATETIME_LOCAL: 'datetime-local', | ||
MONTH: 'month', | ||
WEEK: 'week', | ||
|
||
FILE: 'file', | ||
|
||
BUTTON: 'button', | ||
SUBMIT: 'submit', | ||
RESET: 'reset', | ||
SEARCH: 'search', | ||
IMAGE: 'image', | ||
|
||
TEXTAREA: 'textarea', | ||
|
||
SELECT_MULTIPLE: 'select-multiple', | ||
SELECT_ONE: 'select-one', | ||
SUBMIT: 'submit', | ||
TEL: 'tel', | ||
TEXT: 'text', | ||
TEXTAREA: 'textarea', | ||
TIME: 'time', | ||
URL: 'url', | ||
WEEK: 'week', | ||
|
||
OUTPUT: 'output', | ||
|
||
FIELDSET: 'fieldset', | ||
}); | ||
|
||
export { ControlType }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters