@inseefr/lunatic 3.4.11-rc.0 → 3.5.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/components/Loop/Loop.js +7 -27
  2. package/components/Loop/Loop.js.map +1 -1
  3. package/components/RosterForLoop/RosterForLoop.js +8 -37
  4. package/components/RosterForLoop/RosterForLoop.js.map +1 -1
  5. package/components/Sequence/Sequence.d.ts +1 -1
  6. package/components/Subsequence/Subsequence.d.ts +1 -1
  7. package/components/library.d.ts +2 -2
  8. package/esm/components/Loop/Loop.js +7 -27
  9. package/esm/components/Loop/Loop.js.map +1 -1
  10. package/esm/components/RosterForLoop/RosterForLoop.js +8 -34
  11. package/esm/components/RosterForLoop/RosterForLoop.js.map +1 -1
  12. package/esm/components/Sequence/Sequence.d.ts +1 -1
  13. package/esm/components/Subsequence/Subsequence.d.ts +1 -1
  14. package/esm/components/library.d.ts +2 -2
  15. package/esm/hooks/useArticulation.d.ts +58 -0
  16. package/esm/hooks/useArticulation.js +81 -0
  17. package/esm/hooks/useArticulation.js.map +1 -0
  18. package/esm/i18n/dictionary.d.ts +0 -8
  19. package/esm/i18n/dictionary.js +0 -5
  20. package/esm/i18n/dictionary.js.map +1 -1
  21. package/esm/i18n/index.d.ts +1 -1
  22. package/esm/type.source.js +1 -0
  23. package/esm/type.source.js.map +1 -1
  24. package/esm/use-lunatic/commons/variables/behaviours/resizing-behaviour.js +7 -23
  25. package/esm/use-lunatic/commons/variables/behaviours/resizing-behaviour.js.map +1 -1
  26. package/esm/use-lunatic/commons/variables/lunatic-variables-store.d.ts +1 -3
  27. package/esm/use-lunatic/commons/variables/lunatic-variables-store.js +1 -1
  28. package/esm/use-lunatic/commons/variables/lunatic-variables-store.js.map +1 -1
  29. package/esm/use-lunatic/commons/variables/lunatic-variables-store.spec.js +1 -66
  30. package/esm/use-lunatic/commons/variables/lunatic-variables-store.spec.js.map +1 -1
  31. package/esm/use-lunatic/props/getComponentTypeProps.d.ts +4 -4
  32. package/esm/use-lunatic/reducer/commons/index.d.ts +1 -0
  33. package/esm/use-lunatic/reducer/commons/index.js +1 -0
  34. package/esm/use-lunatic/reducer/commons/index.js.map +1 -1
  35. package/esm/use-lunatic/reducer/commons/resize-array-variable.d.ts +5 -0
  36. package/esm/use-lunatic/reducer/commons/resize-array-variable.js +21 -0
  37. package/esm/use-lunatic/reducer/commons/resize-array-variable.js.map +1 -0
  38. package/esm/use-lunatic/reducer/reducerInitializer.d.ts +2 -2
  39. package/esm/use-lunatic/reducer/reducerInitializer.js +31 -24
  40. package/esm/use-lunatic/reducer/reducerInitializer.js.map +1 -1
  41. package/esm/use-lunatic/type.d.ts +0 -1
  42. package/esm/utils/array.d.ts +0 -4
  43. package/esm/utils/array.js +2 -15
  44. package/esm/utils/array.js.map +1 -1
  45. package/esm/utils/array.spec.js +1 -12
  46. package/esm/utils/array.spec.js.map +1 -1
  47. package/hooks/useArticulation.d.ts +58 -0
  48. package/hooks/useArticulation.js +81 -0
  49. package/hooks/useArticulation.js.map +1 -0
  50. package/i18n/dictionary.d.ts +0 -8
  51. package/i18n/dictionary.js +0 -5
  52. package/i18n/dictionary.js.map +1 -1
  53. package/i18n/index.d.ts +1 -1
  54. package/package.json +20 -4
  55. package/src/components/Loop/Loop.tsx +12 -42
  56. package/src/components/RosterForLoop/RosterForLoop.tsx +2 -42
  57. package/src/components/RosterForLoop/__snapshots__/RosterForLoop.spec.tsx.snap +0 -18
  58. package/src/hooks/useArticulation.ts +124 -0
  59. package/src/i18n/dictionary.ts +0 -5
  60. package/src/stories/behaviour/articulation/articulation.stories.tsx +105 -0
  61. package/src/stories/behaviour/articulation/roundabout.json +348 -0
  62. package/src/stories/behaviour/performance/performance.stories.jsx +1 -1
  63. package/src/stories/pairwise/data.json +1 -1
  64. package/src/stories/pairwise/source.json +1 -3
  65. package/src/stories/utils/{SchemaValidator.jsx → SchemaValidator.tsx} +1 -1
  66. package/src/stories/utils/{orchestrator.jsx → orchestrator.tsx} +13 -9
  67. package/src/stories/utils/{overview.jsx → overview.tsx} +2 -0
  68. package/src/type.source.ts +1 -0
  69. package/src/use-lunatic/__snapshots__/use-lunatic.test.ts.snap +323 -0
  70. package/src/use-lunatic/commons/variables/behaviours/resizing-behaviour.ts +10 -35
  71. package/src/use-lunatic/commons/variables/lunatic-variables-store.spec.ts +1 -68
  72. package/src/use-lunatic/commons/variables/lunatic-variables-store.ts +2 -4
  73. package/src/use-lunatic/reducer/commons/index.ts +1 -0
  74. package/src/use-lunatic/reducer/commons/resize-array-variable.ts +28 -0
  75. package/src/use-lunatic/reducer/reducerInitializer.tsx +40 -29
  76. package/src/use-lunatic/type.ts +0 -1
  77. package/src/utils/array.spec.ts +1 -18
  78. package/src/utils/array.ts +3 -21
  79. package/tsconfig.build.tsbuildinfo +1 -1
  80. package/type.source.js +1 -0
  81. package/type.source.js.map +1 -1
  82. package/use-lunatic/commons/variables/behaviours/resizing-behaviour.js +6 -22
  83. package/use-lunatic/commons/variables/behaviours/resizing-behaviour.js.map +1 -1
  84. package/use-lunatic/commons/variables/lunatic-variables-store.d.ts +1 -3
  85. package/use-lunatic/commons/variables/lunatic-variables-store.js +1 -1
  86. package/use-lunatic/commons/variables/lunatic-variables-store.js.map +1 -1
  87. package/use-lunatic/commons/variables/lunatic-variables-store.spec.js +1 -66
  88. package/use-lunatic/commons/variables/lunatic-variables-store.spec.js.map +1 -1
  89. package/use-lunatic/props/getComponentTypeProps.d.ts +4 -4
  90. package/use-lunatic/reducer/commons/index.d.ts +1 -0
  91. package/use-lunatic/reducer/commons/index.js +6 -0
  92. package/use-lunatic/reducer/commons/index.js.map +1 -1
  93. package/use-lunatic/reducer/commons/resize-array-variable.d.ts +5 -0
  94. package/use-lunatic/reducer/commons/resize-array-variable.js +23 -0
  95. package/use-lunatic/reducer/commons/resize-array-variable.js.map +1 -0
  96. package/use-lunatic/reducer/reducerInitializer.d.ts +2 -2
  97. package/use-lunatic/reducer/reducerInitializer.js +31 -24
  98. package/use-lunatic/reducer/reducerInitializer.js.map +1 -1
  99. package/use-lunatic/type.d.ts +0 -1
  100. package/utils/array.d.ts +0 -4
  101. package/utils/array.js +2 -16
  102. package/utils/array.js.map +1 -1
  103. package/utils/array.spec.js +0 -11
  104. package/utils/array.spec.js.map +1 -1
