-
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.
style: edit folder structure fp-54 (#77)
* style: edit folder structure fp-54 * style: clarify functions names fp-54
- Loading branch information
1 parent
aba6c0b
commit 53624a4
Showing
39 changed files
with
232 additions
and
203 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,14 +50,14 @@ npm install form-payload | |
|
||
```js | ||
// index.js | ||
import { getFormValues, getControlValue } from 'form-payload'; | ||
import { getFormPayload, getFormControlPayload } from 'form-payload'; | ||
|
||
const { general: generalFormNode, mailing: mailingFormNode } = document.forms; | ||
|
||
generalFormNode.addEventListener('submit', (evt) => { | ||
evt.preventDefault(); | ||
|
||
console.log(getFormValues(generalFormNode)); | ||
console.log(getFormPayload(generalFormNode)); | ||
// { | ||
// name: 'Jon', | ||
// birthday: Sat Mar 27 2021 18:06:42 GMT+0200 (Eastern European Standard Time), | ||
|
@@ -69,7 +69,7 @@ generalFormNode.addEventListener('submit', (evt) => { | |
}); | ||
|
||
mailingFormNode.addEventListener('change', (evt) => { | ||
console.log(getControlValue(evt.target)); | ||
console.log(getFormControlPayload(evt.target)); | ||
// '[email protected]' | ||
}); | ||
``` | ||
|
@@ -81,21 +81,21 @@ _It doesn't matter which framework you use, you just need to pass the valid [HTM | |
### React | ||
|
||
```jsx | ||
import { getFormValues, getControlValue, ControlType } from 'form-payload'; | ||
import { getFormPayload, getFormControlPayload, ControlType } from 'form-payload'; | ||
|
||
const SimpleForm = () => { | ||
const handleSubmit = (evt: React.FormEvent<HTMLFormElement>) => { | ||
evt.preventDefault(); | ||
|
||
console.log(getFormValues(evt.target as HTMLFormElement)); | ||
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(getControlValue(evt.target)); | ||
console.log(getFormControlPayload(evt.target)); | ||
// '[email protected]' | ||
}; | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
130 changes: 0 additions & 130 deletions
130
src/helpers/get-control-value/get-control-value.helper.js
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,19 +1,4 @@ | ||
import { getElementsValues } from './helpers/helpers.js'; | ||
|
||
/** @typedef {import('./common/types/types.js').HTMLFormControlElement} HTMLFormControlElement */ | ||
|
||
/** | ||
* @template {Record<string, unknown>} T | ||
* @param {HTMLFormElement} formNode | ||
* @returns {T} | ||
*/ | ||
const getFormValues = (formNode) => { | ||
return getElementsValues( | ||
/** @type {HTMLFormControlElement[]} */ ([...formNode.elements]), | ||
); | ||
}; | ||
|
||
export { getFormValues }; | ||
export { ControlType } from './common/enums/enums.js'; | ||
export { FormPayloadError } from './exceptions/exceptions.js'; | ||
export { getControlValue } from './helpers/helpers.js'; | ||
export { ControlType } from './libs/enums/enums.js'; | ||
export { FormPayloadError } from './libs/exceptions/exceptions.js'; | ||
export { getFormControlPayload } from './packages/get-form-control-payload/get-form-control-payload.js'; | ||
export { getFormPayload } from './packages/get-form-payload/get-form-payload.js'; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { FormPayloadError } from './form-payload-error.exception.js'; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
120 changes: 120 additions & 0 deletions
120
src/packages/get-form-control-payload/get-form-control-payload.js
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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { ControlType } from '../../libs/enums/enums.js'; | ||
import { FormPayloadError } from '../../libs/exceptions/exceptions.js'; | ||
import { | ||
checkIsReferToAnotherNode, | ||
getAllowedElements, | ||
getCheckboxValue, | ||
getFormControlValue, | ||
getInputDateValue, | ||
getInputFileValue, | ||
getInputNumericValue, | ||
getMultiSelectValues, | ||
} from './helpers/helpers.js'; | ||
|
||
/** @typedef {import('../../libs/types/types.js').HTMLFormControlElement} HTMLFormControlElement */ | ||
/** @typedef {import('../../libs/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */ | ||
|
||
/** | ||
* @template {Record<string, unknown>} T | ||
* @param {...HTMLFormControlElement} controlElements | ||
* @returns {T} | ||
*/ | ||
const getFormControlsPayload = (...controlElements) => { | ||
const allowedElements = getAllowedElements(controlElements); | ||
|
||
let elementsValues = /** @type {T} */ ({}); | ||
|
||
for (const element of allowedElements) { | ||
const isReferToAnotherNode = checkIsReferToAnotherNode( | ||
element, | ||
...allowedElements, | ||
); | ||
|
||
if (isReferToAnotherNode) { | ||
continue; | ||
} | ||
|
||
elementsValues = { | ||
...elementsValues, | ||
[element.name]: getFormControlPayload(element), | ||
}; | ||
} | ||
|
||
return elementsValues; | ||
}; | ||
|
||
/** | ||
* @param {HTMLFormOperationalControlElement} controlNode | ||
* @returns {unknown} | ||
* @throws {FormPayloadError} | ||
*/ | ||
const getFormControlPayload = (controlNode) => { | ||
switch (controlNode.type) { | ||
case ControlType.COLOR: | ||
case ControlType.EMAIL: | ||
case ControlType.HIDDEN: | ||
case ControlType.PASSWORD: | ||
case ControlType.RADIO: | ||
case ControlType.SEARCH: | ||
case ControlType.TEL: | ||
case ControlType.TEXT: | ||
case ControlType.URL: | ||
case ControlType.OUTPUT: | ||
case ControlType.TEXTAREA: | ||
case ControlType.SELECT_ONE: { | ||
return getFormControlValue( | ||
/** | ||
* @type {HTMLInputElement | ||
* | HTMLOutputElement | ||
* | HTMLTextAreaElement | ||
* | HTMLSelectElement} | ||
*/ (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( | ||
/** @type {HTMLInputElement} */ (controlNode), | ||
); | ||
} | ||
case ControlType.CHECKBOX: { | ||
return getCheckboxValue( | ||
/** @type {HTMLInputElement} */ (controlNode), | ||
); | ||
} | ||
case ControlType.SELECT_MULTIPLE: { | ||
return getMultiSelectValues( | ||
/** @type {HTMLSelectElement} */ (controlNode), | ||
); | ||
} | ||
case ControlType.FILE: { | ||
return getInputFileValue( | ||
/** @type {HTMLInputElement} */ (controlNode), | ||
); | ||
} | ||
case ControlType.FIELDSET: { | ||
const elements = [ | ||
.../** @type {HTMLFieldSetElement} */ (controlNode).elements, | ||
]; | ||
|
||
return getFormControlsPayload( | ||
.../** @type {HTMLFormControlElement[]} */ (elements), | ||
); | ||
} | ||
} | ||
|
||
throw new FormPayloadError({ | ||
message: 'Unknown control type.', | ||
}); | ||
}; | ||
|
||
export { getFormControlPayload, getFormControlsPayload }; |
2 changes: 1 addition & 1 deletion
2
.../check-is-refer-to-another-node.helper.js → .../check-is-refer-to-another-node.helper.js
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
6 changes: 3 additions & 3 deletions
6
...d-elements/get-allowed-elements.helper.js → ...d-elements/get-allowed-elements.helper.js
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
9 changes: 9 additions & 0 deletions
9
...packages/get-form-control-payload/helpers/get-checkbox-value/get-checkbox-value.helper.js
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @param {HTMLInputElement} element | ||
* @returns {boolean} | ||
*/ | ||
const getCheckboxValue = (element) => { | ||
return element.checked; | ||
}; | ||
|
||
export { getCheckboxValue }; |
12 changes: 12 additions & 0 deletions
12
src/packages/get-form-control-payload/helpers/get-control-value/get-control-value.helper.js
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* @param {HTMLInputElement | ||
* | HTMLOutputElement | ||
* | HTMLTextAreaElement | ||
* | HTMLSelectElement} element | ||
* @returns {string} | ||
*/ | ||
const getFormControlValue = (element) => { | ||
return element.value; | ||
}; | ||
|
||
export { getFormControlValue }; |
9 changes: 9 additions & 0 deletions
9
...ages/get-form-control-payload/helpers/get-input-date-value/get-input-date-value.helper.js
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @param {HTMLInputElement} element | ||
* @returns {Date | null} | ||
*/ | ||
const getInputDateValue = (element) => { | ||
return element.valueAsDate; | ||
}; | ||
|
||
export { getInputDateValue }; |
File renamed without changes.
Oops, something went wrong.