From 928cacf6f224005b57966d35486b8c5c882e255b Mon Sep 17 00:00:00 2001 From: Claudio Savino Date: Thu, 13 Jul 2023 13:11:02 +0200 Subject: [PATCH] fix: `get('value')` retrieve empty array removed after `update()` `get('value')` retrieve empty array removed after `update()` --- CHANGELOG.md | 4 ++++ src/Base.ts | 2 +- src/composer.ts | 4 ++-- src/parser.ts | 7 +++++-- tests/data/_.fixes.ts | 2 ++ tests/data/forms/fixes/form.v.ts | 6 +++--- tests/data/forms/fixes/form.z.ts | 33 ++++++++++++++++++++++++++++++++ 7 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 tests/data/forms/fixes/form.z.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index a197a31d..85218a5e 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/Base.ts b/src/Base.ts index 1dd95435..6e38f925 100755 --- a/src/Base.ts +++ b/src/Base.ts @@ -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; diff --git a/src/composer.ts b/src/composer.ts index eedd0352..f055fa42 100644 --- a/src/composer.ts +++ b/src/composer.ts @@ -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'), @@ -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'), diff --git a/src/parser.ts b/src/parser.ts index 5da86a28..f5b3ed8a 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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.")) { diff --git a/tests/data/_.fixes.ts b/tests/data/_.fixes.ts index 885e9e97..396f1312 100755 --- a/tests/data/_.fixes.ts +++ b/tests/data/_.fixes.ts @@ -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"; @@ -69,6 +70,7 @@ export default { $T, $U, $V, + $Z, $425, $472, diff --git a/tests/data/forms/fixes/form.v.ts b/tests/data/forms/fixes/form.v.ts index e155eb76..1886240b 100644 --- a/tests/data/forms/fixes/form.v.ts +++ b/tests/data/forms/fixes/form.v.ts @@ -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( diff --git a/tests/data/forms/fixes/form.z.ts b/tests/data/forms/fixes/form.z.ts new file mode 100644 index 00000000..7c0d0f66 --- /dev/null +++ b/tests/data/forms/fixes/form.z.ts @@ -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))); + } + } + } + ); \ No newline at end of file