@@ -1855,6 +1855,329 @@ exports[`use-lunatic() > overview > with loop > should handle initialPage 1`] =
1855
1855
  ]
1856
1856
  `;
1857
1857
 
1858
+ exports[`use-lunatic() > overview > with loop > should handle lastReachedPage 1`] = `
1859
+ [
1860
+ {
1861
+ "children": [
1862
+ {
1863
+ "children": [],
1864
+ "current": false,
1865
+ "description": undefined,
1866
+ "id": "lujqeci5",
1867
+ "label": <MDLabel
1868
+ expression="Votre santé"
1869
+ />,
1870
+ "page": "2",
1871
+ "reached": true,
1872
+ "type": "Subsequence",
1873
+ },
1874
+ {
1875
+ "children": [],
1876
+ "current": false,
1877
+ "description": undefined,
1878
+ "id": "lujqbyzl",
1879
+ "label": <MDLabel
1880
+ expression="Votre logement"
1881
+ />,
1882
+ "page": "3",
1883
+ "reached": true,
1884
+ "type": "Subsequence",
1885
+ },
1886
+ ],
1887
+ "current": true,
1888
+ "description": undefined,
1889
+ "id": "lujqfpva",
1890
+ "label": <MDLabel
1891
+ expression="I - Séquence 1"
1892
+ />,
1893
+ "page": "1",
1894
+ "reached": true,
1895
+ "type": "Sequence",
1896
+ },
1897
+ {
1898
+ "children": [
1899
+ {
1900
+ "children": [],
1901
+ "current": false,
1902
+ "description": undefined,
1903
+ "id": "lulbmyhr",
1904
+ "label": <MDLabel
1905
+ expression="Commentaire sur composition du logement"
1906
+ />,
1907
+ "page": "8",
1908
+ "reached": true,
1909
+ "type": "Subsequence",
1910
+ },
1911
+ ],
1912
+ "current": false,
1913
+ "description": undefined,
1914
+ "id": "lujqrqmp",
1915
+ "label": <MDLabel
1916
+ expression="II - Habitants du logement"
1917
+ />,
1918
+ "page": "5",
1919
+ "reached": true,
1920
+ "type": "Sequence",
1921
+ },
1922
+ {
1923
+ "children": [
1924
+ {
1925
+ "children": [],
1926
+ "current": false,
1927
+ "description": undefined,
1928
+ "id": "lujykwaz",
1929
+ "label": <MDLabel
1930
+ expression="Caractéristiques de Quentin"
1931
+ />,
1932
+ "page": "10.1#1",
1933
+ "reached": true,
1934
+ "type": "Subsequence",
1935
+ },
1936
+ {
1937
+ "children": [],
1938
+ "current": false,
1939
+ "description": undefined,
1940
+ "id": "lujykwaz",
1941
+ "label": <MDLabel
1942
+ expression="Caractéristiques de Luna"
1943
+ />,
1944
+ "page": "10.1#2",
1945
+ "reached": true,
1946
+ "type": "Subsequence",
1947
+ },
1948
+ {
1949
+ "children": [],
1950
+ "current": false,
1951
+ "description": undefined,
1952
+ "id": "lujykwaz",
1953
+ "label": <MDLabel
1954
+ expression="Caractéristiques de Paul"
1955
+ />,
1956
+ "page": "10.1#3",
1957
+ "reached": true,
1958
+ "type": "Subsequence",
1959
+ },
1960
+ {
1961
+ "children": [],
1962
+ "current": false,
1963
+ "description": undefined,
1964
+ "id": "lujyik5q",
1965
+ "label": <MDLabel
1966
+ expression="Autres caractéristiques de Luna : filtrée pour le premier individu"
1967
+ />,
1968
+ "page": "11.1#2",
1969
+ "reached": true,
1970
+ "type": "Subsequence",
1971
+ },
1972
+ {
1973
+ "children": [],
1974
+ "current": false,
1975
+ "description": undefined,
1976
+ "id": "luk0swcz",
1977
+ "label": <MDLabel
1978
+ expression="Encore d'autres caractéristiques de Luna : filtrée pour le premier individu"
1979
+ />,
1980
+ "page": "11.2#2",
1981
+ "reached": true,
1982
+ "type": "Subsequence",
1983
+ },
1984
+ {
1985
+ "children": [],
1986
+ "current": false,
1987
+ "description": undefined,
1988
+ "id": "lujyik5q",
1989
+ "label": <MDLabel
1990
+ expression="Autres caractéristiques de Paul : filtrée pour le premier individu"
1991
+ />,
1992
+ "page": "11.1#3",
1993
+ "reached": false,
1994
+ "type": "Subsequence",
1995
+ },
1996
+ {
1997
+ "children": [],
1998
+ "current": false,
1999
+ "description": undefined,
2000
+ "id": "luk0swcz",
2001
+ "label": <MDLabel
2002
+ expression="Encore d'autres caractéristiques de Paul : filtrée pour le premier individu"
2003
+ />,
2004
+ "page": "11.2#3",
2005
+ "reached": false,
2006
+ "type": "Subsequence",
2007
+ },
2008
+ ],
2009
+ "current": false,
2010
+ "description": undefined,
2011
+ "id": "lujyi4pe",
2012
+ "label": <MDLabel
2013
+ expression="III - Détail des individus"
2014
+ />,
2015
+ "page": "9",
2016
+ "reached": true,
2017
+ "type": "Sequence",
2018
+ },
2019
+ {
2020
+ "children": [
2021
+ {
2022
+ "children": [],
2023
+ "current": false,
2024
+ "description": undefined,
2025
+ "id": "lumfc98o",
2026
+ "label": <MDLabel
2027
+ expression="Belle sous-séquence pour Quentin"
2028
+ />,
2029
+ "page": "12.2#1",
2030
+ "reached": false,
2031
+ "type": "Subsequence",
2032
+ },
2033
+ {
2034
+ "children": [],
2035
+ "current": false,
2036
+ "description": undefined,
2037
+ "id": "lumfe3bj",
2038
+ "label": <MDLabel
2039
+ expression="Autre belle sous-seq"
2040
+ />,
2041
+ "page": "12.3#1",
2042
+ "reached": false,
2043
+ "type": "Subsequence",
2044
+ },
2045
+ ],
2046
+ "current": false,
2047
+ "description": undefined,
2048
+ "id": "luk1ojt5",
2049
+ "label": <MDLabel
2050
+ expression="IV - Belle séquence pour Quentin"
2051
+ />,
2052
+ "page": "12.1#1",
2053
+ "reached": false,
2054
+ "type": "Sequence",
2055
+ },
2056
+ {
2057
+ "children": [],
2058
+ "current": false,
2059
+ "description": undefined,
2060
+ "id": "lulbelgr",
2061
+ "label": <MDLabel
2062
+ expression="V - Autre séquence pour Quentin"
2063
+ />,
2064
+ "page": "12.4#1",
2065
+ "reached": false,
2066
+ "type": "Sequence",
2067
+ },
2068
+ {
2069
+ "children": [
2070
+ {
2071
+ "children": [],
2072
+ "current": false,
2073
+ "description": undefined,
2074
+ "id": "lumfc98o",
2075
+ "label": <MDLabel
2076
+ expression="Belle sous-séquence pour Luna"
2077
+ />,
2078
+ "page": "12.2#2",
2079
+ "reached": false,
2080
+ "type": "Subsequence",
2081
+ },
2082
+ {
2083
+ "children": [],
2084
+ "current": false,
2085
+ "description": undefined,
2086
+ "id": "lumfe3bj",
2087
+ "label": <MDLabel
2088
+ expression="Autre belle sous-seq"
2089
+ />,
2090
+ "page": "12.3#2",
2091
+ "reached": false,
2092
+ "type": "Subsequence",
2093
+ },
2094
+ ],
2095
+ "current": false,
2096
+ "description": undefined,
2097
+ "id": "luk1ojt5",
2098
+ "label": <MDLabel
2099
+ expression="IV - Belle séquence pour Luna"
2100
+ />,
2101
+ "page": "12.1#2",
2102
+ "reached": false,
2103
+ "type": "Sequence",
2104
+ },
2105
+ {
2106
+ "children": [],
2107
+ "current": false,
2108
+ "description": undefined,
2109
+ "id": "lulbelgr",
2110
+ "label": <MDLabel
2111
+ expression="V - Autre séquence pour Luna"
2112
+ />,
2113
+ "page": "12.4#2",
2114
+ "reached": false,
2115
+ "type": "Sequence",
2116
+ },
2117
+ {
2118
+ "children": [
2119
+ {
2120
+ "children": [],
2121
+ "current": false,
2122
+ "description": undefined,
2123
+ "id": "lumfc98o",
2124
+ "label": <MDLabel
2125
+ expression="Belle sous-séquence pour Paul"
2126
+ />,
2127
+ "page": "12.2#3",
2128
+ "reached": false,
2129
+ "type": "Subsequence",
2130
+ },
2131
+ {
2132
+ "children": [],
2133
+ "current": false,
2134
+ "description": undefined,
2135
+ "id": "lumfe3bj",
2136
+ "label": <MDLabel
2137
+ expression="Autre belle sous-seq"
2138
+ />,
2139
+ "page": "12.3#3",
2140
+ "reached": false,
2141
+ "type": "Subsequence",
2142
+ },
2143
+ ],
2144
+ "current": false,
2145
+ "description": undefined,
2146
+ "id": "luk1ojt5",
2147
+ "label": <MDLabel
2148
+ expression="IV - Belle séquence pour Paul"
2149
+ />,
2150
+ "page": "12.1#3",
2151
+ "reached": false,
2152
+ "type": "Sequence",
2153
+ },
2154
+ {
2155
+ "children": [],
2156
+ "current": false,
2157
+ "description": undefined,
2158
+ "id": "lulbelgr",
2159
+ "label": <MDLabel
2160
+ expression="V - Autre séquence pour Paul"
2161
+ />,
2162
+ "page": "12.4#3",
2163
+ "reached": false,
2164
+ "type": "Sequence",
2165
+ },
2166
+ {
2167
+ "children": [],
2168
+ "current": false,
2169
+ "description": undefined,
2170
+ "id": "COMMENT-SEQ",
2171
+ "label": <MDLabel
2172
+ expression="Commentaire"
2173
+ />,
2174
+ "page": "13",
2175
+ "reached": false,
2176
+ "type": "Sequence",
2177
+ },
2178
+ ]
2179
+ `;
2180
+
1858
2181
  exports[`use-lunatic() > overview > with loop > should work with loop 1`] = `
