Skip to content

Commit

Permalink
fixed set checkbox checked with array values on implicit names #35
Browse files Browse the repository at this point in the history
  • Loading branch information
brainfoolong committed Aug 13, 2023
1 parent a051f8c commit 59bbdac
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 101 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 2.2.1 - 2023-08-13

* fixed set checkbox checked with array values on implicit names https://github.com/brainfoolong/form-data-json/issues/35

### 2.2.0 - 2023-08-03

* added ES6 module support with `form-data-json.es6.js`
Expand Down
59 changes: 40 additions & 19 deletions dist/form-data-json.es6.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// form-data-json-convert | version: 2.2.0 | url: https://github.com/brainfoolong/form-data-json
// form-data-json-convert | version: 2.2.1 | url: https://github.com/brainfoolong/form-data-json
/**
* Form Data Json Converter
* @link https://github.com/brainfoolong/form-data-json
Expand Down Expand Up @@ -51,17 +51,17 @@ export default class FormDataJson {
* @param {Object} inputs
* @param {Object} values
*/
function recursion(inputs, values) {
function recursiveGetInputValues(inputs, values) {
for (let key in inputs) {
const row = inputs[key];
const objectKey = options.flatList ? row.name : key;
// next level
if (row.type === 'nested') {
if (options.flatList) {
recursion(row.tree, values);
recursiveGetInputValues(row.tree, values);
} else {
values[key] = {};
recursion(row.tree, values[key]);
recursiveGetInputValues(row.tree, values[key]);
}
continue;
}
Expand Down Expand Up @@ -190,7 +190,7 @@ export default class FormDataJson {
}
return count ? newObject : null;
}
recursion(tree, returnObject);
recursiveGetInputValues(tree, returnObject);
if (files.length) {
let filesDone = 0;
let filesRequired = 0;
Expand Down Expand Up @@ -250,17 +250,34 @@ export default class FormDataJson {
* @param {*} inputs
* @param {*} newValues
*/
function recursion(inputs, newValues) {
function resursiveSetInputValues(inputs, newValues) {
if (!FormDataJson.isObject(newValues) && !FormDataJson.isArray(newValues)) return;
for (let key in inputs) {
const row = inputs[key];
const objectKey = options.flatList ? row.name : key;
// next level
if (row.type === 'nested') {
if (options.flatList) {
recursion(row.tree, newValues);
resursiveSetInputValues(row.tree, newValues);
} else if (typeof newValues[objectKey] === 'object') {
recursion(row.tree, newValues[objectKey]);
// check for a very special case of array checkbox values using autoIncrement names and value to set is given an array
// the array contains the values to be checked in a random order, that's because we need to check this specially
const newSubValues = newValues[objectKey];
let newTree = row.tree;
if (FormDataJson.isArray(newSubValues)) {
newTree = {};
for (let subTreeKey in row.tree) {
const subRow = row.tree[subTreeKey];
if (subRow.autoIncrementInputs) {
if (subRow.input.type.toLowerCase() === 'checkbox' && newSubValues.indexOf(subRow.input.value) > -1) {
subRow.input.checked = true;
continue;
}
}
newTree[subTreeKey] = subRow;
}
}
resursiveSetInputValues(newTree, newSubValues);
}
continue;
}
Expand All @@ -285,7 +302,7 @@ export default class FormDataJson {
FormDataJson.setInputValue(row, newValues[objectKey] || null, options.triggerChangeEvent);
}
}
recursion(tree, values);
resursiveSetInputValues(tree, values);
}

/**
Expand All @@ -299,12 +316,12 @@ export default class FormDataJson {
* Recursive reset
* @param {*} inputs
*/
function recursion(inputs) {
function recursiveResetInputValues(inputs) {
for (let key in inputs) {
const row = inputs[key];
// next level
if (row.type === 'nested') {
recursion(row.tree);
recursiveResetInputValues(row.tree);
continue;
}
if (row.type === 'radio') {
Expand Down Expand Up @@ -335,7 +352,7 @@ export default class FormDataJson {
}
}
}
recursion(tree);
recursiveResetInputValues(tree);
}

/**
Expand All @@ -349,12 +366,12 @@ export default class FormDataJson {
* Recursive clear
* @param {*} inputs
*/
function recursion(inputs) {
function recursiveClearInputValues(inputs) {
for (let key in inputs) {
const row = inputs[key];
// next level
if (row.type === 'nested') {
recursion(row.tree);
recursiveClearInputValues(row.tree);
continue;
}
if (row.input) {
Expand All @@ -366,7 +383,7 @@ export default class FormDataJson {
FormDataJson.setInputValue(row, null);
}
}
recursion(tree);
recursiveClearInputValues(tree);
}

/**
Expand Down Expand Up @@ -487,7 +504,8 @@ export default class FormDataJson {
const inputType = (input.type || 'text').toLowerCase();
let name = input.name;
const keyParts = input.name.replace(/\]/g, '').split('[');
if (name.substr(-2) === '[]') {
const isAutoIncrement = name.substr(-2) === '[]';
if (isAutoIncrement) {
if (input instanceof HTMLSelectElement && input.multiple) {
// special for multiple selects, we skip the last empty part to fix double nested arrays
keyParts.pop();
Expand Down Expand Up @@ -521,20 +539,23 @@ export default class FormDataJson {
'type': 'radio',
'name': input.name,
'inputType': inputType,
'inputs': []
'inputs': [],
'autoIncrementInputs': 0
};
}
if (isAutoIncrement) useObject[keyPart].autoIncrementInputs = 1;
useObject[keyPart].inputs.push(input);
} else {
useObject[keyPart] = {
'type': 'default',
'name': input.name,
'inputType': inputType,
'input': input
'input': input,
'autoIncrementInputs': isAutoIncrement ? 1 : 0
};
}
} else {
// it could be possible, than really weird a non standard conform input names result in already
// it could be possible, then really weird a non-standard conform input names result in already
// existing data which we need to override here, for which the check to .tree is for
if (typeof useObject[keyPart] === 'undefined' || typeof useObject[keyPart].tree === 'undefined') {
useObject[keyPart] = {
Expand Down
59 changes: 40 additions & 19 deletions dist/form-data-json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// form-data-json-convert | version: 2.2.0 | url: https://github.com/brainfoolong/form-data-json
// form-data-json-convert | version: 2.2.1 | url: https://github.com/brainfoolong/form-data-json
"use strict";

/**
Expand Down Expand Up @@ -54,17 +54,17 @@ var FormDataJson = /*#__PURE__*/function () {
* @param {Object} inputs
* @param {Object} values
*/
function recursion(inputs, values) {
function recursiveGetInputValues(inputs, values) {
for (var key in inputs) {
var row = inputs[key];
var objectKey = options.flatList ? row.name : key;
// next level
if (row.type === 'nested') {
if (options.flatList) {
recursion(row.tree, values);
recursiveGetInputValues(row.tree, values);
} else {
values[key] = {};
recursion(row.tree, values[key]);
recursiveGetInputValues(row.tree, values[key]);
}
continue;
}
Expand Down Expand Up @@ -193,7 +193,7 @@ var FormDataJson = /*#__PURE__*/function () {
}
return count ? newObject : null;
}
recursion(tree, returnObject);
recursiveGetInputValues(tree, returnObject);
if (files.length) {
var filesDone = 0;
var filesRequired = 0;
Expand Down Expand Up @@ -259,17 +259,34 @@ var FormDataJson = /*#__PURE__*/function () {
* @param {*} inputs
* @param {*} newValues
*/
function recursion(inputs, newValues) {
function resursiveSetInputValues(inputs, newValues) {
if (!FormDataJson.isObject(newValues) && !FormDataJson.isArray(newValues)) return;
for (var key in inputs) {
var row = inputs[key];
var objectKey = options.flatList ? row.name : key;
// next level
if (row.type === 'nested') {
if (options.flatList) {
recursion(row.tree, newValues);
resursiveSetInputValues(row.tree, newValues);
} else if (typeof newValues[objectKey] === 'object') {
recursion(row.tree, newValues[objectKey]);
// check for a very special case of array checkbox values using autoIncrement names and value to set is given an array
// the array contains the values to be checked in a random order, that's because we need to check this specially
var newSubValues = newValues[objectKey];
var newTree = row.tree;
if (FormDataJson.isArray(newSubValues)) {
newTree = {};
for (var subTreeKey in row.tree) {
var subRow = row.tree[subTreeKey];
if (subRow.autoIncrementInputs) {
if (subRow.input.type.toLowerCase() === 'checkbox' && newSubValues.indexOf(subRow.input.value) > -1) {
subRow.input.checked = true;
continue;
}
}
newTree[subTreeKey] = subRow;
}
}
resursiveSetInputValues(newTree, newSubValues);
}
continue;
}
Expand All @@ -294,7 +311,7 @@ var FormDataJson = /*#__PURE__*/function () {
FormDataJson.setInputValue(row, newValues[objectKey] || null, options.triggerChangeEvent);
}
}
recursion(tree, values);
resursiveSetInputValues(tree, values);
}

/**
Expand All @@ -308,12 +325,12 @@ var FormDataJson = /*#__PURE__*/function () {
* Recursive reset
* @param {*} inputs
*/
function recursion(inputs) {
function recursiveResetInputValues(inputs) {
for (var key in inputs) {
var row = inputs[key];
// next level
if (row.type === 'nested') {
recursion(row.tree);
recursiveResetInputValues(row.tree);
continue;
}
if (row.type === 'radio') {
Expand Down Expand Up @@ -344,7 +361,7 @@ var FormDataJson = /*#__PURE__*/function () {
}
}
}
recursion(tree);
recursiveResetInputValues(tree);
}

/**
Expand All @@ -358,12 +375,12 @@ var FormDataJson = /*#__PURE__*/function () {
* Recursive clear
* @param {*} inputs
*/
function recursion(inputs) {
function recursiveClearInputValues(inputs) {
for (var key in inputs) {
var row = inputs[key];
// next level
if (row.type === 'nested') {
recursion(row.tree);
recursiveClearInputValues(row.tree);
continue;
}
if (row.input) {
Expand All @@ -375,7 +392,7 @@ var FormDataJson = /*#__PURE__*/function () {
FormDataJson.setInputValue(row, null);
}
}
recursion(tree);
recursiveClearInputValues(tree);
}

/**
Expand Down Expand Up @@ -496,7 +513,8 @@ var FormDataJson = /*#__PURE__*/function () {
var inputType = (input.type || 'text').toLowerCase();
var name = input.name;
var keyParts = input.name.replace(/\]/g, '').split('[');
if (name.substr(-2) === '[]') {
var isAutoIncrement = name.substr(-2) === '[]';
if (isAutoIncrement) {
if (input instanceof HTMLSelectElement && input.multiple) {
// special for multiple selects, we skip the last empty part to fix double nested arrays
keyParts.pop();
Expand Down Expand Up @@ -530,20 +548,23 @@ var FormDataJson = /*#__PURE__*/function () {
'type': 'radio',
'name': input.name,
'inputType': inputType,
'inputs': []
'inputs': [],
'autoIncrementInputs': 0
};
}
if (isAutoIncrement) useObject[keyPart].autoIncrementInputs = 1;
useObject[keyPart].inputs.push(input);
} else {
useObject[keyPart] = {
'type': 'default',
'name': input.name,
'inputType': inputType,
'input': input
'input': input,
'autoIncrementInputs': isAutoIncrement ? 1 : 0
};
}
} else {
// it could be possible, than really weird a non standard conform input names result in already
// it could be possible, then really weird a non-standard conform input names result in already
// existing data which we need to override here, for which the check to .tree is for
if (typeof useObject[keyPart] === 'undefined' || typeof useObject[keyPart].tree === 'undefined') {
useObject[keyPart] = {
Expand Down
Loading

0 comments on commit 59bbdac

Please sign in to comment.