Skip to content

Commit

Permalink
fix: get('value') retrieve empty array removed after update()
Browse files Browse the repository at this point in the history
`get('value')` retrieve empty array removed after `update()`
  • Loading branch information
foxhound87 committed Jul 13, 2023
1 parent 8d18b2f commit 928cacf
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 6.3.3 (master)
Fix: `get('value')` retrieve empty array removed after `update()`
Fix: `composer` returned in `validate/submit` instead of single methods.

# 6.3.2 (master)
Fix: `preserveDeletedFieldsValues` on `add()` action to handle `fields` prop.

Expand Down
2 changes: 1 addition & 1 deletion src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ export default class Base implements BaseInterface {
this.$changed ++;
this.state.form.$changed ++;
execEvent && this.execHook(FieldPropsEnum.onAdd);
return;
return this;
}

let key;
Expand Down
4 changes: 2 additions & 2 deletions src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const composer = (forms: { [key in string]: FormInterface }) => {
Promise.all(Object.values(forms)
.map((form: FormInterface) => form.validate({ showErrors })))
.then(() => ({
get, check, select, instances, clear, reset,
composer: composer(forms),
valid: valid(),
error: error(),
errors: get('error'),
Expand All @@ -60,7 +60,7 @@ export const composer = (forms: { [key in string]: FormInterface }) => {
{ execOnSubmitHook, execValidationHooks , validate }
)))
.then(() => ({
get, check, select, instances, clear, reset,
composer: composer(forms),
valid: valid(),
error: error(),
errors: get('error'),
Expand Down
7 changes: 5 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ const parseArrayProp = (val: any, prop: string, removeNullishValuesInArrays: boo
if (removeNullishValuesInArrays && isValProp) {
return _.without(values, ...[null, undefined, ""]);
}

return values;
};

const parseCheckArray = (field: any, value: any, prop: string, removeNullishValuesInArrays: boolean) =>
field.hasIncrementalKeys ? parseArrayProp(value, prop, removeNullishValuesInArrays) : value;
const parseCheckArray = (field: any, value: any, prop: string, removeNullishValuesInArrays: boolean) => {
if (!field.fields.size && _.isObject(value) && _.isEmpty(value)) return [];
return field.hasIncrementalKeys ? parseArrayProp(value, prop, removeNullishValuesInArrays) : value;
}

const parseCheckOutput = (field: any, prop: string, retrieveNullifiedEmptyStrings = false) => {
if (prop === FieldPropsEnum.value || prop.startsWith("value.")) {
Expand Down
2 changes: 2 additions & 0 deletions tests/data/_.fixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import $S from "./forms/fixes/form.s";
import $T from "./forms/fixes/form.t";
import $U from "./forms/fixes/form.u";
import $V from "./forms/fixes/form.v";
import $Z from "./forms/fixes/form.z";

import $425 from "./forms/fixes/form.425";
import $472 from "./forms/fixes/form.472";
Expand Down Expand Up @@ -69,6 +70,7 @@ export default {
$T,
$U,
$V,
$Z,

$425,
$472,
Expand Down
6 changes: 3 additions & 3 deletions tests/data/forms/fixes/form.v.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Form } from "../../../../src";
import FormInterface from "../../../../src/models/FormInterface";

const generateNewPiece = () => ({
length: Math.floor(Math.random() * 10),
width: Math.floor(Math.random() * 10),
height: Math.floor(Math.random() * 10)
length: 10,
width: 10,
height: 10
});

export default new Form(
Expand Down
33 changes: 33 additions & 0 deletions tests/data/forms/fixes/form.z.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import { Form } from "../../../../src";
import FormInterface from "../../../../src/models/FormInterface";
import { expect } from "chai";

const values = { pieces: [{ id: "2", blocks: [{ id: "1" }] }] };

const removeBlocks = { pieces: [{ id: "3", blocks: [] }] };

export default new Form(
{
fields: [
"pieces[]",
"pieces[].blocks[]",
"pieces[].blocks[].id",
"pieces[].id"
],
values,
},
{
name: 'Fixes-Z',
hooks: {
onInit(form: FormInterface) {

form.update(removeBlocks);

describe("Nested udpate()", () =>
it('blocks should be empty array', () =>
expect(form.values()).to.be.deep.equal(removeBlocks)));
}
}
}
);

0 comments on commit 928cacf

Please sign in to comment.