From dc8de338c49910114c8bdaec5f52d3da6ca23091 Mon Sep 17 00:00:00 2001 From: Vladyslav Zubko <42296182+what1s1ove@users.noreply.github.com> Date: Fri, 8 Dec 2023 19:18:08 +0200 Subject: [PATCH] docs: prettify readme fp-63 (#82) * docs: update examples in readme fp-63 * docs: update examples in readme fp-63 * docs: add demos to readme fp-63 --- eslint.config.js | 6 + readme.md | 152 +++++++----------- src/libs/enums/control-type.enum.js | 49 +++--- .../get-form-control-payload.js | 40 ++--- 4 files changed, 116 insertions(+), 131 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index e351658..ad15cec 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -171,6 +171,12 @@ const overridesConfigs = [ 'perfectionist/sort-imports': ['off'], }, }, + { + files: ['src/libs/enums/control-type.enum.js'], + rules: { + 'perfectionist/sort-objects': ['off'], + }, + }, ]; /** @type {import('eslint').Linter.FlatConfig[]} */ diff --git a/readme.md b/readme.md index 70b3f81..582d2d4 100644 --- a/readme.md +++ b/readme.md @@ -3,7 +3,7 @@ [![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 @@ -11,119 +11,89 @@ Gets form-payload (or payload for a named form field) - via `form.elements`. 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 -
-... - -``` - -```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)); - // 'example@mail.com' -}); -``` -## With Frameworks + ``` -## 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