@jalik/form-parser
Advanced tools
Comparing version 3.1.1 to 3.1.2
@@ -18,3 +18,3 @@ /** | ||
*/ | ||
export declare function getInputValue(element: HTMLInputElement): string | string[] | undefined; | ||
export declare function getInputValue(element: HTMLInputElement): string | string[] | null; | ||
/** | ||
@@ -41,3 +41,4 @@ * Returns select value. | ||
/** | ||
* Checks if field value is an array. | ||
* Checks if field name describes an array | ||
* or if the name is found multiple times in the form. | ||
* @param element | ||
@@ -44,0 +45,0 @@ */ |
@@ -140,6 +140,5 @@ "use strict"; | ||
function getInputValue(element) { | ||
var _a; | ||
var value = element.value; | ||
var value = null; | ||
var form = element.form; | ||
if (isMultipleField(element)) { | ||
var form = element.form; | ||
var fields = form ? getFieldsByName(element.name, form) : [element]; | ||
@@ -151,6 +150,18 @@ if (isCheckableField(element)) { | ||
} | ||
else if (isCheckableField(element)) { | ||
var form = element.form; | ||
var fields = (form ? getFieldsByName(element.name, form) : [element]); | ||
value = (_a = fields.find(function (el) { return el instanceof HTMLInputElement && el.checked; })) === null || _a === void 0 ? void 0 : _a.value; | ||
else if (element instanceof HTMLInputElement) { | ||
if (element.type === 'radio') { | ||
var fields = (form ? getFieldsByName(element.name, form) : [element]); | ||
var field = fields.find(function (el) { return el instanceof HTMLInputElement && el.checked; }); | ||
if (field != null) { | ||
value = field.value; | ||
} | ||
} | ||
else if (element.type === 'checkbox') { | ||
if (element.checked) { | ||
value = element.value; | ||
} | ||
} | ||
else { | ||
value = element.value; | ||
} | ||
} | ||
@@ -242,3 +253,4 @@ return value; | ||
/** | ||
* Checks if field value is an array. | ||
* Checks if field name describes an array | ||
* or if the name is found multiple times in the form. | ||
* @param element | ||
@@ -255,4 +267,3 @@ */ | ||
// Check if form contains other elements with the same name. | ||
if (element.form && | ||
element.type === 'checkbox' && | ||
if (element.form && element.type !== 'radio' && | ||
getFieldsByName(element.name, element.form).length > 1) { | ||
@@ -432,8 +443,2 @@ return true; | ||
} | ||
else if (element instanceof HTMLInputElement && !element.checked) { | ||
var bool = parseBoolean(element.value); | ||
if (typeof bool === 'boolean') { | ||
value = !bool; | ||
} | ||
} | ||
else { | ||
@@ -440,0 +445,0 @@ value = parseBoolean(value); |
@@ -18,3 +18,3 @@ /** | ||
*/ | ||
export declare function getInputValue(element: HTMLInputElement): string | string[] | undefined; | ||
export declare function getInputValue(element: HTMLInputElement): string | string[] | null; | ||
/** | ||
@@ -41,3 +41,4 @@ * Returns select value. | ||
/** | ||
* Checks if field value is an array. | ||
* Checks if field name describes an array | ||
* or if the name is found multiple times in the form. | ||
* @param element | ||
@@ -44,0 +45,0 @@ */ |
@@ -124,5 +124,5 @@ /* | ||
export function getInputValue(element) { | ||
let value = element.value; | ||
let value = null; | ||
const { form } = element; | ||
if (isMultipleField(element)) { | ||
const { form } = element; | ||
let fields = form ? getFieldsByName(element.name, form) : [element]; | ||
@@ -134,6 +134,18 @@ if (isCheckableField(element)) { | ||
} | ||
else if (isCheckableField(element)) { | ||
const { form } = element; | ||
const fields = (form ? getFieldsByName(element.name, form) : [element]); | ||
value = fields.find((el) => el instanceof HTMLInputElement && el.checked)?.value; | ||
else if (element instanceof HTMLInputElement) { | ||
if (element.type === 'radio') { | ||
const fields = (form ? getFieldsByName(element.name, form) : [element]); | ||
const field = fields.find((el) => el instanceof HTMLInputElement && el.checked); | ||
if (field != null) { | ||
value = field.value; | ||
} | ||
} | ||
else if (element.type === 'checkbox') { | ||
if (element.checked) { | ||
value = element.value; | ||
} | ||
} | ||
else { | ||
value = element.value; | ||
} | ||
} | ||
@@ -220,3 +232,4 @@ return value; | ||
/** | ||
* Checks if field value is an array. | ||
* Checks if field name describes an array | ||
* or if the name is found multiple times in the form. | ||
* @param element | ||
@@ -233,4 +246,3 @@ */ | ||
// Check if form contains other elements with the same name. | ||
if (element.form && | ||
element.type === 'checkbox' && | ||
if (element.form && element.type !== 'radio' && | ||
getFieldsByName(element.name, element.form).length > 1) { | ||
@@ -408,8 +420,2 @@ return true; | ||
} | ||
else if (element instanceof HTMLInputElement && !element.checked) { | ||
const bool = parseBoolean(element.value); | ||
if (typeof bool === 'boolean') { | ||
value = !bool; | ||
} | ||
} | ||
else { | ||
@@ -416,0 +422,0 @@ value = parseBoolean(value); |
{ | ||
"name": "@jalik/form-parser", | ||
"version": "3.1.1", | ||
"version": "3.1.2", | ||
"description": "A utility to parse complex forms with minimum effort.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -19,3 +19,3 @@ # @jalik/form-parser | ||
- Clean values using a custom function | ||
* TypeScript declarations ♥ | ||
- TypeScript declarations ♥ | ||
@@ -22,0 +22,0 @@ ## Sandbox |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
94078
1273