1859
2182
  [
1860
2183
  {
@@ -1,8 +1,9 @@
1
1
  import type { LunaticVariablesStore } from '../lunatic-variables-store';
2
2
  import type { LunaticSource } from '../../../type';
3
3
  import { forceInt } from '../../../../utils/number';
4
+ import { resizeArrayVariable } from '../../../reducer/commons';
4
5
  import { getExpressionAsString } from '../../../../utils/vtl';
5
- import { resizeArray, resizeDownArrayWithIndex } from '../../../../utils/array';
6
+ import { resizeArray } from '../../../../utils/array';
6
7
 
7
8
  /**
8
9
  * Resizing behaviour for the store
@@ -41,20 +42,8 @@ export function resizingBehaviour(
41
42
  const newSize = forceInt(store.run(resizingInfo.size));
42
43
  for (const variableName of resizingInfo.variables) {
43
44
  const value = store.get(variableName);
44
- if (Array.isArray(value) && e.detail.removedIndex) {
45
- store.set(
46
- variableName,
47
- resizeDownArrayWithIndex(value, e.detail.removedIndex),
48
- {
49
- cause: 'resizing',
50
- }
51
- );
52
- }
53
- if (
54
- !e.detail.removedIndex &&
55
- (!Array.isArray(value) || value.length !== newSize)
56
- ) {
57
- store.set(variableName, resizeArray(value, newSize, null), {
45
+ if (!Array.isArray(value) || value.length !== newSize) {
46
+ store.set(variableName, resizeArrayVariable(value, newSize, null), {
58
47
  cause: 'resizing',
59
48
  });
60
49
  }
@@ -72,7 +61,6 @@ function resizePairwise(
72
61
  },
73
62
  args: {
74
63
  iteration?: number[];
75
- removedIndex?: number;
76
64
  }
77
65
  ) {
78
66
  // Handle expression being sent as an array or an object (ensure backward compatibility)
@@ -90,25 +78,12 @@ function resizePairwise(
90
78
  });
91
79
  resizingInfo.linksVariables.forEach((variable) => {
92
80
  const value = store.get(variable, args.iteration);
93
- let resizedValue;
94
- if (args.removedIndex) {
95
- const removedIndex = args.removedIndex;
96
- resizedValue = resizeDownArrayWithIndex(
97
- Array.isArray(value)
98
- ? value.map((i) => resizeDownArrayWithIndex(i, removedIndex))
99
- : [],
100
- removedIndex
101
- );
102
- } else {
103
- resizedValue = resizeArray(
104
- // The value is not an array, force an array
105
- Array.isArray(value)
106
- ? value.map((i) => resizeArray(i, ySize, null))
107
- : [],
108
- xSize,
109
- new Array(ySize).fill(null)
110
- );
111
- }
81
+ const resizedValue = resizeArray(
82
+ // The value is not an array, force an array
83
+ Array.isArray(value) ? value.map((i) => resizeArray(i, ySize, null)) : [],
84
+ xSize,
85
+ new Array(ySize).fill(null)
86
+ );
112
87
  store.set(variable, resizedValue);
113
88
  });
114
89
  }
@@ -281,28 +281,6 @@ describe('lunatic-variables-store', () => {
281
281
  cause: 'resizing',
282
282
  });
283
283
  });
284
-
285
- it('should resize variables with Index', () => {
286
- variables.set('PRENOM', ['John', 'Jane', 'Marc']);
287
- variables.set('AGE', [20, 30, 40]);
288
- const spy = vi.fn();
289
- variables.on('change', (e) => spy(e.detail));
290
- resizingBehaviour(variables, {
291
- PRENOM: {
292
- size: 'count(PRENOM)',
293
- variables: ['AGE'],
294
- },
295
- });
296
- variables.set('PRENOM', ['John', 'Marc'], { removedIndex: 1 });
297
- expect((variables.get('PRENOM') as string[]).length).toEqual(2);
298
- expect((variables.get('AGE') as string[]).length).toEqual(2);
299
- expect(spy).toHaveBeenLastCalledWith({
300
- name: 'AGE',
301
- value: [20, 40],
302
- cause: 'resizing',
303
- });
304
- });
305
-
306
284
  it('should resize pairwise with the array syntax', () => {
307
285
  variables.set('PRENOM', []);
308
286
  variables.set('LINKS', [[]]);
@@ -338,29 +316,7 @@ describe('lunatic-variables-store', () => {
338
316
  [null, null, null],
339
317
  ]);
340
318
  });
341
- it('should resize pairwise with the object syntax with index', () => {
342
- variables.set('PRENOM', ['John', 'Jane', 'Marc']);
343
- variables.set('LINKS', [
344
- [null, 2, 4],
345
- [1, null, 2],
346
- [3, 2, null],
347
- ]);
348
- resizingBehaviour(variables, {
349
- PRENOM: {
350
- sizeForLinksVariables: {
351
- xAxisSize: 'count(PRENOM)',
352
- yAxisSize: 'count(PRENOM)',
353
- },
354
- linksVariables: ['LINKS'],
355
- },
356
- });
357
- variables.set('PRENOM', ['John', 'Marc'], { removedIndex: 1 });
358
- expect(variables.get('LINKS') as string[][]).toEqual([
359
- [null, 4],
360
- [3, null],
361
- ]);
362
- });
363
- it('should handle both: pairwise and normal resize', () => {
319
+ it('should handle both pairwise and normal resize', () => {
364
320
  variables.set('PRENOM', []);
365
321
  variables.set('NOM', []);
366
322
  variables.set('LINKS', [[]]);
@@ -380,29 +336,6 @@ describe('lunatic-variables-store', () => {
380
336
  ]);
381
337
  expect(variables.get('NOM') as string[]).toEqual([null, null, null]);
382
338
  });
383
- it('should handle both: pairwise and normal resize with index', () => {
384
- variables.set('PRENOM', ['John', 'Jane', 'Marc']);
385
- variables.set('AGE', [40, 30, 20]);
386
- variables.set('LINKS', [
387
- [null, 2, 4],
388
- [1, null, 2],
389
- [3, 2, null],
390
- ]);
391
- resizingBehaviour(variables, {
392
- PRENOM: {
393
- sizeForLinksVariables: ['count(PRENOM)', 'count(PRENOM)'],
394
- linksVariables: ['LINKS'],
395
- size: 'count(PRENOM)',
396
- variables: ['AGE'],
397
- },
398
- });
399
- variables.set('PRENOM', ['John', 'Marc'], { removedIndex: 1 });
400
- expect(variables.get('LINKS') as string[][]).toEqual([
401
- [null, 4],
402
- [3, null],
403
- ]);
404
- expect(variables.get('AGE') as string[]).toEqual([40, 20]);
405
- });
406
339
  });
407
340
 
408
341
  describe('cleaning', () => {
@@ -33,8 +33,6 @@ export type EventArgs = {
33
33
  value: unknown;
34
34
  /** Iteration changed (for array). */
35
35
  iteration?: IterationLevel | undefined;
36
- /** removedIndex: when resize an array directly with only one handleChange (remove one line in tableLoop) */
37
- removedIndex?: number;
38
36
  /** What triggered this change. */
39
37
  cause?: 'resizing' | 'cleaning';
40
38
  /** Extra sent when setting the variable. */
@@ -62,7 +60,7 @@ export class LunaticVariablesStore {
62
60
  if (!source.variables) {
63
61
  return store;
64
62
  }
65
- // Source data (picked from "variables" in the source.json)s
63
+ // Source data (picked from "variables" in the source.json)
66
64
  const sourceValues: Record<string, unknown> = {};
67
65
  // Starting data for the form (merged with data.json or injected data)
68
66
  const initialValues: Record<string, unknown> = {};
@@ -125,7 +123,7 @@ export class LunaticVariablesStore {
125
123
  public set(
126
124
  name: string,
127
125
  value: unknown,
128
- args: Pick<EventArgs['change'], 'iteration' | 'cause' | 'removedIndex'> = {}
126
+ args: Pick<EventArgs['change'], 'iteration' | 'cause'> = {}
129
127
  ): LunaticVariable {
130
128
  if (!this.dictionary.has(name)) {
131
129
  this.dictionary.set(
@@ -1 +1,2 @@
1
+ export { default as resizeArrayVariable } from './resize-array-variable';
1
2
  export * from './validate-condition-filter';
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Cast the variable into an array and adjust the length if necessary
3
+ */
4
+ function resizeArrayVariable<T = unknown>(
5
+ array: unknown,
6
+ length: number,
7
+ defaultValue?: T
8
+ ): T[] {
9
+ if (!Array.isArray(array)) {
10
+ // create the array
11
+ return new Array(length).fill(defaultValue);
12
+ } else if (array.length !== length) {
13
+ // renew array end keep previous values
14
+ return new Array(length).fill(defaultValue).reduce(function (
15
+ step,
16
+ current,
17
+ index
18
+ ) {
19
+ if (index < array.length) {
20
+ return [...step, array[index]];
21
+ }
22
+ return [...step, current];
23
+ }, []);
24
+ }
25
+ return array;
26
+ }
27
+
28
+ export default resizeArrayVariable;
@@ -38,6 +38,8 @@ const baseState = {
38
38
  options: { disableFilters: false },
39
39
  } satisfies LunaticReducerState;
40
40
 
41
+ const onChange = { current: () => {} };
42
+
41
43
  export function reducerInitializer({
42
44
  source,
43
45
  data,
@@ -47,8 +49,8 @@ export function reducerInitializer({
47
49
  withOverview = false,
48
50
  disableFilters = false,
49
51
  getReferentiel,
50
- onVariableChange,
51
- logger,
52
+ onVariableChange = onChange,
53
+ logger = console.error,
52
54
  }: {
53
55
  source: LunaticSource;
54
56
  data: LunaticData;
@@ -58,8 +60,8 @@ export function reducerInitializer({
58
60
  withOverview?: LunaticOptions['withOverview'];
59
61
  disableFilters?: LunaticOptions['disableFilters'];
60
62
  getReferentiel?: LunaticOptions['getReferentiel'];
61
- onVariableChange: RefObject<LunaticOptions['onVariableChange']>;
62
- logger: LunaticLogger;
63
+ onVariableChange?: RefObject<LunaticOptions['onVariableChange']>;
64
+ logger?: LunaticLogger;
63
65
  }): LunaticReducerState {
64
66
  const variables = LunaticVariablesStore.makeFromSource(
65
67
  source,
@@ -110,11 +112,13 @@ export function reducerInitializer({
110
112
  }
111
113
  return result as any;
112
114
  } catch (e) {
113
- // If there is an error interpreting a variable, return the raw expression
114
- logger({
115
- type: 'ERROR',
116
- error: e as Error,
117
- });
115
+ if (logger) {
116
+ // If there is an error interpreting a variable, return the raw expression
117
+ logger({
118
+ type: 'ERROR',
119
+ error: e as Error,
120
+ });
121
+ }
118
122
  return expressionString;
119
123
  }
120
124
  };
@@ -131,9 +135,9 @@ export function reducerInitializer({
131
135
  const pager = {
132
136
  page: initialPager?.page ?? 1,
133
137
  maxPage: source.maxPage ? parseInt(source.maxPage, 10) : 1,
134
- subPage: undefined,
138
+ subPage: initialPager?.subPage,
135
139
  nbSubPages: undefined,
136
- iteration: undefined,
140
+ iteration: initialPager?.iteration,
137
141
  nbIterations: undefined,
138
142
  lastReachedPage: lastReachedPage ?? initialPage,
139
143
  };
@@ -143,7 +147,7 @@ export function reducerInitializer({
143
147
  pager,
144
148
  previousPager: pager,
145
149
  pages,
146
- isInLoop: false,
150
+ isInLoop: pager.subPage !== undefined,
147
151
  updatedAt: Date.now(),
148
152
  overview: withOverview ? buildOverview(source) : [],
149
153
  updateBindings,
@@ -161,22 +165,29 @@ function fillPagerForLoop(state: LunaticReducerState): LunaticReducerState {
161
165
  return state;
162
166
  }
163
167
  const { isLoop, subPages, iterations, loopDependencies } = pages[pager.page];
164
- if (!isLoop) {
165
- return state;
168
+
169
+ if (
170
+ // For loop, jump at the first page
171
+ isLoop ||
172
+ // For roundabout, jump at the desired iteration / subpage (only if defined)
173
+ (pager?.iteration !== undefined && subPages)
174
+ ) {
175
+ return {
176
+ ...state,
177
+ isInLoop: true,
178
+ pager: {
179
+ ...pager,
180
+ subPage: pager?.subPage ?? 1,
181
+ nbSubPages: (subPages ?? []).length,
182
+ iteration: pager?.iteration ?? 0,
183
+ nbIterations: forceInt(
184
+ state.executeExpression(iterations, {
185
+ deps: loopDependencies,
186
+ })
187
+ ),
188
+ },
189
+ };
166
190
  }
167
- return {
168
- ...state,
169
- isInLoop: true,
170
- pager: {
171
- ...pager,
172
- subPage: pager?.subPage ?? 0,
173
- nbSubPages: (subPages ?? []).length,
174
- iteration: pager?.iteration ?? 0,
175
- nbIterations: forceInt(
176
- state.executeExpression(iterations, {
177
- deps: loopDependencies,
178
- })
179
- ),
180
- },
181
- };
191
+
192
+ return state;
182
193
  }
@@ -349,6 +349,5 @@ export type LunaticChangesHandler = (
349
349
  name: string;
350
350
  value: any;
351
351
  iteration?: number[];
352
- removedIndex?: number;
353
352
  }[]
354
353
  ) => void;