From d699cb938d1158237830e2f8466dedfea76d0633 Mon Sep 17 00:00:00 2001 From: Renaud Genevois Date: Wed, 25 Sep 2024 13:04:38 +0200 Subject: [PATCH] negative value for array variables when resizing (#1151) --- src/stories/input-number/source.json | 12 ++++++++++++ .../reducer/commons/resize-array-variable.ts | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/stories/input-number/source.json b/src/stories/input-number/source.json index 34dbadfa4..96da7b3e3 100644 --- a/src/stories/input-number/source.json +++ b/src/stories/input-number/source.json @@ -17,6 +17,7 @@ "response": { "name": "NB" } } ], + "resizing": { "NB": { "size": "NB", "variables": ["NAMES"] } }, "variables": [ { "variableType": "COLLECTED", @@ -28,6 +29,17 @@ "EDITED": null, "INPUTED": null } + }, + { + "variableType": "COLLECTED", + "name": "NAMES", + "values": { + "PREVIOUS": null, + "COLLECTED": [null], + "FORCED": null, + "EDITED": null, + "INPUTED": null + } } ] } diff --git a/src/use-lunatic/reducer/commons/resize-array-variable.ts b/src/use-lunatic/reducer/commons/resize-array-variable.ts index 975c20c90..57197f2c6 100644 --- a/src/use-lunatic/reducer/commons/resize-array-variable.ts +++ b/src/use-lunatic/reducer/commons/resize-array-variable.ts @@ -6,10 +6,14 @@ function resizeArrayVariable( length: number, defaultValue?: T ): T[] { + if (length < 0) { + return []; + } if (!Array.isArray(array)) { // create the array return new Array(length).fill(defaultValue); - } else if (array.length !== length) { + } + if (array.length !== length) { // renew array end keep previous values return new Array(length) .fill(defaultValue)