Skip to content

Commit

Permalink
docs: prettify readme fp-63 (#82)
Browse files Browse the repository at this point in the history
* docs: update examples in readme fp-63

* docs: update examples in readme fp-63

* docs: add demos to readme fp-63
  • Loading branch information
what1s1ove authored Dec 8, 2023
1 parent 78cacbf commit dc8de33
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 131 deletions.
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[]} */
Expand Down
152 changes: 61 additions & 91 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||||
49 changes: 29 additions & 20 deletions src/libs/enums/control-type.enum.js
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 };
40 changes: 20 additions & 20 deletions src/packages/get-form-control-payload/get-form-control-payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ const getFormControlsPayload = (...controlElements) => {
*/
const getFormControlPayload = (controlNode) => {
switch (controlNode.type) {
case ControlType.COLOR:
case ControlType.EMAIL:
case ControlType.HIDDEN:
case ControlType.TEXT:
case ControlType.PASSWORD:
case ControlType.RADIO:
case ControlType.EMAIL:
case ControlType.SEARCH:
case ControlType.TEL:
case ControlType.TEXT:
case ControlType.URL:
case ControlType.OUTPUT:
case ControlType.TEL:
case ControlType.COLOR:
case ControlType.RADIO:
case ControlType.HIDDEN:
case ControlType.TEXTAREA:
case ControlType.SELECT_ONE: {
case ControlType.SELECT_ONE:
case ControlType.OUTPUT: {
return getFormControlValue(
/**
* @type {HTMLInputElement
Expand All @@ -71,15 +71,6 @@ const getFormControlPayload = (controlNode) => {
*/ (controlNode),
);
}
case ControlType.DATE:
case ControlType.DATETIME_LOCAL:
case ControlType.MONTH:
case ControlType.TIME:
case ControlType.WEEK: {
return getInputDateValue(
/** @type {HTMLInputElement} */ (controlNode),
);
}
case ControlType.NUMBER:
case ControlType.RANGE: {
return getInputNumericValue(
Expand All @@ -91,16 +82,25 @@ const getFormControlPayload = (controlNode) => {
/** @type {HTMLInputElement} */ (controlNode),
);
}
case ControlType.SELECT_MULTIPLE: {
return getMultiSelectValues(
/** @type {HTMLSelectElement} */ (controlNode),
case ControlType.DATE:
case ControlType.TIME:
case ControlType.DATETIME_LOCAL:
case ControlType.MONTH:
case ControlType.WEEK: {
return getInputDateValue(
/** @type {HTMLInputElement} */ (controlNode),
);
}
case ControlType.FILE: {
return getInputFileValue(
/** @type {HTMLInputElement} */ (controlNode),
);
}
case ControlType.SELECT_MULTIPLE: {
return getMultiSelectValues(
/** @type {HTMLSelectElement} */ (controlNode),
);
}
case ControlType.FIELDSET: {
const elements = [
.../** @type {HTMLFieldSetElement} */ (controlNode).elements,
Expand Down

0 comments on commit dc8de33

Please sign in to comment.